UNPKG

@shockpkg/core

Version:
184 lines (151 loc) 4.04 kB
import _initializerDefineProperty from "@babel/runtime/helpers/initializerDefineProperty.js"; import _applyDecoratedDescriptor from "@babel/runtime/helpers/applyDecoratedDescriptor.js"; import _initializerWarningHelper from "@babel/runtime/helpers/initializerWarningHelper.js"; var _dec, _dec2, _dec3, _class, _descriptor, _descriptor2, _descriptor3, _temp; import properLockfile from 'proper-lockfile'; import { property } from "./decorators.mjs"; import { Dispatcher } from "./dispatcher.mjs"; /** * Lock constructor. * * @param path The path to lock. */ export let Lock = (_dec = property(false), _dec2 = property(false), _dec3 = property(false), (_class = (_temp = class Lock extends Object { /** * Duration at which the lock is considered stale in milliseconds. * Minimum value of 2000. */ /** * Update interval in milliseconds. */ /** * The number of retries to attempt to aquire the lock. */ /** * Resolve symlinks using realpath. */ /** * Compromised lock events. */ // eslint-disable-next-line no-invalid-this /** * Lock has been compromised since aquire. */ /** * The path to lock. */ /** * The lock release function. */ constructor(path) { super(); this.stale = 10000; this.update = 5000; this.retries = 0; this.realpath = false; this.eventCompromised = new Dispatcher(this); _initializerDefineProperty(this, "_compromised", _descriptor, this); _initializerDefineProperty(this, "_path", _descriptor2, this); _initializerDefineProperty(this, "_release", _descriptor3, this); this._path = path; } /** * The path to lock. * * @returns The path. */ get path() { return this._path; } /** * Boolean for if lock is held. * The lock could be compromised and not yet detected however. * * @returns Is held. */ get held() { return !!this._release; } /** * Boolean for if the lock hase been compromised since aquire. * The lock could be compromised and not yet detected however. * * @returns Is compromised. */ get compromised() { return this._compromised; } /** * Check if path is already locked by any instance including this one. * Does not verify the lock file belongs to this instance. * * @returns True if locked, false if not. */ async check() { // Will throw if not exist when using realpath, so catch. let r; try { r = await properLockfile.check(this.path); } catch (err) { if (err.code === 'ENOENT') { r = false; } else { throw err; } } if (!r) { this._release = null; } return r; } /** * Aquire lock or fail. */ async aquire() { if (this._release) { throw new Error('Lock already aquired'); } this._compromised = false; this._release = await properLockfile.lock(this.path, { stale: this.stale, update: this.update, retries: this.retries, realpath: this.realpath, onCompromised: async err => { this._compromised = true; this._release = null; await this.eventCompromised.triggerOrThrowAsync(err); } }); } /** * Release lock or fail. */ async release() { if (!this._release) { throw new Error('Lock not aquired'); } await this._release(); this._release = null; } }, _temp), (_descriptor = _applyDecoratedDescriptor(_class.prototype, "_compromised", [_dec], { configurable: true, enumerable: true, writable: true, initializer: function () { return false; } }), _descriptor2 = _applyDecoratedDescriptor(_class.prototype, "_path", [_dec2], { configurable: true, enumerable: true, writable: true, initializer: null }), _descriptor3 = _applyDecoratedDescriptor(_class.prototype, "_release", [_dec3], { configurable: true, enumerable: true, writable: true, initializer: function () { return null; } })), _class)); //# sourceMappingURL=lock.mjs.map