2022-03-31 04:04:00 +00:00
|
|
|
import {AwareOfContainerLifecycle, Inject, Singleton, StaticInstantiable} from '../../di'
|
2022-01-27 16:34:01 +00:00
|
|
|
import {
|
|
|
|
BusConnectorConfig,
|
|
|
|
BusSubscriber,
|
|
|
|
Event,
|
|
|
|
EventBus,
|
|
|
|
EventHandler,
|
|
|
|
EventHandlerReturn,
|
|
|
|
EventHandlerSubscription,
|
|
|
|
} from './types'
|
2022-03-30 22:24:45 +00:00
|
|
|
import {Awaitable, Collection, ifDebugging, Pipeline, uuid4} from '../../util'
|
2022-01-27 01:37:54 +00:00
|
|
|
import {Logging} from '../../service/Logging'
|
|
|
|
import {Unit} from '../../lifecycle/Unit'
|
2022-01-27 16:34:01 +00:00
|
|
|
import {Config} from '../../service/Config'
|
|
|
|
import {RedisBus} from './RedisBus'
|
|
|
|
import {getEventName} from './getEventName'
|
2022-01-27 01:37:54 +00:00
|
|
|
|
|
|
|
export interface BusInternalSubscription {
|
|
|
|
busUuid: string
|
|
|
|
subscriberUuid: string
|
|
|
|
subscription: EventHandlerSubscription
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Propagating event bus implementation.
|
|
|
|
*/
|
|
|
|
@Singleton()
|
2022-03-31 04:04:00 +00:00
|
|
|
export class Bus<TEvent extends Event = Event> extends Unit implements EventBus<TEvent>, AwareOfContainerLifecycle {
|
|
|
|
awareOfContainerLifecycle: true = true
|
|
|
|
|
2022-01-27 01:37:54 +00:00
|
|
|
@Inject()
|
|
|
|
protected readonly logging!: Logging
|
|
|
|
|
2022-01-27 16:34:01 +00:00
|
|
|
@Inject()
|
|
|
|
protected readonly config!: Config
|
|
|
|
|
2022-01-27 01:37:54 +00:00
|
|
|
public readonly uuid = uuid4()
|
|
|
|
|
|
|
|
/** Local listeners subscribed to events on this bus. */
|
|
|
|
protected subscribers: Collection<BusSubscriber<Event>> = new Collection()
|
|
|
|
|
|
|
|
/** Connections to other event busses to be propagated. */
|
|
|
|
protected connectors: Collection<EventBus> = new Collection()
|
|
|
|
|
|
|
|
protected subscriptions: Collection<BusInternalSubscription> = new Collection()
|
|
|
|
|
|
|
|
/** True if the bus has been initialized. */
|
|
|
|
private isUp = false
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Push an event onto the bus.
|
|
|
|
* @param event
|
|
|
|
*/
|
|
|
|
async push(event: TEvent): Promise<void> {
|
|
|
|
if ( event.originBusUuid === this.uuid ) {
|
2022-01-27 16:34:01 +00:00
|
|
|
this.logging.verbose('Skipping propagation of event, because it originated from this bus.')
|
2022-01-27 01:37:54 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !event.originBusUuid ) {
|
|
|
|
event.originBusUuid = this.uuid
|
|
|
|
}
|
|
|
|
|
2022-01-27 16:34:01 +00:00
|
|
|
this.logging.verbose('Raising event on process-local bus:')
|
|
|
|
this.logging.verbose(event)
|
2022-01-27 01:37:54 +00:00
|
|
|
if ( await this.callSubscribers(event) ) {
|
|
|
|
// One of the subscribers halted propagation of the event
|
2022-01-27 16:34:01 +00:00
|
|
|
this.logging.verbose('Process-local subscriber halted propagation of event.')
|
2022-01-27 01:37:54 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( await this.shouldBroadcast(event) ) {
|
2022-01-27 16:34:01 +00:00
|
|
|
this.logging.verbose('Raising event on connected busses...')
|
2022-01-27 01:37:54 +00:00
|
|
|
await this.connectors.awaitMapCall('push', event)
|
2022-01-27 16:34:01 +00:00
|
|
|
} else {
|
|
|
|
this.logging.verbose('Will not broadcast event.')
|
2022-01-27 01:37:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns true if the given event should be pushed to connected event busses. */
|
|
|
|
protected async shouldBroadcast(event: Event): Promise<boolean> {
|
|
|
|
if ( typeof event.shouldBroadcast === 'function' ) {
|
|
|
|
return event.shouldBroadcast()
|
|
|
|
}
|
|
|
|
|
|
|
|
return Boolean(event.shouldBroadcast)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Call all local listeners for the given event. Returns true if the propagation
|
|
|
|
* of the event should be halted.
|
|
|
|
* @param event
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
protected async callSubscribers(event: TEvent): Promise<boolean> {
|
|
|
|
return this.subscribers
|
|
|
|
.filter(sub => event instanceof sub.eventKey)
|
|
|
|
.pluck('handler')
|
|
|
|
.toAsync()
|
|
|
|
.some(handler => handler(event))
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Register a pipeline as an event handler. */
|
|
|
|
pipe<T extends TEvent>(eventKey: StaticInstantiable<T>, line: Pipeline<T, EventHandlerReturn>): Awaitable<EventHandlerSubscription> {
|
|
|
|
return this.subscribe(eventKey, event => line.apply(event))
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Subscribe to an event on the bus.
|
|
|
|
* @param eventKey
|
|
|
|
* @param handler
|
|
|
|
*/
|
|
|
|
async subscribe<T extends TEvent>(eventKey: StaticInstantiable<T>, handler: EventHandler<T>): Promise<EventHandlerSubscription> {
|
|
|
|
const uuid = uuid4()
|
|
|
|
|
|
|
|
this.subscribers.push({
|
2022-01-27 16:34:01 +00:00
|
|
|
eventName: getEventName(eventKey),
|
2022-01-27 01:37:54 +00:00
|
|
|
handler,
|
|
|
|
eventKey,
|
|
|
|
uuid,
|
|
|
|
} as unknown as BusSubscriber<Event>)
|
|
|
|
|
|
|
|
this.subscriptions.concat(await this.connectors
|
|
|
|
.promiseMap<BusInternalSubscription>(async bus => {
|
2022-01-27 16:34:01 +00:00
|
|
|
this.logging.verbose(`Connecting subscriber to bus ${bus.constructor?.name}#${bus.uuid}...`)
|
2022-01-27 01:37:54 +00:00
|
|
|
return {
|
|
|
|
busUuid: bus.uuid,
|
|
|
|
subscriberUuid: uuid,
|
|
|
|
subscription: await bus.subscribe(eventKey, (event: T) => {
|
|
|
|
if ( event.originBusUuid !== this.uuid ) {
|
|
|
|
return handler(event)
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
|
|
|
return {
|
|
|
|
unsubscribe: async () => {
|
|
|
|
this.subscribers = this.subscribers.where('uuid', '!=', uuid)
|
|
|
|
|
|
|
|
await this.subscriptions
|
|
|
|
.where('subscriberUuid', '=', uuid)
|
|
|
|
.tap(trashed => this.subscriptions.diffInPlace(trashed))
|
|
|
|
.pluck('subscription')
|
|
|
|
.awaitMapCall('unsubscribe')
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Connect an external event bus to this bus. */
|
|
|
|
async connect(bus: EventBus): Promise<void> {
|
2022-03-30 22:34:19 +00:00
|
|
|
if ( this.isUp && !bus.isConnected() ) {
|
2022-01-27 01:37:54 +00:00
|
|
|
await bus.up()
|
|
|
|
}
|
|
|
|
|
|
|
|
this.connectors.push(bus)
|
|
|
|
|
|
|
|
await this.subscribers
|
|
|
|
.promiseMap<BusInternalSubscription>(async subscriber => {
|
|
|
|
return {
|
|
|
|
busUuid: bus.uuid,
|
|
|
|
subscriberUuid: subscriber.uuid,
|
|
|
|
subscription: await bus.subscribe(subscriber.eventKey, event => {
|
|
|
|
if ( event.originBusUuid !== this.uuid ) {
|
|
|
|
return subscriber.handler(event)
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-03-30 22:34:19 +00:00
|
|
|
isConnected(): boolean {
|
|
|
|
return this.isUp
|
|
|
|
}
|
|
|
|
|
2022-01-27 01:37:54 +00:00
|
|
|
async disconnect(bus: EventBus): Promise<void> {
|
|
|
|
await this.subscriptions
|
|
|
|
.where('busUuid', '=', bus.uuid)
|
|
|
|
.tap(trashed => this.subscriptions.diffInPlace(trashed))
|
|
|
|
.pluck('subscription')
|
|
|
|
.awaitMapCall('unsubscribe')
|
|
|
|
|
|
|
|
if ( this.isUp ) {
|
|
|
|
await bus.down()
|
|
|
|
}
|
|
|
|
|
|
|
|
this.connectors.diffInPlace([bus])
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Initialize this event bus. */
|
2022-03-31 04:34:17 +00:00
|
|
|
async up(createUpstreamFromConfig = true): Promise<void> {
|
2022-01-27 01:37:54 +00:00
|
|
|
if ( this.isUp ) {
|
|
|
|
this.logging.warn('Attempted to boot more than once. Skipping.')
|
2022-03-30 22:24:45 +00:00
|
|
|
ifDebugging('extollo.bus', () => {
|
|
|
|
throw new Error('Attempted to boot more than once. Skipping.')
|
|
|
|
})
|
2022-01-27 01:37:54 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-27 16:34:01 +00:00
|
|
|
// Read the connectors from the server.bus config and set them up
|
2022-03-31 04:34:17 +00:00
|
|
|
if ( createUpstreamFromConfig ) {
|
|
|
|
const config = this.config.get('server.bus', {})
|
|
|
|
if ( Array.isArray(config.connectors) ) {
|
|
|
|
for ( const connector of (config.connectors as BusConnectorConfig[]) ) {
|
|
|
|
this.logging.verbose(`Creating bus connection of type: ${connector.type}`)
|
|
|
|
if ( connector.type === 'redis' ) {
|
|
|
|
await this.connect(this.make(RedisBus))
|
|
|
|
}
|
2022-01-27 16:34:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-30 22:34:19 +00:00
|
|
|
await this.connectors
|
|
|
|
.filter(bus => !bus.isConnected())
|
|
|
|
.awaitMapCall('up')
|
2022-01-27 01:37:54 +00:00
|
|
|
|
|
|
|
this.isUp = true
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Clean up this event bus. */
|
|
|
|
async down(): Promise<void> {
|
|
|
|
if ( !this.isUp ) {
|
|
|
|
this.logging.warn('Attempted to shut down but was never properly booted. Skipping.')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.subscriptions
|
|
|
|
.pluck('subscription')
|
|
|
|
.awaitMapCall('unsubscribe')
|
|
|
|
|
|
|
|
await this.connectors.awaitMapCall('down')
|
|
|
|
|
|
|
|
this.isUp = false
|
|
|
|
}
|
2022-03-31 04:04:00 +00:00
|
|
|
|
|
|
|
onContainerDestroy(): Awaitable<void> {
|
2022-03-31 04:40:10 +00:00
|
|
|
if ( this.isUp ) {
|
|
|
|
this.down()
|
|
|
|
}
|
2022-03-31 04:04:00 +00:00
|
|
|
}
|
2022-01-27 01:37:54 +00:00
|
|
|
}
|