1 | import { IDisposer, IAnyStateTreeNode, IActionContext } from "../internal";
|
2 | export interface ISerializedActionCall {
|
3 | name: string;
|
4 | path?: string;
|
5 | args?: any[];
|
6 | }
|
7 | export interface IActionRecorder {
|
8 | actions: ReadonlyArray<ISerializedActionCall>;
|
9 | readonly recording: boolean;
|
10 | stop(): void;
|
11 | resume(): void;
|
12 | replay(target: IAnyStateTreeNode): void;
|
13 | }
|
14 | /**
|
15 | * Applies an action or a series of actions in a single MobX transaction.
|
16 | * Does not return any value
|
17 | * Takes an action description as produced by the `onAction` middleware.
|
18 | *
|
19 | * @param target
|
20 | * @param actions
|
21 | */
|
22 | export declare function applyAction(target: IAnyStateTreeNode, actions: ISerializedActionCall | ISerializedActionCall[]): void;
|
23 | /**
|
24 | * Small abstraction around `onAction` and `applyAction`, attaches an action listener to a tree and records all the actions emitted.
|
25 | * Returns an recorder object with the following signature:
|
26 | *
|
27 | * Example:
|
28 | * ```ts
|
29 | * export interface IActionRecorder {
|
30 | * // the recorded actions
|
31 | * actions: ISerializedActionCall[]
|
32 | * // true if currently recording
|
33 | * recording: boolean
|
34 | * // stop recording actions
|
35 | * stop(): void
|
36 | * // resume recording actions
|
37 | * resume(): void
|
38 | * // apply all the recorded actions on the given object
|
39 | * replay(target: IAnyStateTreeNode): void
|
40 | * }
|
41 | * ```
|
42 | *
|
43 | * The optional filter function allows to skip recording certain actions.
|
44 | *
|
45 | * @param subject
|
46 | * @returns
|
47 | */
|
48 | export declare function recordActions(subject: IAnyStateTreeNode, filter?: (action: ISerializedActionCall, actionContext: IActionContext | undefined) => boolean): IActionRecorder;
|
49 | /**
|
50 | * Registers a function that will be invoked for each action that is called on the provided model instance, or to any of its children.
|
51 | * 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.
|
52 | * Action can also be intercepted by middleware using addMiddleware to change the function call before it will be run.
|
53 | *
|
54 | * Not all action arguments might be serializable. For unserializable arguments, a struct like `{ $MST_UNSERIALIZABLE: true, type: "someType" }` will be generated.
|
55 | * 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).
|
56 | * 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.
|
57 | *
|
58 | * Example:
|
59 | * ```ts
|
60 | * const Todo = types.model({
|
61 | * task: types.string
|
62 | * })
|
63 | *
|
64 | * const TodoStore = types.model({
|
65 | * todos: types.array(Todo)
|
66 | * }).actions(self => ({
|
67 | * add(todo) {
|
68 | * self.todos.push(todo);
|
69 | * }
|
70 | * }))
|
71 | *
|
72 | * const s = TodoStore.create({ todos: [] })
|
73 | *
|
74 | * let disposer = onAction(s, (call) => {
|
75 | * console.log(call);
|
76 | * })
|
77 | *
|
78 | * s.add({ task: "Grab a coffee" })
|
79 | * // Logs: { name: "add", path: "", args: [{ task: "Grab a coffee" }] }
|
80 | * ```
|
81 | *
|
82 | * @param target
|
83 | * @param listener
|
84 | * @param attachAfter (default false) fires the listener *after* the action has executed instead of before.
|
85 | * @returns
|
86 | */
|
87 | export declare function onAction(target: IAnyStateTreeNode, listener: (call: ISerializedActionCall) => void, attachAfter?: boolean): IDisposer;
|