export declare class Dispatcher extends EventEmitter {
static isDispatcher(v: any): v is Dispatcher;
constructor();
onDispatch(handler: (payload: DispatchedPayload, meta: DispatcherPayloadMeta) => void): () => void;
dispatch(payload: DispatchedPayload, meta?: DispatcherPayloadMeta): void;
pipe(toDispatcher: Dispatcher): () => void;
}
Dispatcher is the central event bus system.
Dispatcher
class have these method.
onDispatch(function(payload){ });
dispatch(payload);
It is similar with EventEmitter of Node.js
But, Dispatcher use payload
object as arguments.
payload
object must have type
property.
Following object is a minimal payload
object.
{
"type": "type"
}
Also, You can put any property to payload object.
{
"type": "show",
"value": "value"
}
Q. Why Almin use payload
object instead emit(key, ...args)
?
A. It is for optimization and limitation.
If apply emit style, we should cast ...args
for passing other dispatcher at every time.
So, Almin use payload
object instead of it without casting.
if v
is instance of Dispatcher, return true
constructor not have arguments.
Add handler
(subscriber) to Dispatcher and return unsubscribe function
const dispatcher = new Dispatcher();
const unsubscribe = dispatcher.onDispatch((payload, meta) => {});
unsubscribe(); // release handler
Dispatch payload
to subscribers.
Delegate payload object to other dispatcher.
const a = new Dispatcher();
const b = new Dispatcher();
// Delegate `a` to `b`
a.pipe(b);
// dispatch and `b` can receive it.
a.dispatch({ type : "a" });