123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- "use strict";
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.KeyPromiseQueue = void 0;
- class KeyPromiseQueue {
- constructor() {
- this.queue = {};
- }
- enqueue(key, operation) {
- const tuple = this.beforeOp(key);
- const toAwait = tuple[1];
- const nextOperation = toAwait.then(operation);
- const wrappedOperation = nextOperation.then(result => {
- this.afterOp(key);
- return result;
- });
- tuple[1] = wrappedOperation;
- return wrappedOperation;
- }
- beforeOp(key) {
- let tuple = this.queue[key];
- if (!tuple) {
- tuple = [0, Promise.resolve()];
- this.queue[key] = tuple;
- }
- tuple[0]++;
- return tuple;
- }
- afterOp(key) {
- const tuple = this.queue[key];
- if (!tuple) {
- return;
- }
- tuple[0]--;
- if (tuple[0] <= 0) {
- delete this.queue[key];
- return;
- }
- }
- }
- exports.KeyPromiseQueue = KeyPromiseQueue;
|