@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 • 2.59 kB
JavaScript
/**
* @module Lock
*/
import { UnexpectedError } from "../../../../utilities/_module-exports.js";
import { LOCK_REFRESH_RESULT, } from "../../../../lock/contracts/_module-exports.js";
/**
* @internal
*/
export class DatabaseLockAdapter {
adapter;
constructor(adapter) {
this.adapter = adapter;
}
async acquire(key, owner, ttl) {
try {
const expiration = ttl?.toEndDate() ?? null;
// An error will be thrown if the lock already exists
await this.adapter.insert(key, owner, expiration);
return true;
}
catch (error) {
if (error instanceof UnexpectedError) {
throw error;
}
const expiration = ttl?.toEndDate() ?? null;
const result = await this.adapter.updateIfExpired(key, owner, expiration);
return result > 0;
}
}
async release(key, owner) {
const lockData = await this.adapter.removeIfOwner(key, owner);
if (lockData === null) {
return false;
}
const { expiration } = lockData;
const hasNoExpiration = expiration === null;
if (hasNoExpiration) {
return true;
}
const { owner: currentOwner } = lockData;
const isNotExpired = expiration > new Date();
const isCurrentOwner = owner === currentOwner;
return isNotExpired && isCurrentOwner;
}
async forceRelease(key) {
const lockData = await this.adapter.remove(key);
if (lockData === null) {
return false;
}
if (lockData.expiration === null) {
return true;
}
return lockData.expiration > new Date();
}
async refresh(key, owner, ttl) {
const lockData = await this.adapter.find(key);
if (lockData === null) {
return LOCK_REFRESH_RESULT.UNOWNED_REFRESH;
}
if (lockData.owner !== owner) {
return LOCK_REFRESH_RESULT.UNOWNED_REFRESH;
}
if (lockData.expiration === null) {
return LOCK_REFRESH_RESULT.UNEXPIRABLE_KEY;
}
if (lockData.expiration <= new Date()) {
return LOCK_REFRESH_RESULT.UNOWNED_REFRESH;
}
const expiration = ttl.toEndDate();
const result = await this.adapter.updateExpirationIfOwner(key, owner, expiration);
if (result > 0) {
return LOCK_REFRESH_RESULT.REFRESHED;
}
return LOCK_REFRESH_RESULT.UNOWNED_REFRESH;
}
}
//# sourceMappingURL=database-lock-adapter.js.map