mobx-bonsai
Version:
A fast lightweight alternative to MobX-State-Tree + Y.js two-way binding
34 lines (33 loc) • 1.95 kB
TypeScript
import { NodeForNodeType } from '../nodeTypeKey/NodeForNodeType';
import { AnyTypedNodeType } from '../nodeTypeKey/nodeType';
/**
* Represents a disposer function to clean up resources after a child node is attached to a parent.
*
* @param runDetachDisposers - When true, detach disposers for the child node will be executed during disposal.
*/
export type OnChildAttachedToDisposer = (runDetachDisposers: boolean) => void;
/**
* Parameters for the onChildAttachedTo function.
*
* @template TChildNodeType - The type of the node to watch for.
* @template TChild - The type of the child node that will be attached.
*
* @property target - A function that returns the target object to watch for child attachments.
* @property childNodeType - The type of node to watch for when being attached.
* @property onChildAttached - Callback executed when a child is attached.
* Should return either a cleanup function or void.
* @property - If true, watches for children attached at any level of the tree. If false, only
* watches for direct children. Defaults to false.
* @property - If true, the callback will be executed for all matching children that are already
* attached. Defaults to false.
*/
export type OnChildAttachedToParams<TChildNodeType, TChild> = {
target: () => object;
childNodeType: TChildNodeType;
onChildAttached: (child: TChild) => (() => void) | void;
deep?: boolean;
fireForCurrentChildren?: boolean;
};
export declare function onChildAttachedTo<const NT extends readonly AnyTypedNodeType[]>(params: OnChildAttachedToParams<NT, NodeForNodeType<NT[number]>>): OnChildAttachedToDisposer;
export declare function onChildAttachedTo<const NT extends AnyTypedNodeType>(params: OnChildAttachedToParams<NT, NodeForNodeType<NT>>): OnChildAttachedToDisposer;
export declare function onChildAttachedTo<T extends object = object>(params: OnChildAttachedToParams<undefined, T>): OnChildAttachedToDisposer;