rc-js-util
Version:
A collection of TS and C++ utilities to help writing performant and correct applications, achieved through strict typing and (removable) invariant checking.
66 lines • 2.01 kB
JavaScript
import { _Debug } from "../debug/_debug.js";
/**
* @public
* Performs update operations once every `waitPeriod` until the iterator returned by {@link IIncrementallyUpdatable} is
* exhausted.
*/
export class IncrementalUpdater {
constructor(updatable, waitPeriod = 4) {
this.updatable = updatable;
this.waitPeriod = waitPeriod;
/**
* Remains true while the update is suspended.
*/
this.isUpdating = false;
this.update = () => {
var _a;
(_a = this.currentIterator) !== null && _a !== void 0 ? _a : (this.currentIterator = this.updatable.incrementallyUpdate());
if (this.currentIterator.next().done === true) {
this.currentIterator = null;
this.id = null;
this.isUpdating = false;
}
else {
this.id = setTimeout(this.update, this.waitPeriod);
}
};
this.currentIterator = null;
this.id = null;
}
/**
* Cancel the update and clear the task.
*/
cancel() {
if (this.id != null) {
clearTimeout(this.id);
this.id = null;
this.isUpdating = false;
}
this.currentIterator = null;
}
/**
* Suspends the current task if one is active.
*/
suspend() {
if (this.id != null) {
clearTimeout(this.id);
this.id = null;
}
}
/**
* Start a new update cycle. If an update was already in progress it will be cancelled.
*/
beginUpdate() {
this.cancel();
this.isUpdating = true;
this.update();
}
/**
* Resumes the currently suspended task. It is an error to call this if there is not a currently suspended task.
*/
resume() {
_BUILD.DEBUG && _Debug.assert(this.isUpdating && this.id == null, "nothing to resume");
this.update();
}
}
//# sourceMappingURL=incremental-updater.js.map