aspen-tree-model
Version:
State container for aspen trees
52 lines • 2.01 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TreeModel = void 0;
const aspen_core_1 = require("aspen-core");
const notificar_1 = require("notificar");
const treeState_1 = require("./treeState");
var TreeModelEvent;
(function (TreeModelEvent) {
TreeModelEvent[TreeModelEvent["Change"] = 1] = "Change";
})(TreeModelEvent || (TreeModelEvent = {}));
class TreeModel {
constructor(host, rootPath) {
this.dispatchChange = () => {
this.events.dispatch(TreeModelEvent.Change);
};
this.root = new aspen_core_1.Root(host, rootPath);
this.state = new treeState_1.TreeStateManager(this.root);
this.events = new notificar_1.Notificar();
this.root.onDidUpdate(this.dispatchChange);
}
onChange(callback) {
return this.events.add(TreeModelEvent.Change, callback);
}
async loadTreeState(state) {
if (typeof state === 'string') {
state = JSON.parse(state);
}
return this.state.loadTreeState(state);
}
/**
* Returns a `TreeStateWatcher` that will stay in sync with actual tree state at all times
*
* Included in TreeState:
* - Directory expansion states
* - Scroll offset
*
* Not included in TreeState:
* - Decorations
* - Prompts
*
* Use `TreeStateWatcher#onChange` to attach a listener for when state is updated.
*
* Use `TreeStateWatcher#snapshot` to get snapshot of current tree state (not serialized, but serializable; use `JSON.stringify()`). You can make a time machine with this
*
* Use `TreeStateWatcher#toString` to convert the current state into a JSON string. Useful if you want save the current state to be able restore it later
*/
getTreeStateWatcher(atSurfaceExpandedDirsOnly = false) {
return new treeState_1.TreeStateWatcher(this.state, atSurfaceExpandedDirsOnly);
}
}
exports.TreeModel = TreeModel;
//# sourceMappingURL=TreeModel.js.map