123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- "use strict";
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.KeyPromiseQueue = void 0;
- // KeyPromiseQueue is a simple promise queue
- // used to queue operations per key basis.
- // Once the tail promise in the key-queue fulfills,
- // the chain on that key will be cleared.
- 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;
- //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJLZXlQcm9taXNlUXVldWUiLCJjb25zdHJ1Y3RvciIsInF1ZXVlIiwiZW5xdWV1ZSIsImtleSIsIm9wZXJhdGlvbiIsInR1cGxlIiwiYmVmb3JlT3AiLCJ0b0F3YWl0IiwibmV4dE9wZXJhdGlvbiIsInRoZW4iLCJ3cmFwcGVkT3BlcmF0aW9uIiwicmVzdWx0IiwiYWZ0ZXJPcCIsIlByb21pc2UiLCJyZXNvbHZlIiwiZXhwb3J0cyJdLCJzb3VyY2VzIjpbIi4uL3NyYy9LZXlQcm9taXNlUXVldWUuanMiXSwic291cmNlc0NvbnRlbnQiOlsiLy8gS2V5UHJvbWlzZVF1ZXVlIGlzIGEgc2ltcGxlIHByb21pc2UgcXVldWVcbi8vIHVzZWQgdG8gcXVldWUgb3BlcmF0aW9ucyBwZXIga2V5IGJhc2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