API Overview
addDisposer
target
(IStateTreeNode
)disposer
Use this utility to register a function that should be called whenever the targeted state tree node is destroyed. This is a useful alternative to managing cleanup methods yourself using the beforeDestroy
hook.
const Todo = types.model({
title: types.string
}).actions(self => ({
afterCreate() {
const autoSaveDisposer = reaction(
() => getSnapshot(self),
snapshot => sendSnapshotToServerSomehow(snapshot)
)
// stop sending updates to server if this
// instance is destroyed
addDisposer(self, autoSaveDisposer)
}
}))
addMiddleware
target
(IStateTreeNode
)middleware
Returns IDisposer
.
Middleware can be used to intercept any action that is invoked on the subtree where it is attached. If a tree is protected (by default), this means that any mutation of the tree will pass through your middleware.
applyAction
target
(Object
)actions
(Array<IActionCall>
)options
(IActionCallOptions?
)
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.
applyPatch
target
(Object
)patch
(IJsonPatch
)
Applies a JSON-patch to the given model instance or bails out if the patch couldn't be applied. Can apply a single patch, or an array of patches.
applySnapshot
target
(Object
)snapshot
(Object
)
Applies a snapshot to a given model instances. Patch and snapshot listeners will be invoked as usual.
clone
source
(T
)keepEnvironment
(boolean
|any
): indicates whether the clone should inherit the same environment, or not have an environment. If an object is passed in as second argument, that will act as the environment for the cloned tree (default -true
).
Returns T
.
Returns a deep copy of the given tree node as new tree. Short hand for snapshot(x) = getType(x).create(getSnapshot(x))
.
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 a new instance.
createActionTrackingMiddleware
hooks
Returns IMiddlewareHandler
.
Convenience utility to create action-based middleware that supports async processes more easily. All hooks are called for both synchronous and asynchronous actions, except that either onSuccess
or onFail
is called.
The create middleware tracks the process of an action (assuming it passes the filter
). onResume
can return any value, which will be passed as second argument to any other hook. This makes it possible to keep state during a process.
decorate
middleware
(IMiddlewareHandler
)fn
Returns any
, the original function.
Binds middleware to a specific action.
type.actions(self => {
function takeA____() {
self.toilet.donate()
self.wipe()
self.wipe()
self.toilet.flush()
}
return {
takeA____: decorate(atomic, takeA____)
}
})
destroy
target
Removes a model element from the state tree, and mark it as end-of-life. The element should not be used anymore.
detach
target
Removes a model element from the state tree, and let it live on as a new state tree.
escapeJsonPath
str
Escape slashes and backslashes.
flow
asyncAction
Returns Promise
.
getChildType
object
(IStateTreeNode
)child
(string
)
Returns IType<any, any>
, the declared type of the given sub property of an object, array, or map.
const Box = types.model({ x: 0, y: 0 })
const box = Box.create()
console.log(getChildType(box, "x").name) // 'number'
getEnv
target
(IStateTreeNode
)
Returns any
.
Returns the environment of the current state tree. Returns an empty environment if the tree wasn't initialized with an environment.
getParent
target
(Object
)depth
(Number = 1
): how far should we look upward?
Returns the immediate parent (any
) of this object, or null
.
Note: The immediate parent can be either an object, map, or array, and doesn't necessarily refer to the parent model.
getPath
target
(Object
)
Returns string
.
Returns the path of the given object in the model tree.
getPathParts
target
(Object
)
Returns Array<string>
.
Returns the path of the given object as unescaped string array.
getRelativePath
base
(IStateTreeNode
)target
(IStateTreeNode
)
Returns string
.
Given two state tree nodes that are part of the same tree, returns the shortest JSON path needed to navigate from one to the other.