mobx-bonsai
Version:
A fast lightweight alternative to MobX-State-Tree + Y.js two-way binding
84 lines (83 loc) • 3.18 kB
TypeScript
import { BaseNodeType } from './BaseNodeType';
import { TypedNodeType } from './TypedNodeType';
import { UntypedNodeType } from './UntypedNodeType';
/**
* Property key used to identify a node's type
*/
export declare const nodeTypeKey = "$$type";
/**
* Type of the nodeTypeKey constant
*/
export type NodeTypeKey = typeof nodeTypeKey;
/**
* Value that identifies a node's type (string or number)
*/
export type NodeTypeValue = string | number;
/**
* Value that uniquely identifies a node instance (string or number)
*/
export type NodeKeyValue = string | number;
/**
* Represents any node that has a type designation
*/
export interface NodeWithAnyType {
readonly [nodeTypeKey]: NodeTypeValue;
}
/**
* Combines a specific node type with additional data properties
*
* @template TType - The node's type identifier
* @template TData - Additional data properties for the node
*/
export type TNode<TType extends NodeTypeValue, TData> = {
readonly [nodeTypeKey]: TType;
} & TData;
/**
* Attempts to register a node in the type/key registry
*
* @param node - The node to register
* @returns True if registration was successful
*/
export declare function tryRegisterNodeByTypeAndKey(node: object): boolean;
/**
* A type representing any untyped node type.
*/
export type AnyUntypedNodeType = BaseNodeType<any, "untyped", any, any, unknown>;
/**
* Union of all possible typed node type objects
*/
export type AnyTypedNodeType = BaseNodeType<any, "typed" | "keyed", any, any, unknown>;
/**
* Union of all possible node type objects
*/
export type AnyNodeType = AnyUntypedNodeType | AnyTypedNodeType;
/**
* Retrieves the registered node type for a given type ID
*
* @param typeId - The node type identifier to look up
* @returns The node type object or undefined if not found
*/
export declare function findNodeTypeById(typeId: NodeTypeValue): AnyTypedNodeType | undefined;
export declare function getNodeTypeId<TNode extends NodeWithAnyType>(node: TNode): TNode[NodeTypeKey];
export declare function getNodeTypeId(node: object): NodeTypeValue | undefined;
export declare function getNodeTypeAndKey<TNode extends NodeWithAnyType>(node: TNode): {
type: AnyTypedNodeType;
key: NodeKeyValue | undefined;
};
export declare function getNodeTypeAndKey(node: object): {
type: AnyTypedNodeType | undefined;
key: NodeKeyValue | undefined;
};
export declare function nodeType<TNode extends NodeWithAnyType = never>(type: TNode[NodeTypeKey]): TypedNodeType<TNode>;
export declare function nodeType<TNode extends object = never>(): UntypedNodeType<TNode>;
/**
* Registers a callback function to be invoked after a node of the specified type is initialized.
*
* @template TNode - The type of the node that extends NodeWithAnyType
*
* @param nodeType - The typed node type to attach the initialization callback to
* @param callback - Function to be called with the newly initialized node
*
* @returns A disposer function that can be called to unregister the callback
*/
export declare function onInit<TNode extends NodeWithAnyType>(nodeType: TypedNodeType<TNode>, callback: (node: TNode) => void): import('../../utils/disposable').Dispose;