UNPKG

@daiso-tech/core

Version:

The library offers flexible, framework-agnostic solutions for modern web applications, built on adaptable components that integrate seamlessly with popular frameworks like Next Js.

146 lines 5.29 kB
/** * @module EventBus */ import {} from "../../../../event-bus/contracts/_module-exports.js"; import { EventBus, } from "../../../../event-bus/implementations/derivables/event-bus/_module.js"; import { DefaultAdapterNotDefinedError, UnregisteredAdapterError, } from "../../../../utilities/_module-exports.js"; /** * The `EventBusFactory` class is immutable. * * IMPORT_PATH: `"@daiso-tech/core/event-bus"` * @group Derivables */ export class EventBusFactory { settings; /** * @example * ```ts * import { type IEventBusAdapter, BaseEvent } from "@daiso-tech/core/event-bus/contracts"; * import { EventBusFactory } from "@daiso-tech/core/event-bus"; * import { MemoryEventBusAdapter, RedisPubSubEventBusAdapter } from "@daiso-tech/core/event-bus/adapters"; * import { KeyPrefixer, type FactoryFn } from "@daiso-tech/core/utilities"; * import { Serde } from "@daiso-tech/core/serde"; * import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/adapters" * import Redis from "ioredis"; * * type Store = Partial<Record<string, IEventBusAdapter>>; * * function cahceAdapterFactory(store: Store): FactoryFn<string, IEventBusAdapter> { * return (prefix) => { * let adapter = store[prefix]; * if (adapter === undefined) { * adapter = new MemoryEventBusAdapter(); * store[prefix] = adapter; * } * return adapter; * } * } * * const serde = new Serde(new SuperJsonSerdeAdapter()); * const store: Store = {}; * const eventBusFactory = new EventBusFactory({ * keyPrefixer: new KeyPrefixer("event-bus"), * adapters: { * memory: new MemoryEventBusAdapter(), * memoryFactory: cahceAdapterFactory(store), * redis: new RedisPubSubEventBusAdapter({ * serde, * dispatcherClient: new Redis("YOUR_REDIS_CONNECTION_STRING"), * listenerClient: new Redis("YOUR_REDIS_CONNECTION_STRING"), * }), * }, * defaultAdapter: "memory" * }); * ``` */ constructor(settings) { this.settings = settings; } setKeyPrefixer(keyPrefixer) { return new EventBusFactory({ ...this.settings, keyPrefixer, }); } setLazyPromiseFactory(factory) { return new EventBusFactory({ ...this.settings, lazyPromiseFactory: factory, }); } /** * @example * ```ts * import { type IEventBusAdapter, BaseEvent } from "@daiso-tech/core/event-bus/contracts"; * import { EventBusFactory } from "@daiso-tech/core/event-bus"; * import { MemoryEventBusAdapter, RedisPubSubEventBusAdapter } from "@daiso-tech/core/event-bus/adapters"; * import { KeyPrefixer, type FactoryFn } from "@daiso-tech/core/utilities"; * import { Serde } from "@daiso-tech/core/serde"; * import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/adapters" * import Redis from "ioredis"; * * type Store = Partial<Record<string, IEventBusAdapter>>; * * function cahceAdapterFactory(store: Store): FactoryFn<string, IEventBusAdapter> { * return (prefix) => { * let adapter = store[prefix]; * if (adapter === undefined) { * adapter = new MemoryEventBusAdapter(); * store[prefix] = adapter; * } * return adapter; * } * } * * const dispatcherClient = new Redis("YOUR_REDIS_CONNECTION_STRING"); * const listenerClient = new Redis("YOUR_REDIS_CONNECTION_STRING"); * const serde = new Serde(new SuperJsonSerdeAdapter()); * const store: Store = {}; * const eventBusFactory = new EventBusFactory({ * keyPrefixer: new KeyPrefixer("event-bus"), * adapters: { * memory: new MemoryEventBusAdapter(), * memoryFactory: cahceAdapterFactory(store), * redis: new RedisPubSubEventBusAdapter({ * serde, * dispatcherClient: new Redis("YOUR_REDIS_CONNECTION_STRING"), * listenerClient: new Redis("YOUR_REDIS_CONNECTION_STRING"), * }), * }, * defaultAdapter: "memory" * }); * * type AddEvent = { * a: number; * b: number; * }; * type EventMap = { * add: AddEvent; * }; * * // Will dispatch AddEvent using the default adapter which is MemoryEventBusAdapter * await eventBusFactory * .use<EventMap>() * .dispatch("add", { a: 1, b: 2 }); * * // Will dispatch AddEvent using the redis adapter which is RedisPubSubEventBusAdapter * await eventBusFactory * .use<EventMap>("redis") * .dispatch("add", { a: 1, b: 2 }); * ``` */ use(adapterName = this.settings.defaultAdapter) { if (adapterName === undefined) { throw new DefaultAdapterNotDefinedError(EventBusFactory.name); } const adapter = this.settings.adapters[adapterName]; if (adapter === undefined) { throw new UnregisteredAdapterError(adapterName); } return new EventBus({ adapter, ...this.settings, }); } } //# sourceMappingURL=event-bus-factory.js.map