2021-10-26 22:19:41 +02:00
|
|
|
|
|
|
|
export interface EventBusCommand {
|
|
|
|
isGlobal?: boolean,
|
|
|
|
function: (commandConfig: any) => void | Promise<void>
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class EventBus {
|
|
|
|
|
|
|
|
private commands: { [x: string]: EventBusCommand[]} = {};
|
|
|
|
private downstreamBuses: { [x: string]: EventBus } = {};
|
|
|
|
private upstreamBus?: EventBus;
|
|
|
|
|
|
|
|
|
|
|
|
subscribe(commandString: string, command: EventBusCommand) {
|
|
|
|
if (!this.commands[commandString]) {
|
|
|
|
this.commands[commandString] = [command];
|
|
|
|
} else {
|
|
|
|
this.commands[commandString].push(command);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
send(command: string, config: any, stopPropagation?: boolean) {
|
2021-11-01 01:18:07 +01:00
|
|
|
if (!this.commands ||!this.commands[command]) {
|
|
|
|
// ensure send is not being called for commands that we have no subscriptions for
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-26 22:19:41 +02:00
|
|
|
for (const eventBusCommand of this.commands[command]) {
|
|
|
|
eventBusCommand.function(config);
|
|
|
|
|
|
|
|
if (eventBusCommand.isGlobal && !stopPropagation) {
|
|
|
|
this.sendUpstream(command, config);
|
|
|
|
this.sendDownstream(command, config);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sendGlobal(command: string, config: any) {
|
2021-11-01 01:18:07 +01:00
|
|
|
if (!this.commands ||!this.commands[command]) {
|
|
|
|
// ensure send is not being called for commands that we have no subscriptions for
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-26 22:19:41 +02:00
|
|
|
for (const eventBusCommand of this.commands[command]) {
|
|
|
|
this.sendUpstream(command, config);
|
|
|
|
this.sendDownstream(command, config);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sendDownstream(command: string, config: any) {
|
|
|
|
|
|
|
|
}
|
|
|
|
sendUpstream(command: string, config: any) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|