@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.
77 lines (76 loc) • 3.17 kB
TypeScript
/**
* @module Lock
*/
import type { TimeSpan } from "../../../../utilities/_module-exports.js";
import { type Key, type AsyncLazy, type OneOrMore, type Result } from "../../../../utilities/_module-exports.js";
import { KeyAlreadyAcquiredLockError, type AquireBlockingSettings, type LockEventMap } from "../../../../lock/contracts/_module-exports.js";
import { type ILock, type ILockAdapter } from "../../../../lock/contracts/_module-exports.js";
import { LazyPromise } from "../../../../async/_module-exports.js";
import type { IEventDispatcher } from "../../../../event-bus/contracts/_module-exports.js";
import type { LockState } from "../../../../lock/implementations/derivables/lock-provider/lock-state.js";
/**
* @internal
*/
export type ISerializedLock = {
key: OneOrMore<string>;
owner: string;
ttlInMs: number | null;
expirationInMs: number | null;
};
/**
* @internal
*/
export type LockSettings = {
createLazyPromise: <TValue = void>(asyncFn: () => PromiseLike<TValue>) => LazyPromise<TValue>;
adapter: ILockAdapter;
lockState: LockState;
eventDispatcher: IEventDispatcher<LockEventMap>;
key: Key;
owner: OneOrMore<string>;
ttl: TimeSpan | null;
expirationInMs: number | null;
defaultBlockingInterval: TimeSpan;
defaultBlockingTime: TimeSpan;
defaultRefreshTime: TimeSpan;
};
/**
* IMPORTANT: This class is not intended to be instantiated directly, instead it should be created by the `LockProvider` class instance.
* @group Derivables
*/
export declare class Lock implements ILock {
/**
* @internal
*/
static serialize(deserializedValue: Lock): ISerializedLock;
private readonly createLazyPromise;
private readonly adapter;
private readonly lockState;
private readonly eventDispatcher;
private readonly key;
private readonly owner;
private readonly ttl;
private readonly defaultBlockingInterval;
private readonly defaultBlockingTime;
private readonly defaultRefreshTime;
/**
* @internal
*/
constructor(settings: LockSettings);
run<TValue = void>(asyncFn: AsyncLazy<TValue>): LazyPromise<Result<TValue, KeyAlreadyAcquiredLockError>>;
runOrFail<TValue = void>(asyncFn: AsyncLazy<TValue>): LazyPromise<TValue>;
runBlocking<TValue = void>(asyncFn: AsyncLazy<TValue>, settings?: AquireBlockingSettings): LazyPromise<Result<TValue, KeyAlreadyAcquiredLockError>>;
runBlockingOrFail<TValue = void>(asyncFn: AsyncLazy<TValue>, settings?: AquireBlockingSettings): LazyPromise<TValue>;
acquire(): LazyPromise<boolean>;
acquireOrFail(): LazyPromise<void>;
acquireBlocking(settings?: AquireBlockingSettings): LazyPromise<boolean>;
acquireBlockingOrFail(settings?: AquireBlockingSettings): LazyPromise<void>;
release(): LazyPromise<boolean>;
releaseOrFail(): LazyPromise<void>;
forceRelease(): LazyPromise<void>;
isExpired(): LazyPromise<boolean>;
isLocked(): LazyPromise<boolean>;
refresh(ttl?: TimeSpan): LazyPromise<boolean>;
refreshOrFail(ttl?: TimeSpan): LazyPromise<void>;
getRemainingTime(): LazyPromise<TimeSpan | null>;
getOwner(): LazyPromise<string>;
}