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.

207 lines (206 loc) 9.2 kB
/** * @module SharedLock */ import { type EventListener, type IEventBus, type Unsubscribe } from "../../../../event-bus/contracts/_module.js"; import { Namespace } from "../../../../namespace/_module.js"; import { type ISerderRegister } from "../../../../serde/contracts/_module.js"; import { type ISharedLock, type SharedLockEventMap, type SharedLockProviderCreateSettings, type ISharedLockProvider, type SharedLockAdapterVariants } from "../../../../shared-lock/contracts/_module.js"; import { type ITask } from "../../../../task/contracts/_module.js"; import { type ITimeSpan } from "../../../../time-span/contracts/_module.js"; import { type Invokable, type OneOrMore } from "../../../../utilities/_module.js"; /** * * IMPORT_PATH: `"@daiso-tech/core/shared-lock"` * @group Derivables */ export type SharedLockProviderSettingsBase = { /** * @default * ```ts * import { Namespace } from "@daiso-tech/core/namespace"; * * new Namespace("@shared-lock") * ``` */ namespace?: Namespace; serde?: OneOrMore<ISerderRegister>; /** * @default "" */ serdeTransformerName?: string; /** * You can pass your lock id id generator function. * @default * ```ts * import { v4 } from "uuid"; * * () => v4 */ createLockId?: Invokable<[], string>; /** * @default * ```ts * import { EventBus } from "@daiso-tech/core/event-bus"; * import { NoOpEventBusAdapter } from "@daiso-tech/core/event-bus/no-op-event-bus-adapter"; * * new EventBus({ * adapter: new NoOpEventBusAdapter() * }) * ``` */ eventBus?: IEventBus; /** * You can decide the default ttl value for {@link ISharedLock | `ISharedLock`} expiration. If null is passed then no ttl will be used by default. * @default * ```ts * import { TimeSpan } from "@daiso-tech/core/time-span"; * * TimeSpan.fromMinutes(5); * ``` */ defaultTtl?: ITimeSpan | null; /** * The default refresh time used in the {@link ISharedLock | `ISharedLock`} `acquireBlocking` and `runBlocking` methods. * @default * ```ts * import { TimeSpan } from "@daiso-tech/core/time-span"; * * TimeSpan.fromSeconds(1); * ``` */ defaultBlockingInterval?: ITimeSpan; /** * The default refresh time used in the {@link ISharedLock | `ISharedLock`} `acquireBlocking` and `runBlocking` methods. * @default * ```ts * import { TimeSpan } from "@daiso-tech/core/time-span"; * * TimeSpan.fromMinutes(1); * ``` */ defaultBlockingTime?: ITimeSpan; /** * The default refresh time used in the {@link ISharedLock | `ISharedLock`} `referesh` method. * ```ts * import { TimeSpan } from "@daiso-tech/core/time-span"; * * TimeSpan.fromMinutes(5); * ``` */ defaultRefreshTime?: ITimeSpan; }; /** * * IMPORT_PATH: `"@daiso-tech/core/shared-lock"` * @group Derivables */ export type SharedLockProviderSettings = SharedLockProviderSettingsBase & { adapter: SharedLockAdapterVariants; }; /** * * IMPORT_PATH: `"@daiso-tech/core/shared-lock"` * @group Derivables */ export declare const DEFAULT_SHARED_LOCK_NAMESPACE: Namespace; /** * `SharedLockProvider` class can be derived from any {@link ISharedLockAdapter | `ISharedLockAdapter`} or {@link IDatabaseSharedLockAdapter | `IDatabaseSharedLockAdapter`}. * * Note the {@link ISharedLock | `ISharedLock`} instances created by the `SharedLockProvider` class are serializable and deserializable, * allowing them to be seamlessly transferred across different servers, processes, and databases. * This can be done directly using {@link ISerderRegister | `ISerderRegister`} or indirectly through components that rely on {@link ISerderRegister | `ISerderRegister`} internally. * * IMPORT_PATH: `"@daiso-tech/core/shared-lock"` * @group Derivables */ export declare class SharedLockProvider implements ISharedLockProvider { private readonly eventBus; private readonly originalAdapter; private readonly adapter; private readonly namespace; private readonly creatLockId; private readonly defaultTtl; private readonly defaultBlockingInterval; private readonly defaultBlockingTime; private readonly defaultRefreshTime; private readonly serde; private readonly serdeTransformerName; /** * @example * ```ts * import { KyselySharedLockAdapter } from "@daiso-tech/core/shared-lock/kysely-shared-lock-adapter"; * import { SharedLockProvider } from "@daiso-tech/core/shared-lock"; * import { Serde } from "@daiso-tech/core/serde"; * import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/super-json-serde-adapter"; * import Sqlite from "better-sqlite3"; * import { Kysely, SqliteDialect } from "kysely"; * * const sharedLockAdapter = new KyselySharedLockAdapter({ * kysely: new Kysely({ * dialect: new SqliteDialect({ * database: new Sqlite("local.db"), * }), * }); * }); * // You need initialize the adapter once before using it. * await sharedLockAdapter.init(); * * const serde = new Serde(new SuperJsonSerdeAdapter()) * const lockProvider = new SharedLockProvider({ * serde, * adapter: sharedLockAdapter, * }); * ``` */ constructor(settings: SharedLockProviderSettings); private registerToSerde; /** * You can listen to the following {@link SharedLockEventMap | `SharedLockEventMap`} of all {@link ISharedLock | `ISharedLock`} instances created by the {@link ISharedLockProvider | `ISharedLockProvider`}. * To understand how this method works, refer to {@link IEventListenable | `IEventListenable `}. */ addListener<TEventName extends keyof SharedLockEventMap>(eventName: TEventName, listener: EventListener<SharedLockEventMap[TEventName]>): ITask<void>; /** * You can listen to the following {@link SharedLockEventMap | `SharedLockEventMap`} of all {@link ISharedLock | `ISharedLock`} instances created by the {@link ISharedLockProvider | `ISharedLockProvider`}. * To understand how this method works, refer to {@link IEventListenable | `IEventListenable `}. */ removeListener<TEventName extends keyof SharedLockEventMap>(eventName: TEventName, listener: EventListener<SharedLockEventMap[TEventName]>): ITask<void>; /** * You can listen to the following {@link SharedLockEventMap | `SharedLockEventMap`} of all {@link ISharedLock | `ISharedLock`} instances created by the {@link ISharedLockProvider | `ISharedLockProvider`}. * To understand how this method works, refer to {@link IEventListenable | `IEventListenable `}. */ listenOnce<TEventName extends keyof SharedLockEventMap>(eventName: TEventName, listener: EventListener<SharedLockEventMap[TEventName]>): ITask<void>; /** * You can listen to the following {@link SharedLockEventMap | `SharedLockEventMap`} of all {@link ISharedLock | `ISharedLock`} instances created by the {@link ISharedLockProvider | `ISharedLockProvider`}. * To understand how this method works, refer to {@link IEventListenable | `IEventListenable `}. */ asTask<TEventName extends keyof SharedLockEventMap>(eventName: TEventName): ITask<SharedLockEventMap[TEventName]>; /** * You can listen to the following {@link SharedLockEventMap | `SharedLockEventMap`} of all {@link ISharedLock | `ISharedLock`} instances created by the {@link ISharedLockProvider | `ISharedLockProvider`}. * To understand how this method works, refer to {@link IEventListenable | `IEventListenable `}. */ subscribeOnce<TEventName extends keyof SharedLockEventMap>(eventName: TEventName, listener: EventListener<SharedLockEventMap[TEventName]>): ITask<Unsubscribe>; /** * You can listen to the following {@link SharedLockEventMap | `SharedLockEventMap`} of all {@link ISharedLock | `ISharedLock`} instances created by the {@link ISharedLockProvider | `ISharedLockProvider`}. * To understand how this method works, refer to {@link IEventListenable | `IEventListenable `}. */ subscribe<TEventName extends keyof SharedLockEventMap>(eventName: TEventName, listener: EventListener<SharedLockEventMap[TEventName]>): ITask<Unsubscribe>; /** * @example * ```ts * import { SharedLockProvider } from "@daiso-tech/core/shared-lock"; * import { MemorySharedLockAdapter } from "@daiso-tech/core/shared-lock/memory-shared-lock-adapter"; * import { Namespace } from "@daiso-tech/core/namespace"; * import { Serde } from "@daiso-tech/core/serde"; * import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/super-json-serde-adapter"; * * const lockProvider = new SharedLockProvider({ * adapter: new MemorySharedLockAdapter(), * namespace: new Namespace("shared_lock"), * serde: new Serde(new SuperJsonSerdeAdapter()) * }); * * const sharedLock = lockProvider.create("a"); * ``` */ create(key: string, settings: SharedLockProviderCreateSettings): ISharedLock; }