UNPKG

2.77 kBTypeScriptView Raw
1import { IStateTreeNode } from "../core/node";
2import { IDisposer } from "../utils";
3export declare type ISerializedActionCall = {
4 name: string;
5 path?: string;
6 args?: any[];
7};
8export interface IActionRecorder {
9 actions: ReadonlyArray<ISerializedActionCall>;
10 stop(): any;
11 replay(target: IStateTreeNode): any;
12}
13/**
14 * Applies an action or a series of actions in a single MobX transaction.
15 * Does not return any value
16 * Takes an action description as produced by the `onAction` middleware.
17 *
18 * @export
19 * @param {Object} target
20 * @param {IActionCall[]} actions
21 * @param {IActionCallOptions} [options]
22 */
23export declare function applyAction(target: IStateTreeNode, actions: ISerializedActionCall | ISerializedActionCall[]): void;
24/**
25 * Small abstraction around `onAction` and `applyAction`, attaches an action listener to a tree and records all the actions emitted.
26 * Returns an recorder object with the following signature:
27 *
28 * @example
29 * export interface IActionRecorder {
30 * // the recorded actions
31 * actions: ISerializedActionCall[]
32 * // stop recording actions
33 * stop(): any
34 * // apply all the recorded actions on the given object
35 * replay(target: IStateTreeNode): any
36 * }
37 *
38 * @export
39 * @param {IStateTreeNode} subject
40 * @returns {IPatchRecorder}
41 */
42export declare function recordActions(subject: IStateTreeNode): IActionRecorder;
43/**
44 * Registers a function that will be invoked for each action that is called on the provided model instance, or to any of its children.
45 * 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.
46 * Action can also be intercepted by middleware using addMiddleware to change the function call before it will be run.
47 *
48 * Not all action arguments might be serializable. For unserializable arguments, a struct like `{ $MST_UNSERIALIZABLE: true, type: "someType" }` will be generated.
49 * 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).
50 * 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.
51 *
52 * @export
53 * @param {IStateTreeNode} target
54 * @param {(call: ISerializedActionCall) => void} listener
55 * @param attachAfter {boolean} (default false) fires the listener *after* the action has executed instead of before.
56 * @returns {IDisposer}
57 */
58export declare function onAction(target: IStateTreeNode, listener: (call: ISerializedActionCall) => void, attachAfter?: boolean): IDisposer;