import {EventBus} from './EventBus' import {Collection} from '../util' import {Bus, Dispatchable} from './types' /** * A non-queued bus implementation that executes subscribers immediately in the main thread. * This bus also supports "propagating" events along to any other connected buses. * Such behavior is useful, e.g., if we want to have a semi-isolated request- * level bus whose events still reach the global EventBus instance. */ export class PropagatingEventBus extends EventBus { protected recipients: Collection = new Collection() async dispatch(event: Dispatchable): Promise { await super.dispatch(event) await this.recipients.promiseMap(bus => bus.dispatch(event)) } /** * Register the given bus to receive events fired on this bus. * @param recipient */ connect(recipient: Bus): void { if ( !this.recipients.includes(recipient) ) { this.recipients.push(recipient) } } }