UNPKG

mobx-keystone

Version:

A MobX powered state management solution based on data trees with first class support for TypeScript, snapshots, patches and much more

86 lines (85 loc) 3.1 kB
/** * Returns the sandbox manager of a node, or `undefined` if none. * * @param node Node to check. * @returns The sandbox manager of a node, or `undefined` if none. */ export declare function getNodeSandboxManager(node: object): SandboxManager | undefined; /** * Returns if a given node is a sandboxed node. * * @param node Node to check. * @returns `true` if it is sandboxed, `false` */ export declare function isSandboxedNode(node: object): boolean; /** * Callback function for `SandboxManager.withSandbox`. */ export type WithSandboxCallback<T extends readonly [object, ...object[]], R> = (...nodes: T) => boolean | { commit: boolean; return: R; }; /** * Manager class returned by `sandbox` that allows you to make changes to a sandbox copy of the * original subtree and apply them to the original subtree or reject them. */ export declare class SandboxManager { private readonly subtreeRoot; /** * The sandbox copy of the original subtree. */ private readonly subtreeRootClone; /** * The internal disposer. */ private disposer; /** * The internal `withSandbox` patch recorder. If `undefined`, no `withSandbox` call is being * executed. */ private withSandboxPatchRecorder; /** * Function from `readonlyMiddleware` that will allow actions to be started inside the provided * code block on a readonly node. */ private allowWrite; /** * Whether changes made in the sandbox are currently being committed to the original subtree. */ private isCommitting; /** * Creates an instance of `SandboxManager`. * Do not use directly, use `sandbox` instead. * * @param subtreeRoot Subtree root target object. */ constructor(subtreeRoot: object); /** * Executes `fn` with sandbox copies of the elements of `nodes`. The changes made to the sandbox * in `fn` can be accepted, i.e. applied to the original subtree, or rejected. * * @template T Object type. * @template R Return type. * @param nodes Tuple of objects for which to obtain sandbox copies. * @param fn Function that is called with sandbox copies of the elements of `nodes`. Any changes * made to the sandbox are applied to the original subtree when `fn` returns `true` or * `{ commit: true, ... }`. When `fn` returns `false` or `{ commit: false, ... }` the changes made * to the sandbox are rejected. * @returns Value of type `R` when `fn` returns an object of type `{ commit: boolean; return: R }` * or `void` when `fn` returns a boolean. */ withSandbox<T extends readonly [object, ...object[]], R = void>(nodes: T, fn: WithSandboxCallback<T, R>): R; /** * Disposes of the sandbox. */ dispose(): void; private prepareSandboxChanges; } /** * Creates a sandbox. * * @param subtreeRoot Subtree root target object. * @returns A `SandboxManager` which allows you to manage the sandbox operations and dispose of the * sandbox. */ export declare function sandbox(subtreeRoot: object): SandboxManager;