@tscommon/synchronized
Version:
In an async environment, a race condition occurs when two or more async operations attempt to update mutable shared data at the same time. @synchronized decorator offers a mechanism to avoid race conditions by synchronizing async operations access to shar
20 lines • 735 B
JavaScript
;
/* eslint-disable @typescript-eslint/no-explicit-any */
Object.defineProperty(exports, "__esModule", { value: true });
exports.synchronized = synchronized;
/**
* Synchronize method calls on the same object.
*/
function synchronized(_target, _propertyKey, descriptor) {
const queue = new WeakMap();
if (typeof descriptor.value === 'function') {
const { value: method } = descriptor;
descriptor.value = function synchronized(...args) {
const next = () => method.apply(this, args);
const promise = Promise.resolve(queue.get(this)).then(next, next);
queue.set(this, promise);
return promise;
};
}
}
//# sourceMappingURL=synchronized.js.map