@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.
60 lines • 1.4 kB
JavaScript
/**
* @module Lock
*/
import { TimeSpan } from "../../../../utilities/_module-exports.js";
/**
* Keeps track of lock states in memory
*
* @internal
*/
export class LockState {
stateRecord;
key;
constructor(stateRecord, key) {
this.stateRecord = stateRecord;
this.key = key;
}
/**
* Return the expiration as a TimeSpan.
*/
get() {
const state = this.stateRecord[this.key];
if (state === undefined) {
return null;
}
if (state.expiration === null) {
return null;
}
return TimeSpan.fromDateRange(new Date(), state.expiration);
}
/**
* Sets the key expiration time.
*/
set(ttl) {
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();
}
/**
* Removes the key.
*/
remove() {
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete this.stateRecord[this.key];
}
}
//# sourceMappingURL=lock-state.js.map