UNPKG

molstar

Version:

A comprehensive macromolecular library.

51 lines (50 loc) 1.65 kB
/** * Copyright (c) 2019 mol* contributors, licensed under MIT, See LICENSE file for more info. * * @author David Sehnal <david.sehnal@gmail.com> */ import { arrayRemoveInPlace } from './array'; import { Subject } from 'rxjs'; var AsyncQueue = /** @class */ (function () { function AsyncQueue() { this.queue = []; this.signal = new Subject(); } Object.defineProperty(AsyncQueue.prototype, "length", { get: function () { return this.queue.length; }, enumerable: false, configurable: true }); AsyncQueue.prototype.enqueue = function (v) { this.queue.push(v); if (this.queue.length === 1) return true; return this.waitFor(v); }; AsyncQueue.prototype.handled = function (v) { arrayRemoveInPlace(this.queue, v); if (this.queue.length > 0) { this.signal.next({ v: this.queue[0], stillPresent: true }); } }; AsyncQueue.prototype.remove = function (v) { var rem = arrayRemoveInPlace(this.queue, v); if (rem) this.signal.next({ v: v, stillPresent: false }); return rem; }; AsyncQueue.prototype.waitFor = function (t) { var _this = this; return new Promise(function (res) { var sub = _this.signal.subscribe(function (_a) { var v = _a.v, removed = _a.stillPresent; if (v === t) { sub.unsubscribe(); res(removed); } }); }); }; return AsyncQueue; }()); export { AsyncQueue };