@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.
138 lines (137 loc) • 5.47 kB
TypeScript
/**
* @module EventBus
*/
import type { LazyPromise } from "../../../../async/_module-exports.js";
import { type IEventBus, type IEventBusFactory, type BaseEventMap, type IEventBusAdapter } from "../../../../event-bus/contracts/_module-exports.js";
import { type EventBusSettingsBase } from "../../../../event-bus/implementations/derivables/event-bus/_module.js";
import type { AsyncLazy, Factory, KeyPrefixer } from "../../../../utilities/_module-exports.js";
/**
*
* IMPORT_PATH: `"@daiso-tech/core/event-bus"`
* @group Derivables
*/
export type EventBusAdapters<TAdapters extends string = string> = Partial<Record<TAdapters, IEventBusAdapter>>;
/**
*
* IMPORT_PATH: `"@daiso-tech/core/event-bus"`
* @group Derivables
*/
export type EventBusFactorySettings<TAdapters extends string = string> = EventBusSettingsBase & {
adapters: EventBusAdapters<TAdapters>;
defaultAdapter?: NoInfer<TAdapters>;
};
/**
* The `EventBusFactory` class is immutable.
*
* IMPORT_PATH: `"@daiso-tech/core/event-bus"`
* @group Derivables
*/
export declare class EventBusFactory<TAdapters extends string = string> implements IEventBusFactory<TAdapters> {
private readonly 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: EventBusFactorySettings<TAdapters>);
setKeyPrefixer(keyPrefixer: KeyPrefixer): EventBusFactory<TAdapters>;
setLazyPromiseFactory(factory: Factory<AsyncLazy<any>, LazyPromise<any>>): EventBusFactory<TAdapters>;
/**
* @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<TEventMap extends BaseEventMap>(adapterName?: TAdapters | undefined): IEventBus<TEventMap>;
}