diff --git a/src/event-bus/event-bus.ts b/src/event-bus/event-bus.ts new file mode 100644 index 0000000000000000000000000000000000000000..ad144af2a0342779a756e654e9945a9472caf9c2 --- /dev/null +++ b/src/event-bus/event-bus.ts @@ -0,0 +1,13 @@ +import { EventType } from './types'; +import { NotImplementedError } from 'funfix'; + +export abstract class EventBus { + // register the following: + // - eventsToHandle - a list of events you will publish/subscribe to + // - serviceName - used when subscribing to generate a unique queue for holding + // incoming messages of the form: `consumer__${eventType}__${serviceName}` + constructor(readonly eventsToHandle: EventType[], readonly serviceName: string) {} + destroy(): Promise<void> { + throw new NotImplementedError('destroy() on EventBus'); + } +} diff --git a/src/event-bus/types.ts b/src/event-bus/types.ts new file mode 100644 index 0000000000000000000000000000000000000000..0bb8497590208fa0b91196a9f02710e4c3657867 --- /dev/null +++ b/src/event-bus/types.ts @@ -0,0 +1,32 @@ +// Abstract Message Queue - types and interfaces + +/** + * TODO: Once agreed, add these to DefinitelyTyped so they can be shared. + */ + +export type EventType = string; + +export interface Event<T extends object> { + readonly eventType: EventType; + readonly id: string; // Generated when the event is emitted + readonly created: Date; + readonly payload: T; // The actual data the event is carrying. + // version: has been removed - so we can remain weakly typed + // context: has also been removed - if you need information about the origin + // source of the event then put it in the payload. +} + +export interface EventPublisher { + // Promise<boolean> should this become void | exception? we only need to know if something went wrong + publish<T extends object>(event: Event<T>): Promise<boolean>; +} + +export interface EventSubscriber { + // handler: returns whether or not we should ack the message + subscribe<T extends object>(eventType: string, handler: (event: Event<T>) => Promise<boolean>): void; +} + +// This isn't generic enough +export interface EventConfig { + url: string; +}