// NOTE: to access `this` in this handler // we MUST use a real function and NOT the // fat arrow syntax: () => {} bus.on('custom-event', functionhandler() { console.log(this.key); // 'value' });
example
// sequential mode
constbus = events.bus(null, functions.sequence);
bus.on('event', handler1); // runs first bus.on('event', handler2); // runs after
// allow users to register concurrent event handlers; // if any of these handlers throw an exception, the // fired event will be rejected constinterceptors = functions.parallel();
// our internal event handler will run after any // interceptors have run and be provided the results // returned by all the interceptors asyncfunctioninternalHandler(...args, results) { // results is array of values returned from interceptors }
// all interceptors will have access to our context // object (e.g. `this.store` inside the subscriber) constcontext = { store:stores.memoryStore(), };
// execute handlers sequentially // instead of in parallel constbus = events.bus(context, functions.sequence);
bus.on('event', interceptors); // run first bus.on('event', internalHandler); // run second
// we could also have written: // const bus = events.bus(context); // bus.on('event', functions.sequence(interceptors, internalHandler));
Optional factory to create an execution processor. The
default is parallel, but you could also pass sequence
or your own factory method. See the examples.
Provides event publish/subscribe functionality.