UNPKG

mobx-state-tree

Version:

Opinionated, transactional, MobX powered state container

59 lines (58 loc) 2.77 kB
import { IStateTreeNode } from "../core/node"; import { IDisposer } from "../utils"; export declare type ISerializedActionCall = { name: string; path?: string; args?: any[]; }; export interface IActionRecorder { actions: ReadonlyArray<ISerializedActionCall>; stop(): any; replay(target: IStateTreeNode): any; } /** * Applies an action or a series of actions in a single MobX transaction. * Does not return any value * Takes an action description as produced by the `onAction` middleware. * * @export * @param {Object} target * @param {IActionCall[]} actions * @param {IActionCallOptions} [options] */ export declare function applyAction(target: IStateTreeNode, actions: ISerializedActionCall | ISerializedActionCall[]): void; /** * Small abstraction around `onAction` and `applyAction`, attaches an action listener to a tree and records all the actions emitted. * Returns an recorder object with the following signature: * * @example * export interface IActionRecorder { * // the recorded actions * actions: ISerializedActionCall[] * // stop recording actions * stop(): any * // apply all the recorded actions on the given object * replay(target: IStateTreeNode): any * } * * @export * @param {IStateTreeNode} subject * @returns {IPatchRecorder} */ export declare function recordActions(subject: IStateTreeNode): IActionRecorder; /** * Registers a function that will be invoked for each action that is called on the provided model instance, or to any of its children. * See [actions](https://github.com/mobxjs/mobx-state-tree#actions) for more details. onAction events are emitted only for the outermost called action in the stack. * Action can also be intercepted by middleware using addMiddleware to change the function call before it will be run. * * Not all action arguments might be serializable. For unserializable arguments, a struct like `{ $MST_UNSERIALIZABLE: true, type: "someType" }` will be generated. * MST Nodes are considered non-serializable as well (they could be serialized as there snapshot, but it is uncertain whether an replaying party will be able to handle such a non-instantiated snapshot). * Rather, when using `onAction` middleware, one should consider in passing arguments which are 1: an id, 2: a (relative) path, or 3: a snapshot. Instead of a real MST node. * * @export * @param {IStateTreeNode} target * @param {(call: ISerializedActionCall) => void} listener * @param attachAfter {boolean} (default false) fires the listener *after* the action has executed instead of before. * @returns {IDisposer} */ export declare function onAction(target: IStateTreeNode, listener: (call: ISerializedActionCall) => void, attachAfter?: boolean): IDisposer;