1 | /**
|
2 | * Returns the _actual_ type of the given tree node. (Or throws)
|
3 | *
|
4 | * @export
|
5 | * @param {IStateTreeNode} object
|
6 | * @returns {IType<S, T>}
|
7 | */
|
8 | export declare function getType<S, T>(object: IStateTreeNode): IType<S, T>;
|
9 | /**
|
10 | * Returns the _declared_ type of the given sub property of an object, array or map.
|
11 | *
|
12 | * @example
|
13 | * const Box = types.model({ x: 0, y: 0 })
|
14 | * const box = Box.create()
|
15 | *
|
16 | * console.log(getChildType(box, "x").name) // 'number'
|
17 | *
|
18 | * @export
|
19 | * @param {IStateTreeNode} object
|
20 | * @param {string} child
|
21 | * @returns {IType<any, any>}
|
22 | */
|
23 | export declare function getChildType(object: IStateTreeNode, child: string): IType<any, any>;
|
24 | /**
|
25 | * Registers a function that will be invoked for each mutation that is applied to the provided model instance, or to any of its children.
|
26 | * See [patches](https://github.com/mobxjs/mobx-state-tree#patches) for more details. onPatch events are emitted immediately and will not await the end of a transaction.
|
27 | * Patches can be used to deep observe a model tree.
|
28 | *
|
29 | * @export
|
30 | * @param {Object} target the model instance from which to receive patches
|
31 | * @param {(patch: IJsonPatch, reversePatch) => void} callback the callback that is invoked for each patch. The reversePatch is a patch that would actually undo the emitted patch
|
32 | * @param {includeOldValue} boolean if oldValue is included in the patches, they can be inverted. However patches will become much bigger and might not be suitable for efficient transport
|
33 | * @returns {IDisposer} function to remove the listener
|
34 | */
|
35 | export declare function onPatch(target: IStateTreeNode, callback: (patch: IJsonPatch, reversePatch: IJsonPatch) => void): IDisposer;
|
36 | export declare function onSnapshot<S>(target: ObservableMap<S>, callback: (snapshot: {
|
37 | [key: string]: S;
|
38 | }) => void): IDisposer;
|
39 | export declare function onSnapshot<S>(target: IObservableArray<S>, callback: (snapshot: S[]) => void): IDisposer;
|
40 | export declare function onSnapshot<S>(target: ISnapshottable<S>, callback: (snapshot: S) => void): IDisposer;
|
41 | /**
|
42 | * Applies a JSON-patch to the given model instance or bails out if the patch couldn't be applied
|
43 | * See [patches](https://github.com/mobxjs/mobx-state-tree#patches) for more details.
|
44 | *
|
45 | * Can apply a single past, or an array of patches.
|
46 | *
|
47 | * @export
|
48 | * @param {Object} target
|
49 | * @param {IJsonPatch} patch
|
50 | * @returns
|
51 | */
|
52 | export declare function applyPatch(target: IStateTreeNode, patch: IJsonPatch | IJsonPatch[]): void;
|
53 | export interface IPatchRecorder {
|
54 | patches: ReadonlyArray<IJsonPatch>;
|
55 | inversePatches: ReadonlyArray<IJsonPatch>;
|
56 | stop(): any;
|
57 | replay(target?: IStateTreeNode): any;
|
58 | undo(target?: IStateTreeNode): void;
|
59 | }
|
60 | /**
|
61 | * Small abstraction around `onPatch` and `applyPatch`, attaches a patch listener to a tree and records all the patches.
|
62 | * Returns an recorder object with the following signature:
|
63 | *
|
64 | * @example
|
65 | * export interface IPatchRecorder {
|
66 | * // the recorded patches
|
67 | * patches: IJsonPatch[]
|
68 | * // the inverse of the recorded patches
|
69 | * inversePatches: IJsonPatch[]
|
70 | * // stop recording patches
|
71 | * stop(target?: IStateTreeNode): any
|
72 | * // resume recording patches
|
73 | * resume()
|
74 | * // apply all the recorded patches on the given target (the original subject if omitted)
|
75 | * replay(target?: IStateTreeNode): any
|
76 | * // reverse apply the recorded patches on the given target (the original subject if omitted)
|
77 | * // stops the recorder if not already stopped
|
78 | * undo(): void
|
79 | * }
|
80 | *
|
81 | * @export
|
82 | * @param {IStateTreeNode} subject
|
83 | * @returns {IPatchRecorder}
|
84 | */
|
85 | export declare function recordPatches(subject: IStateTreeNode): IPatchRecorder;
|
86 | /**
|
87 | * The inverse of `unprotect`
|
88 | *
|
89 | * @export
|
90 | * @param {IStateTreeNode} target
|
91 | *
|
92 | */
|
93 | export declare function protect(target: IStateTreeNode): void;
|
94 | /**
|
95 | * By default it is not allowed to directly modify a model. Models can only be modified through actions.
|
96 | * However, in some cases you don't care about the advantages (like replayability, traceability, etc) this yields.
|
97 | * For example because you are building a PoC or don't have any middleware attached to your tree.
|
98 | *
|
99 | * In that case you can disable this protection by calling `unprotect` on the root of your tree.
|
100 | *
|
101 | * @example
|
102 | * const Todo = types.model({
|
103 | * done: false,
|
104 | * toggle() {
|
105 | * this.done = !this.done
|
106 | * }
|
107 | * })
|
108 | *
|
109 | * const todo = new Todo()
|
110 | * todo.done = true // OK
|
111 | * protect(todo)
|
112 | * todo.done = false // throws!
|
113 | * todo.toggle() // OK
|
114 | */
|
115 | export declare function unprotect(target: IStateTreeNode): void;
|
116 | /**
|
117 | * Returns true if the object is in protected mode, @see protect
|
118 | */
|
119 | export declare function isProtected(target: IStateTreeNode): boolean;
|
120 | /**
|
121 | * Applies a snapshot to a given model instances. Patch and snapshot listeners will be invoked as usual.
|
122 | *
|
123 | * @export
|
124 | * @param {Object} target
|
125 | * @param {Object} snapshot
|
126 | * @returns
|
127 | */
|
128 | export declare function applySnapshot<S, T>(target: IStateTreeNode, snapshot: S): void;
|
129 | export declare function getSnapshot<S>(target: ObservableMap<S>): {
|
130 | [key: string]: S;
|
131 | };
|
132 | export declare function getSnapshot<S>(target: IObservableArray<S>): S[];
|
133 | export declare function getSnapshot<S>(target: ISnapshottable<S>): S;
|
134 | /**
|
135 | * Given a model instance, returns `true` if the object has a parent, that is, is part of another object, map or array
|
136 | *
|
137 | * @export
|
138 | * @param {Object} target
|
139 | * @param {number} depth = 1, how far should we look upward?
|
140 | * @returns {boolean}
|
141 | */
|
142 | export declare function hasParent(target: IStateTreeNode, depth?: number): boolean;
|
143 | export declare function getParent(target: IStateTreeNode, depth?: number): any & IStateTreeNode;
|
144 | export declare function getParent<T>(target: IStateTreeNode, depth?: number): T & IStateTreeNode;
|
145 | export declare function getRoot(target: IStateTreeNode): any & IStateTreeNode;
|
146 | export declare function getRoot<T>(target: IStateTreeNode): T & IStateTreeNode;
|
147 | /**
|
148 | * Returns the path of the given object in the model tree
|
149 | *
|
150 | * @export
|
151 | * @param {Object} target
|
152 | * @returns {string}
|
153 | */
|
154 | export declare function getPath(target: IStateTreeNode): string;
|
155 | /**
|
156 | * Returns the path of the given object as unescaped string array
|
157 | *
|
158 | * @export
|
159 | * @param {Object} target
|
160 | * @returns {string[]}
|
161 | */
|
162 | export declare function getPathParts(target: IStateTreeNode): string[];
|
163 | /**
|
164 | * Returns true if the given object is the root of a model tree
|
165 | *
|
166 | * @export
|
167 | * @param {Object} target
|
168 | * @returns {boolean}
|
169 | */
|
170 | export declare function isRoot(target: IStateTreeNode): boolean;
|
171 | /**
|
172 | * Resolves a path relatively to a given object.
|
173 | * Returns undefined if no value can be found.
|
174 | *
|
175 | * @export
|
176 | * @param {Object} target
|
177 | * @param {string} path - escaped json path
|
178 | * @returns {*}
|
179 | */
|
180 | export declare function resolvePath(target: IStateTreeNode, path: string): IStateTreeNode | any;
|
181 | /**
|
182 | * Resolves a model instance given a root target, the type and the identifier you are searching for.
|
183 | * Returns undefined if no value can be found.
|
184 | *
|
185 | * @export
|
186 | * @param {IType<any, any>} type
|
187 | * @param {IStateTreeNode} target
|
188 | * @param {(string | number)} identifier
|
189 | * @returns {*}
|
190 | */
|
191 | export declare function resolveIdentifier(type: IType<any, any>, target: IStateTreeNode, identifier: string | number): any;
|
192 | /**
|
193 | *
|
194 | *
|
195 | * @export
|
196 | * @param {Object} target
|
197 | * @param {string} path
|
198 | * @returns {*}
|
199 | */
|
200 | export declare function tryResolve(target: IStateTreeNode, path: string): IStateTreeNode | any;
|
201 | /**
|
202 | * Given two state tree nodes that are part of the same tree,
|
203 | * returns the shortest jsonpath needed to navigate from the one to the other
|
204 | *
|
205 | * @export
|
206 | * @param {IStateTreeNode} base
|
207 | * @param {IStateTreeNode} target
|
208 | * @returns {string}
|
209 | */
|
210 | export declare function getRelativePath(base: IStateTreeNode, target: IStateTreeNode): string;
|
211 | /**
|
212 | * Returns a deep copy of the given state tree node as new tree.
|
213 | * Short hand for `snapshot(x) = getType(x).create(getSnapshot(x))`
|
214 | *
|
215 | * _Tip: clone will create a literal copy, including the same identifiers. To modify identifiers etc during cloning, don't use clone but take a snapshot of the tree, modify it, and create new instance_
|
216 | *
|
217 | * @export
|
218 | * @template T
|
219 | * @param {T} source
|
220 | * @param {boolean | any} keepEnvironment indicates whether the clone should inherit the same environment (`true`, the default), or not have an environment (`false`). If an object is passed in as second argument, that will act as the environment for the cloned tree.
|
221 | * @returns {T}
|
222 | */
|
223 | export declare function clone<T extends IStateTreeNode>(source: T, keepEnvironment?: boolean | any): T;
|
224 | /**
|
225 | * Removes a model element from the state tree, and let it live on as a new state tree
|
226 | */
|
227 | export declare function detach<T extends IStateTreeNode>(target: T): T;
|
228 | /**
|
229 | * Removes a model element from the state tree, and mark it as end-of-life; the element should not be used anymore
|
230 | */
|
231 | export declare function destroy(target: IStateTreeNode): void;
|
232 | /**
|
233 | * Returns true if the given state tree node is not killed yet.
|
234 | * This means that the node is still a part of a tree, and that `destroy`
|
235 | * has not been called. If a node is not alive anymore, the only thing one can do with it
|
236 | * is requesting it's last path and snapshot
|
237 | *
|
238 | * @export
|
239 | * @param {IStateTreeNode} target
|
240 | * @returns {boolean}
|
241 | */
|
242 | export declare function isAlive(target: IStateTreeNode): boolean;
|
243 | /**
|
244 | * Use this utility to register a function that should be called whenever the
|
245 | * targeted state tree node is destroyed. This is a useful alternative to managing
|
246 | * cleanup methods yourself using the `beforeDestroy` hook.
|
247 | *
|
248 | * @example
|
249 | * const Todo = types.model({
|
250 | * title: types.string
|
251 | * }, {
|
252 | * afterCreate() {
|
253 | * const autoSaveDisposer = reaction(
|
254 | * () => getSnapshot(this),
|
255 | * snapshot => sendSnapshotToServerSomehow(snapshot)
|
256 | * )
|
257 | * // stop sending updates to server if this
|
258 | * // instance is destroyed
|
259 | * addDisposer(this, autoSaveDisposer)
|
260 | * }
|
261 | * })
|
262 | *
|
263 | * @export
|
264 | * @param {IStateTreeNode} target
|
265 | * @param {() => void} disposer
|
266 | */
|
267 | export declare function addDisposer(target: IStateTreeNode, disposer: () => void): void;
|
268 | /**
|
269 | * Returns the environment of the current state tree. For more info on environments,
|
270 | * see [Dependency injection](https://github.com/mobxjs/mobx-state-tree#dependency-injection)
|
271 | *
|
272 | * Returns an empty environment if the tree wasn't initialized with an environment
|
273 | *
|
274 | * @export
|
275 | * @param {IStateTreeNode} target
|
276 | * @returns {*}
|
277 | */
|
278 | export declare function getEnv<T = any>(target: IStateTreeNode): T;
|
279 | /**
|
280 | * Performs a depth first walk through a tree
|
281 | */
|
282 | export declare function walk(target: IStateTreeNode, processor: (item: IStateTreeNode) => void): void;
|
283 | import { IObservableArray, ObservableMap } from "mobx";
|
284 | import { IStateTreeNode } from "./node";
|
285 | import { IJsonPatch } from "./json-patch";
|
286 | import { IDisposer } from "../utils";
|
287 | import { ISnapshottable, IType } from "../types/type";
|