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.

77 lines 1.9 kB
/** * @module Lock */ import { TimeSpan } from "../../../../utilities/_module-exports.js"; /** * @internal */ export class LockState { stateRecord; key; constructor(stateRecord, key) { this.stateRecord = stateRecord; this.key = key; } /** * Return the expiration as a date. */ get() { const state = this.stateRecord[this.key]; if (state === undefined) { return null; } const { expiration } = state; return expiration; } /** * Sets the expiration time. * If a number is provided, it must be in milliseconds. */ set(ttl) { if (typeof ttl === "number") { this.stateRecord[this.key] = { expiration: new Date(ttl), }; return; } this.stateRecord[this.key] = { expiration: ttl?.toEndDate() ?? null, }; } /** * Checks if the key is expired. */ isExpired() { const state = this.stateRecord[this.key]; if (state === undefined) { return true; } const { expiration } = state; if (expiration === null) { return false; } return expiration.getTime() < Date.now(); } /** * Returns the remaining time. */ getRemainingTime() { const state = this.stateRecord[this.key]; if (state === undefined) { return null; } const { expiration } = state; if (expiration === null) { return null; } return TimeSpan.fromDateRange(new Date(), expiration); } /** * Removes the expiration from the record. */ remove() { // eslint-disable-next-line @typescript-eslint/no-dynamic-delete delete this.stateRecord[this.key]; } } //# sourceMappingURL=lock-state.js.map