proxy-state-tree
Version:
An implementation of the Mobx/Vue state tracking approach, for library authors
59 lines • 1.72 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TrackStateTree = void 0;
class TrackStateTree {
constructor(root) {
this.pathDependencies = new Set();
this.trackPathListeners = [];
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;
}
}
exports.TrackStateTree = TrackStateTree;
//# sourceMappingURL=TrackStateTree.js.map