From 4b44efe6fc7c7f3e3ed8a500981eddc348691cbc Mon Sep 17 00:00:00 2001 From: Tamius Han Date: Fri, 6 May 2022 00:22:35 +0200 Subject: [PATCH] Finish event bus propagation --- src/ext/lib/EventBus.ts | 44 ++++++++++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/src/ext/lib/EventBus.ts b/src/ext/lib/EventBus.ts index 2e766ff..4c2da13 100644 --- a/src/ext/lib/EventBus.ts +++ b/src/ext/lib/EventBus.ts @@ -7,9 +7,24 @@ export interface EventBusCommand { export default class EventBus { private commands: { [x: string]: EventBusCommand[]} = {}; - private downstreamBuses: { [x: string]: EventBus } = {}; + private downstreamBuses: EventBus[] = []; private upstreamBus?: EventBus; + setUpstreamBus(eventBus: EventBus, stopRecursing: boolean = false) { + this.upstreamBus = eventBus; + if (!stopRecursing) { + this.upstreamBus.addDownstreamBus(this, true); + } + } + addDownstreamBus(eventBus: EventBus, stopRecursing: boolean = false) { + if (!this.downstreamBuses.includes(eventBus)) { + this.downstreamBuses.push(eventBus); + + if (!stopRecursing) { + eventBus.setUpstreamBus(this, true); + } + } + } subscribe(commandString: string, command: EventBusCommand) { if (!this.commands[commandString]) { @@ -52,21 +67,26 @@ export default class EventBus { sendGlobal(command: string, config: any) { - if (!this.commands ||!this.commands[command]) { - // ensure send is not being called for commands that we have no subscriptions for - return; - } + this.send(command, config); + this.sendUpstream(command, config); + this.sendDownstream(command, config); + } - for (const eventBusCommand of this.commands[command]) { - this.sendUpstream(command, config); - this.sendDownstream(command, config); + + sendDownstream(command: string, config: any, sourceEventBus?: EventBus) { + for (const eventBus of this.downstreamBuses) { + if (eventBus !== sourceEventBus) { + eventBus.send(command, config); + eventBus.sendDownstream(command, config); + } } } - sendDownstream(command: string, config: any) { - - } sendUpstream(command: string, config: any) { - + if (this.upstreamBus) { + this.upstreamBus.send(command, config); + this.upstreamBus.sendUpstream(command, config); + this.upstreamBus.sendDownstream(command, config, this); + } } }