mobx-bonsai
Version:
A fast lightweight alternative to MobX-State-Tree + Y.js two-way binding
74 lines (73 loc) • 2.97 kB
TypeScript
import { IArrayDidChange, IAtom, IObjectDidChange, ObservableSet } from "mobx";
import { Dispose } from "../utils/disposable";
type ParentNode = {
object: object;
path: string;
};
export type NodeChange = IObjectDidChange | IArrayDidChange;
export type NodeChangeListener = (change: NodeChange) => void;
type NodeData = {
parent: ParentNode | undefined;
parentAtom: IAtom | undefined;
onChangeListeners: NodeChangeListener[];
childrenObjects: ObservableSet<object>;
frozen: boolean;
};
export declare function getNodeData(node: object): NodeData;
export declare function reportNodeParentObserved(node: object): void;
/**
* Checks if the given object is a MobX-Bonsai node.
*
* @param struct The object to check.
* @returns `true` if the object is a MobX-Bonsai node, `false` otherwise.
*/
export declare function isNode(struct: unknown): boolean;
/**
* Checks if the given node is frozen.
* A frozen node is a node that cannot be modified.
*
* @param node - The object to check.
* @returns `true` if the object is a node and is frozen, `false` otherwise.
*/
export declare function isFrozenNode(node: object): boolean;
/**
* Asserts that the given object is a mobx-bonsai node.
*
* @param node The object to check.
* @param argName The name of the argument being checked. This is used in the error message.
* @throws If the object is not a mobx-bonsai node.
*/
export declare function assertIsNode(node: object, argName: string): void;
/**
* Registers a deep change listener on the provided node.
*
* The listener is invoked whenever the node undergoes a change, such as additions,
* updates, or removals within its observable structure. This includes receiving
* events from both object and array mutations.
*
* @param node - The node to attach the change listener to.
* @param listener - The callback function that is called when a change occurs.
* The listener receives two parameters:
* - changeTarget: The node where the change occurred.
* - change: The change event, which is a NodeChange.
*
* @returns A disposer function that, when invoked, unregisters the listener.
*/
export declare function onDeepChange(node: object, listener: NodeChangeListener): Dispose;
export declare const runDetachingDuplicatedNodes: (fn: () => void) => void;
/**
* Converts a plain/observable object or array into a mobx-bonsai node.
* If the data is already a node it is returned as is.
* If the data contains a type and key and they match an already existing node
* then that node is reconciled with the new data and the existing node is returned.
*
* @param struct - The object or array to be converted.
* @param options - Optional configuration object.
* @property {boolean} [skipInit] - If true, skips the initialization phase.
*
* @returns The node, an enhanced observable structure.
*/
export declare const node: <T extends object>(struct: T, options?: {
skipInit?: boolean;
}) => T;
export {};