timeline-state-resolver
Version:
Have timeline, control stuff
54 lines • 1.98 kB
JavaScript
;
var _WaitGroup_store, _WaitGroup_nextId;
Object.defineProperty(exports, "__esModule", { value: true });
exports.WaitGroup = void 0;
const tslib_1 = require("tslib");
/**
* A WaitGroup is used to wait for a number of operations to complete, or timeout
*/
class WaitGroup {
constructor() {
_WaitGroup_store.set(this, new Map());
_WaitGroup_nextId.set(this, 0
/**
* Resolve all waiting operations for a key, with success
*/
);
}
/**
* Resolve all waiting operations for a key, with success
*/
clearAllForKey(key) {
const callbacks = tslib_1.__classPrivateFieldGet(this, _WaitGroup_store, "f").get(key);
if (!callbacks)
return;
tslib_1.__classPrivateFieldGet(this, _WaitGroup_store, "f").delete(key);
for (const resolve of callbacks.values()) {
resolve(true);
}
}
/**
* Wait for a key to be resolved (true), or timeout (false)
*/
async waitOnKey(key, delay) {
var _a, _b;
let callbacks = tslib_1.__classPrivateFieldGet(this, _WaitGroup_store, "f").get(key);
if (!callbacks) {
callbacks = new Map();
tslib_1.__classPrivateFieldGet(this, _WaitGroup_store, "f").set(key, callbacks);
}
const callbacks2 = callbacks;
const id = (tslib_1.__classPrivateFieldSet(this, _WaitGroup_nextId, (_b = tslib_1.__classPrivateFieldGet(this, _WaitGroup_nextId, "f"), _a = _b++, _b), "f"), _a);
return new Promise((resolve) => {
const callbackWithCleanup = (value) => {
callbacks2.delete(id);
resolve(value);
};
callbacks2.set(id, callbackWithCleanup);
setTimeout(() => callbackWithCleanup(false), delay || 0);
});
}
}
exports.WaitGroup = WaitGroup;
_WaitGroup_store = new WeakMap(), _WaitGroup_nextId = new WeakMap();
//# sourceMappingURL=waitGroup.js.map