UNPKG

uc-base-libraries

Version:
103 lines 4.23 kB
System.register([], function (exports_1, context_1) { "use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var MutexLock; var __moduleName = context_1 && context_1.id; function WhenTrueAsync(func, maxLockTime = 60000) { return __awaiter(this, void 0, void 0, function* () { const p = new Promise((resolve, reject) => { if (func) { const t = func(); if (t) { resolve(); return; } } const startTime = new Date(); const check = () => { if (func) { const t = func(); if (t) { resolve(); return; } } const seconds = (new Date().getTime() - startTime.getTime()) / 1000; if (seconds >= maxLockTime) { reject('Max Wait Time for lock hit'); return; } setTimeout(check, 100); }; setTimeout(check, 100); }); return p; }); } exports_1("WhenTrueAsync", WhenTrueAsync); return { setters: [], execute: function () { MutexLock = class MutexLock { constructor(maxLockTime) { this.maxLockTime = maxLockTime; this.locked = false; this.lastCalled = null; } get isLocked() { let seconds = 0; if (this.lastCalled) { seconds = (new Date().getTime() - this.lastCalled.getTime()) / 1000; } return this.locked && seconds < this.maxLockTime; } WhenTrueAsync(func) { return __awaiter(this, void 0, void 0, function* () { return WhenTrueAsync(func, this.maxLockTime); }); } WaitTillUnlocked() { return __awaiter(this, void 0, void 0, function* () { yield this.WhenTrueAsync(() => { return !this.isLocked; }); return; }); } LockAreaAsync(func) { return __awaiter(this, void 0, void 0, function* () { yield this.BeginLock(); yield func(); yield this.EndLock(); }); } BeginLock() { return __awaiter(this, void 0, void 0, function* () { yield this.WaitTillUnlocked(); if (this.isLocked) { this.lastCalled = new Date(); return; } this.locked = true; this.lastCalled = new Date(); }); } EndLock() { return __awaiter(this, void 0, void 0, function* () { this.locked = false; }); } }; exports_1("MutexLock", MutexLock); } }; }); //# sourceMappingURL=Lock.js.map