/** * @module SharedLock */ import { type ISharedLockState } from "../../shared-lock/contracts/_module.js"; import { type ITimeSpan } from "../../time-span/contracts/_module.js"; import { type TimeSpan } from "../../time-span/implementations/_module.js"; import { type AsyncLazy } from "../../utilities/_module.js"; /** * IMPORT_PATH: `"@daiso-tech/core/shared-lock/contracts"` * @group Contracts */ export type IReaderSemaphore = { /** * The `runReaderOrFail` method wraps an {@link Invocable | `Invocable`} with the `acquireOrFail` and `release` method. * @throws {LimitReachedReaderSemaphoreError} */ runReaderOrFail(asyncFn: AsyncLazy): Promise; /** * The `acquireReader` method acquires an slots only if the slot limit is not reached. * * @returns Returns true if the slot limit is not reached otherwise false is returned. */ acquireReader(): Promise; /** * The `acquireReaderOrFail` method acquires an slots only if the slot limit is not reached. * Throws an error if the slot limit is reached. * * @throws {LimitReachedReaderSemaphoreError} */ acquireReaderOrFail(): Promise; /** * The `releaseReader` method releases the current slot. * * @returns Returns true if the semaphore exists and has at least one busy slot or false. */ releaseReader(): Promise; /** * The `releaseReaderOrFail` method releases the current slot. * Throws an error if the slot is not acquired. * @throws {FailedReleaseReaderSemaphoreError} */ releaseReaderOrFail(): Promise; /** * The `forceReleaseAllReaders` method releases the all slots. * * @returns Returns true if the semaphore exists and has at least one unavailable slot or false if all slots are available. */ forceReleaseAllReaders(): Promise; /** * The `refreshReader` method updates the `ttl` of the slot when acquired. * * @returns Returns true if the slot is refreshed otherwise false is returned. */ refreshReader(ttl?: ITimeSpan): Promise; /** * The `refreshReaderOrFail` method updates the `ttl` of the slot when acquired. * Throws an error if the slot is not acquired. * @throws {FailedRefreshReaderSemaphoreError} */ refreshReaderOrFail(ttl?: ITimeSpan): Promise; }; /** * IMPORT_PATH: `"@daiso-tech/core/shared-lock/contracts"` * @group Contracts */ export type IWriterLock = { /** * The `runWriterOrFail` method wraps an {@link Invocable | `Invocable`} with the `acquireOrFail` and `release` method. * @throws {FailedAcquireWriterLockError} */ runWriterOrFail(asyncFn: AsyncLazy): Promise; /** * The `acquireWriter` method acquires a lock only if the key is not already acquired by different owner. * * @returns Returns true if the lock is not already acquired otherwise false is returned. */ acquireWriter(): Promise; /** * The `acquireWriterOrFail` method acquires a lock only if the key is not already acquired by different owner. * Throws an error if the lock is already acquired by different owner. * * @throws {FailedAcquireWriterLockError} */ acquireWriterOrFail(): Promise; /** * The `releaseWriter` method releases a lock if owned by the same owner. * * @returns Returns true if the lock is released otherwise false is returned. */ releaseWriter(): Promise; /** * The `releaseWriterOrFail` method releases a lock if owned by the same owner. * Throws an error if the lock is not owned by same owner. * * @throws {FailedReleaseWriterLockError} */ releaseWriterOrFail(): Promise; /** * The `forceReleaseWriter` method releases a lock regardless of the owner. * * @returns Returns true if the lock exists or false if the lock doesnt exists. */ forceReleaseWriter(): Promise; /** * The `refreshWriter` method updates the `ttl` of the lock if expireable and owned by the same owner. * * @returns Returns true if the lock is refreshed otherwise false is returned. */ refreshWriter(ttl?: ITimeSpan): Promise; /** * The `refreshWriterOrFail` method updates the `ttl` of the lock if expireable and owned by the same owner. * Throws an error if the lock is not owned by same owner. * Throws an error if the key is unexpirable. * * @throws {FailedRefreshWriterLockError} */ refreshWriterOrFail(ttl?: ITimeSpan): Promise; }; /** * IMPORT_PATH: `"@daiso-tech/core/shared-lock/contracts"` * @group Contracts */ export type ISharedLockStateMethods = { getState(): Promise; /** * The `key` of the `ISharedLock` instance. */ readonly key: string; /** * The `id` of the `ISharedLock` instance. */ readonly id: string; /** * The `ttl` of `ISharedLock` instance. */ readonly ttl: TimeSpan | null; }; /** * IMPORT_PATH: `"@daiso-tech/core/shared-lock/contracts"` * @group Contracts */ export type ISharedLockBase = IReaderSemaphore & IWriterLock & { forceRelease(): Promise; }; /** * IMPORT_PATH: `"@daiso-tech/core/shared-lock/contracts"` * @group Contracts */ export type ISharedLock = ISharedLockBase & ISharedLockStateMethods;