@drift-labs/common
Version:
Common functions for Drift
24 lines • 1.12 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.PollingSequenceGuard = exports.LATE_POLLING_RESPONSE = void 0;
exports.LATE_POLLING_RESPONSE = Symbol('Late polling response');
/**
* Utility class which makes sure that asyncronous responses get rejected when we expect them to return in the same sequence that we request them but they don't
*/
class PollingSequenceGuard {
static async fetch(key, cb) {
var _a, _b;
const newCount = ((_a = this.pollingCounts.get(key)) !== null && _a !== void 0 ? _a : 0) + 1;
const promise = await cb();
const latestCount = (_b = this.pollingCounts.get(key)) !== null && _b !== void 0 ? _b : 0;
if (latestCount > newCount) {
return Promise.reject(exports.LATE_POLLING_RESPONSE);
}
this.pollingCounts.set(key, newCount);
return promise;
}
}
exports.PollingSequenceGuard = PollingSequenceGuard;
PollingSequenceGuard.pollingCounts = new Map();
PollingSequenceGuard.LATE_POLLING_RESPONSE = exports.LATE_POLLING_RESPONSE;
//# sourceMappingURL=pollingSequenceGuard.js.map
;