@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.
118 lines • 3.49 kB
JavaScript
/**
* @module Lock
*/
import { LOCK_REFRESH_RESULT, } from "../../../../lock/contracts/_module-exports.js";
/**
* Note the `MemoryLockAdapter` is limited to single process usage and cannot be shared across multiple servers or different processes.
* This adapter is meant to be used for testing.
*
* IMPORT_PATH: `"@daiso-tech/core/lock/adapters"`
* @group Adapters
*/
export class MemoryLockAdapter {
map;
/**
* @example
* ```ts
* import { MemoryLockAdapter } from "@daiso-tech/core/lock/adapters";
*
* const lockAdapter = new MemoryLockAdapter();
* ```
* You can also provide an `Map`.
* @example
* ```ts
* import { MemoryLockAdapter } from "@daiso-tech/core/lock/adapters";
*
* const map = new Map<any, any>();
* const lockAdapter = new MemoryLockAdapter(map);
* ```
*/
constructor(map = new Map()) {
this.map = map;
}
// eslint-disable-next-line @typescript-eslint/require-await
async deInit() {
for (const [key, lockData] of this.map) {
if (lockData.hasExpiration) {
clearTimeout(lockData.timeoutId);
}
this.map.delete(key);
}
}
// eslint-disable-next-line @typescript-eslint/require-await
async acquire(key, owner, ttl) {
let lock = this.map.get(key);
if (lock !== undefined) {
return false;
}
if (ttl === null) {
lock = {
owner,
hasExpiration: false,
};
this.map.set(key, lock);
}
else {
const timeoutId = setTimeout(() => {
this.map.delete(key);
}, ttl.toMilliseconds());
lock = {
owner,
hasExpiration: true,
timeoutId,
};
this.map.set(key, lock);
}
return true;
}
// eslint-disable-next-line @typescript-eslint/require-await
async release(key, owner) {
const lock = this.map.get(key);
if (lock === undefined) {
return false;
}
if (lock.owner !== owner) {
return false;
}
if (lock.hasExpiration) {
clearTimeout(lock.timeoutId);
}
this.map.delete(key);
return true;
}
// eslint-disable-next-line @typescript-eslint/require-await
async forceRelease(key) {
const lock = this.map.get(key);
if (lock === undefined) {
return false;
}
if (lock.hasExpiration) {
clearTimeout(lock.timeoutId);
}
this.map.delete(key);
return true;
}
// eslint-disable-next-line @typescript-eslint/require-await
async refresh(key, owner, ttl) {
const lock = this.map.get(key);
if (lock === undefined) {
return LOCK_REFRESH_RESULT.UNOWNED_REFRESH;
}
if (lock.owner !== owner) {
return LOCK_REFRESH_RESULT.UNOWNED_REFRESH;
}
if (!lock.hasExpiration) {
return LOCK_REFRESH_RESULT.UNEXPIRABLE_KEY;
}
clearTimeout(lock.timeoutId);
const timeoutId = setTimeout(() => {
this.map.delete(key);
}, ttl.toMilliseconds());
this.map.set(key, {
...lock,
timeoutId,
});
return LOCK_REFRESH_RESULT.REFRESHED;
}
}
//# sourceMappingURL=memory-lock-adapter.js.map