Something went wrong. Try again.
A social knowledge tool for research trails, built on ATProto semble.so
research-tool social-network tools-for-thought atproto
Something went wrong. Try again.
1.1 kB · 40 lines
TypeScript
at development
1234567891011121314151617181920212223242526272829303132333435363738394041import { IEventSubscriber, IEventHandler,} from '../../application/events/IEventSubscriber';import { IDomainEvent } from '../../domain/events/IDomainEvent';import { InMemoryEventPublisher } from './InMemoryEventPublisher';import { EventName } from './EventConfig';
export class InMemoryEventSubscriber implements IEventSubscriber { private handlers: Map<EventName, IEventHandler<any>> = new Map(); private isStarted = false;
async subscribe<T extends IDomainEvent>( eventType: EventName, handler: IEventHandler<T>, ): Promise<void> { this.handlers.set(eventType, handler);
// Register with the static publisher InMemoryEventPublisher.addSubscriber( eventType, async (event: IDomainEvent) => { const result = await handler.handle(event as T); if (result.isErr()) { throw result.error; } }, ); }
async start(): Promise<void> { this.isStarted = true; console.log('InMemoryEventSubscriber started'); }
async stop(): Promise<void> { this.isStarted = false; console.log('InMemoryEventSubscriber stopped'); }}