Adds same built-in event & side effect handlers as in StatelessEventBus.addBuiltIns()
and the following additions:
EV_SET_VALUE
Resets state path to provided value. See setIn.
Example event definition:
[EV_SET_VALUE, ["path.to.value", val]]
EV_UPDATE_VALUE
Updates a state path's value with provided function and optional extra arguments. See updateIn.
Example event definition:
[EV_UPDATE_VALUE, ["path.to.value", (x, y) => x + y, 1]]
EV_TOGGLE_VALUE
Negates a boolean state value at given path.
Example event definition:
[EV_TOGGLE_VALUE, "path.to.value"]
EV_UNDO
Calls ctx[id].undo()
and uses return value as new state. Assumes ctx[id]
is a History instance, provided via e.g. processQueue({ history })
. The event can be triggered with or without ID. By default "history"
is used as default key to lookup the History
instance. Furthermore, an additional event can be triggered based on if a previous state has been restored or not (basically, if the undo was successful). This is useful for resetting/re-initializing stateful resources after a successful undo action or to notify the user that no more undo's are possible. The new event will be processed in the same frame and has access to the (possibly) restored state. The event structure for these options is shown below:
// using default ID
bus.dispatch([EV_UNDO]);
// using custom history ID
bus.dispatch([EV_UNDO, ["custom"]]);
// using custom ID and dispatch another event after undo
bus.dispatch([EV_UNDO, ["custom", ["ev-undo-success"], ["ev-undo-fail"]]]);
EV_REDO
Similar to EV_UNDO
, but for redo actions.
FX_STATE
Resets state atom to provided value (only a single update per processing frame).
If called during event processing, returns current side effect accumulator / interceptor context. Otherwise returns nothing.
Returns value of internal state. Shorthand for: bus.state.deref()
Adds given events to event queue to be processed by StatelessEventBus.processQueue later on.
-
Dispatches given event after delay
milliseconds (by default 17).
-
-
Adds given events to whatever is the current event queue. If triggered via the FX_DISPATCH_NOW
side effect from an event handler / interceptor, the event will still be executed in the currently active batch / frame. If called from elsewhere, the result is the same as calling dispatch.
-
Prepends given interceptors (or interceptor functions) to selected handlers. If no handler IDs are given, applies instrumentation to all currently registered handlers.
-
-
Merges the new side effects returned from an interceptor into the internal effect accumulator.
-
Takes a collection of side effects generated during event processing and applies them in order of configured priorities.
-
Processes a single event using its configured handler/interceptor chain. Logs warning message and skips processing if no handler is available for the event type.
-
-
Triggers processing of current event queue and returns true
if the any of the processed events caused a state change.
If an event handler triggers the FX_DISPATCH_NOW
side effect, the new event will be added to the currently processed batch and therefore executed in the same frame. Also see dispatchNow.
If the optional ctx
arg is provided it will be merged into the InterceptorContext object passed to each interceptor. Since the merged object is also used to collect triggered side effects, care must be taken that there're no key name clashes.
In order to use the built-in EV_UNDO
, EV_REDO
events, users MUST provide a History (or compatible undo history instance) via the ctx
arg, e.g.
bus.processQueue({ history });
Generated using TypeDoc
Stateful version of StatelessEventBus.
Wraps an IAtom state container (i.e.
Atom
/Cursor
/History
) and provides additional pre-defined event handlers and side effects to manipulate wrapped state. Prefer this as the default implementation for most use cases.