UNPKG

@shockpkg/core

Version:
194 lines (158 loc) 4.5 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.Lock = void 0; var _initializerDefineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/initializerDefineProperty")); var _applyDecoratedDescriptor2 = _interopRequireDefault(require("@babel/runtime/helpers/applyDecoratedDescriptor")); var _initializerWarningHelper2 = _interopRequireDefault(require("@babel/runtime/helpers/initializerWarningHelper")); var _properLockfile = _interopRequireDefault(require("proper-lockfile")); var _decorators = require("./decorators"); var _dispatcher = require("./dispatcher"); var _dec, _dec2, _dec3, _class, _descriptor, _descriptor2, _descriptor3, _temp; /** * Lock constructor. * * @param path The path to lock. */ let Lock = (_dec = (0, _decorators.property)(false), _dec2 = (0, _decorators.property)(false), _dec3 = (0, _decorators.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.Dispatcher(this); (0, _initializerDefineProperty2.default)(this, "_compromised", _descriptor, this); (0, _initializerDefineProperty2.default)(this, "_path", _descriptor2, this); (0, _initializerDefineProperty2.default)(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.default.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.default.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 = (0, _applyDecoratedDescriptor2.default)(_class.prototype, "_compromised", [_dec], { configurable: true, enumerable: true, writable: true, initializer: function () { return false; } }), _descriptor2 = (0, _applyDecoratedDescriptor2.default)(_class.prototype, "_path", [_dec2], { configurable: true, enumerable: true, writable: true, initializer: null }), _descriptor3 = (0, _applyDecoratedDescriptor2.default)(_class.prototype, "_release", [_dec3], { configurable: true, enumerable: true, writable: true, initializer: function () { return null; } })), _class)); exports.Lock = Lock; //# sourceMappingURL=lock.js.map