QEventBroker is an API which provides event broker functionality: dispatch and subscribe to events via observables.
NOTE: this is a work in progress and should not yet be used in any production software.
npm install q-event-broker
Creating the event broker:
const eventBroker = new QEventBroker();
Subscribing to an event type:
//subscribe to an event of type "color"
eventBroker.event$(color).subscribe((data) => {
console.log(data);
});
Subscribing to all events in a single observable:
//subscribe to all events
eventBroker.events$.subscribe((event: IQNodeEvent) => {
switch (event.type) {
case "color":
console.log(event.data);
break;
}
});
Dispatching an event
//dispatch event directly
eventBroker.dispatch(<IQEvent>{
type: "color",
data: "green"
});
//dispatch event via class method decorator
class Test {
@DispatchOnReturn({
eventBroker: eventBroker,
type: "color",
})
getColor() {
return "green";
}
@DispatchOnReturn({
eventBroker: eventBroker,
type: "color",
mapReturnValue: (data) => data.color,
})
getColorMapDispatch() {
return {
color: "green"
};
}
}