@uwdata/mosaic-core
Version:
Scalable and extensible linked data views.
52 lines • 1.38 kB
JavaScript
/**
* Synchronizer class to aid synchronization of updates on multiple pending operations.
*/
export class Synchronizer {
_set;
_done;
_promise;
/**
* Create a new synchronizer instance.
*/
constructor() {
this._set = new Set();
this._done = () => { };
this._promise = new Promise(resolve => this._done = resolve);
}
/**
* Mark an item as pending.
* @param item An item to synchronize on.
*/
pending(item) {
this._set.add(item);
}
/**
* Mark a pending item as ready, indicating it is
* ready for a synchronized update.
* @param item An item to synchronize on.
* @returns True if the synchronizer is ready to
* resolve, false otherwise.
*/
ready(item) {
this._set.delete(item);
return this._set.size === 0;
}
/**
* Resolve the current synchronization cycle, causing the synchronize
* promise to resolve and thereby trigger downstream updates.
*/
resolve() {
this._promise = new Promise(resolve => {
this._done();
this._done = resolve;
});
}
/**
* The promise for the current synchronization cycle.
* @returns The synchronization promise.
*/
get promise() {
return this._promise;
}
}
//# sourceMappingURL=synchronizer.js.map