UNPKG

node-json-db

Version:

Database using JSON file as storage for Node.JS

63 lines 1.91 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.writeLockAsync = exports.readLockAsync = void 0; const ReadWriteLock_1 = require("./ReadWriteLock"); const Error_1 = require("./Error"); const lock = new ReadWriteLock_1.ReadWriteLock(); /** * take a read lock that will be released when the function has finished running * @param func * @param timeout time in ms to wait to get the lock. Null mean infinite. */ const readLockAsync = (func, timeout = null) => { return new Promise(async (resolve, reject) => { try { const release = await lock.readLock(timeout ?? undefined); try { const result = await func(); resolve(result); } finally { release(); } } catch (error) { if (error instanceof Error_1.TimeoutError) { reject(error); } else { reject(error); } } }); }; exports.readLockAsync = readLockAsync; /** * Take a write lock that will be released when the function has finished running * @param func * @param timeout time in ms to wait to get the lock. Null mean infinite. */ const writeLockAsync = (func, timeout = null) => { return new Promise(async (resolve, reject) => { try { const release = await lock.writeLock(timeout ?? undefined); try { const result = await func(); resolve(result); } finally { release(); } } catch (error) { if (error instanceof Error_1.TimeoutError) { reject(error); } else { reject(error); } } }); }; exports.writeLockAsync = writeLockAsync; //# sourceMappingURL=Lock.js.map