Spy

July 21, 2020 ยท View on GitHub

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-react-devtools.

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.

eventfieldsnested
actionname, object (scope), argumentsyes
scheduled-reactionnameno
reactionnameyes
computenameno
errormessageno
update (array)object (the array), index, newValue, oldValueyes
update (map)object (observable map instance), name, newValue, oldValueyes
update (object)object (instance), name, newValue, oldValueyes
splice (array)object (the array), index, added, removed, addedCount, removedCountyes
add (map)object, name, newValueyes
add (object)object, name, newValueyes
delete (map)object, name, oldValueyes
create (boxed observable)object (ObservableValue instance), newValueyes

Note that there are events with the signature { spyReportEnd: true, time? }. These events might not have a type field, but they 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. See intercept & observe for an extensive overview.

In production builds, the spy API is a no-op as it will be minimized away.