@fbltd/async
Version:
Miscellaneous async utils
45 lines (44 loc) • 974 B
JavaScript
class ObservationState {
_isObserved = false;
_isSuspended = false;
dependencies = undefined;
stack = [];
suspend() {
this._isSuspended = true;
}
cancelSuspense() {
this._isSuspended = false;
}
get isObserved() {
return this._isObserved;
}
set isObserved(value) {
if (value) {
this.push();
}
else {
this.pop();
}
this._isObserved = !!this.dependencies;
}
setDep(dep) {
if (this._isSuspended)
return;
if (!this.isObserved)
return;
if (dep.done)
return;
this.dependencies.add(dep);
}
getDeps() {
return this.dependencies;
}
push() {
this.stack.push(this.dependencies);
this.dependencies = new Set();
}
pop() {
this.dependencies = this.stack.pop();
}
}
export const observationState = new ObservationState();