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.

190 lines (189 loc) 7.56 kB
/** * @module SharedLock */ import { type EventBusInput } from "../../../../event-bus/contracts/_module.js"; import { type IExecutionContext } from "../../../../execution-context/contracts/_module.js"; import { type INamespace } from "../../../../namespace/contracts/_module.js"; import { type ISerderRegister } from "../../../../serde/contracts/_module.js"; import { type ISharedLock, type SharedLockFactoryCreateSettings, type ISharedLockFactory, type SharedLockAdapterVariants, type ISharedLockListenable } from "../../../../shared-lock/contracts/_module.js"; import { type ITimeSpan } from "../../../../time-span/contracts/_module.js"; import { type Invokable, type OneOrMore, type WaitUntil } from "../../../../utilities/_module.js"; /** * Base configuration shared by all `SharedLockFactory` variants. * * IMPORT_PATH: `"@daiso-tech/core/shared-lock"` * @group Derivables */ export type SharedLockFactorySettingsBase = { /** * @default * ```ts * import { NoOpNamespace } from "@daiso-tech/core/namespace"; * * new NoOpNamespace() * ``` */ namespace?: INamespace; /** * You can pass an {@link ISerderRegister | `ISerderRegister`} instance to the {@link SharedLockFactory | `SharedLockFactory`} to register the shared lock's serialization and deserialization logic for the provided adapter. * @default * ```ts * import { Serde } from "@daiso-tech/core/serde"; * import { NoOpSerdeAdapter } from "@daiso-tech/core/serde/no-op-serde-adapter"; * * new Serde(new NoOpSerdeAdapter()) * ``` */ serde?: OneOrMore<ISerderRegister>; /** * The serde transformer name used to identify shared-lock serializer and deserializer adapters when there are adapters with the same name. * @default "" */ serdeTransformerName?: string; /** * You can pass your own lock id generator function. * @default * ```ts * import { v4 } from "uuid"; * * () => v4() */ createLockId?: Invokable<[], string>; /** * You can provide an {@link IEventBus | `IEventBus`} or an {@link IEventBusAdapter | `IEventBusAdapter`} instance to handle the component's events. * If you provide an adapter, it will be automatically wrapped in an {@link EventBus | `EventBus`} instance. * * @default * ```ts * import { NoOpEventBusAdapter } from "@daiso-tech/core/event-bus/no-op-event-bus-adapter"; * * new NoOpEventBusAdapter() * ``` */ eventBus?: EventBusInput; /** * 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`} `refresh` method. * ```ts * import { TimeSpan } from "@daiso-tech/core/time-span"; * * TimeSpan.fromMinutes(5); * ``` */ defaultRefreshTime?: ITimeSpan; /** * You can pass the `waitUntil` function to handle background promises. * This is required when working with environments like Cloudflare Workers or Vercel Functions to ensure tasks complete after the response is sent. * @default * ```ts * import { defaultWaitUntil } from "@daiso-tech/core/utilities" * ``` */ waitUntil?: WaitUntil; /** * You can pass {@link IExecutionContext | `IExecutionContext`} that will be used by context-aware adapters. * @default * ```ts * import { ExecutionContext } from "@daiso-tech/core/execution-context" * import { NoOpExecutionContextAdapter } from "@daiso-tech/core/execution-context/no-op-execution-context-adapter" * * new ExecutionContext(new NoOpExecutionContextAdapter()) * ``` */ executionContext?: IExecutionContext; }; /** * Configuration for `SharedLockFactory`. * Extends {@link SharedLockFactorySettingsBase | `SharedLockFactorySettingsBase`} with a required adapter. * * IMPORT_PATH: `"@daiso-tech/core/shared-lock"` * @group Derivables */ export type SharedLockFactorySettings = SharedLockFactorySettingsBase & { /** * The underlying shared-lock adapter that handles the actual locking operations. */ adapter: SharedLockAdapterVariants; }; /** * `SharedLockFactory` class can be derived from any {@link ISharedLockAdapter | `ISharedLockAdapter`} or {@link IDatabaseSharedLockAdapter | `IDatabaseSharedLockAdapter`}. * * Note the {@link ISharedLock | `ISharedLock`} instances created by the `SharedLockFactory` 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 SharedLockFactory implements ISharedLockFactory { private readonly eventBus; private readonly originalAdapter; private readonly adapter; private readonly namespace; private readonly creatLockId; private readonly defaultTtl; private readonly defaultRefreshTime; private readonly serde; private readonly serdeTransformerName; private readonly waitUntil; private readonly executionContext; private readonly use; /** * @example * ```ts * import { KyselySharedLockAdapter } from "@daiso-tech/core/shared-lock/kysely-shared-lock-adapter"; * import { SharedLockFactory } 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 lockFactory = new SharedLockFactory({ * serde, * adapter: sharedLockAdapter, * }); * ``` */ constructor(settings: SharedLockFactorySettings); private registerToSerde; get events(): ISharedLockListenable; /** * @example * ```ts * import { SharedLockFactory } 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 lockFactory = new SharedLockFactory({ * adapter: new MemorySharedLockAdapter(), * namespace: new Namespace("shared_lock"), * serde: new Serde(new SuperJsonSerdeAdapter()) * }); * * const sharedLock = lockFactory.create("a"); * ``` */ create(key: string, settings: SharedLockFactoryCreateSettings): ISharedLock; }