proxy-state-tree
Version:
An implementation of the Mobx/Vue state tracking approach, for library authors
59 lines • 1.61 kB
JavaScript
export class TrackStateTree {
disposeOnReset;
root;
pathDependencies = new Set();
state;
proxifier;
trackPathListeners = [];
constructor(root) {
this.root = root;
this.proxifier = root.proxifier;
this.state = root.state;
}
trackPaths() {
const paths = new Set();
const listener = (path) => {
paths.add(path);
};
this.trackPathListeners.push(listener);
return () => {
this.trackPathListeners.splice(this.trackPathListeners.indexOf(listener), 1);
return paths;
};
}
track() {
this.root.setTrackStateTree(this);
return this;
}
canMutate() {
return false;
}
canTrack() {
return true;
}
addTrackingPath(path) {
this.pathDependencies.add(path);
}
subscribe(cb) {
this.root.unsetTrackStateTree(this);
for (const path of this.pathDependencies) {
this.root.addPathDependency(path, cb);
}
return () => {
for (const path of this.pathDependencies) {
this.root.removePathDependency(path, cb);
}
};
}
trackScope(scope) {
const previousPreviousTree = this.root.previousTree;
const previousCurrentTree = this.root.currentTree;
this.root.currentTree = this;
this.track();
scope(this);
this.root.currentTree = previousCurrentTree;
this.root.previousTree = previousPreviousTree;
return this;
}
}
//# sourceMappingURL=TrackStateTree.js.map