@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.
44 lines • 1.25 kB
JavaScript
/**
* @module Lock
*/
import { UnexpectedLockError } from "../../../../lock/contracts/_module-exports.js";
/**
* @internal
*/
export class DatabaseLockAdapter {
adapter;
constructor(adapter) {
this.adapter = adapter;
}
async acquire(key, owner, ttl) {
const expiration = ttl?.toEndDate() ?? null;
try {
await this.adapter.insert(key, owner, expiration);
return true;
}
catch (error) {
if (error instanceof UnexpectedLockError) {
throw error;
}
const result = await this.adapter.update(key, owner, expiration);
return result > 0;
}
}
async release(key, owner) {
const lock = await this.adapter.find(key);
if (lock === null) {
return true;
}
await this.adapter.remove(key, owner);
const isOwner = lock.owner === owner;
return isOwner;
}
async forceRelease(key) {
await this.adapter.remove(key, null);
}
async refresh(key, owner, ttl) {
const result = await this.adapter.refresh(key, owner, ttl.toEndDate());
return result > 0;
}
}
//# sourceMappingURL=database-lock-adapter.js.map