UNPKG

@iotile/iotile-common

Version:

Common utilities for IoTile Packages and Applications

37 lines 1.03 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var Mutex = /** @class */ (function () { function Mutex() { this._queue = []; this._pending = false; } Mutex.prototype.acquire = function () { var that = this; var entry = new Promise(function (resolve, reject) { that._queue.push(resolve); }); if (!this._pending) { this.dispatch(); } return entry; }; Mutex.prototype.dispatch = function () { if (this._queue.length > 0) { var nextRunnable = this._queue.shift(); var that_1 = this; var releaseFunction = function () { that_1.dispatch(); }; this._pending = true; if (nextRunnable) { nextRunnable(releaseFunction); } } else { this._pending = false; } }; return Mutex; }()); exports.Mutex = Mutex; //# sourceMappingURL=mutex.js.map