Analyzing reactivity {π}
Introspection APIs
The following APIs might come in handy if you want to inspect the internal state of MobX while debugging, or want to build cool tools on top of MobX.
Also relevant are the various isObservable* APIs.
getDebugName
Usage:
getDebugName(thing, property?)
Returns a (generated) friendly debug name of an observable object, property, reaction etc. Used for example by the MobX developer tools.
getDependencyTree
Usage:
getDependencyTree(thing, property?).
Returns a tree structure with all observables the given reaction / computation currently depends upon.
getObserverTree
Usage:
getObserverTree(thing, property?).
Returns a tree structure with all reactions / computations that are observing the given observable.
getAtom
Usage:
getAtom(thing, property?).
Returns the backing Atom of a given observable object, property, reaction etc.
Spy
Usage:
spy(listener)
Registers a global spy listener that listens to all events that happen in MobX.
It is similar to attaching an observe listener to all observables at once, but also notifies about running (trans/re)actions and computations.
Used for example by the MobX developer tools.
Example usage of spying all actions:
spy(event => {
if (event.type === "action") {
console.log(`${event.name} with args: ${event.arguments}`)
}
})
Spy listeners always receive one object, which usually has at least a type field. The following events are emitted by default by spy:
| Type | observableKind | Other fields | Nested |
|---|---|---|---|
| action | name, object (scope), arguments[] | yes | |
| scheduled-reaction | name | no | |
| reaction | name | yes | |
| error | name, message, error | no | |
| add,update,remove,delete,splice | Check out Intercept & observe {π} | yes | |
| report-end | spyReportEnd=true, time? (total execution time in ms) | no |
The report-end events are part of an earlier fired event that had spyReportStart: true.
This event indicates the end of an event and this way groups of events with sub-events are created.
This event might report the total execution time as well.
The spy events for observable values are identical to the events passed to observe.
In production builds, the spy API is a no-op as it will be minimized away.
Check out the Intercept & observe {π} section for an extensive overview.
