UNPKG

overmind

Version:
70 lines (68 loc) 2.4 kB
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.js'); const require_internalTypes = require('./internalTypes.js'); let proxy_state_tree = require("proxy-state-tree"); //#region src/derived.ts const IS_DERIVED = Symbol("IS_DERIVED"); const IS_DERIVED_CONSTRUCTOR = Symbol("IS_DERIVED_CONSTRUCTOR"); var Derived = class { constructor(cb) { this.cb = cb; this.isDirty = true; this.updateCount = 0; const boundEvaluate = this.evaluate.bind(this); if (process.env.NODE_ENV === "development") boundEvaluate.dispose = () => { this.disposeOnMutation(); }; boundEvaluate[IS_DERIVED] = true; return boundEvaluate; } runScope(tree, path) { const parent = path.slice(0, path.length - 1).reduce((curr, key) => curr[key], tree.state); return this.cb(parent, tree.state); } evaluate(eventHub, tree, proxyStateTree, path) { if (!this.disposeOnMutation) this.disposeOnMutation = proxyStateTree.onMutation((_, paths, flushId) => { if (typeof path.reduce((aggr, key) => aggr && aggr[key], proxyStateTree.sourceState) !== "function") { this.disposeOnMutation(); return; } if (this.isDirty) return; for (const mutationPath of paths) if (this.paths.has(mutationPath)) { this.isDirty = true; eventHub.emitAsync(require_internalTypes.EventType.DERIVED_DIRTY, { derivedPath: path, path: mutationPath, flushId }); return; } }); if (this.isDirty || this.previousProxifier !== tree.proxifier) { const getPaths = tree.trackPaths(); this.value = this.runScope(tree, path); this.isDirty = false; this.paths = getPaths(); if (process.env.NODE_ENV === "development") { eventHub.emitAsync(require_internalTypes.EventType.DERIVED, { path, paths: Array.from(this.paths), updateCount: this.updateCount, value: this.value }); this.updateCount++; } } if (tree instanceof proxy_state_tree.TrackStateTree) for (const path$1 of this.paths) { tree.addTrackingPath(path$1); tree.trackPathListeners.forEach((cb) => cb(path$1)); } this.previousProxifier = tree.proxifier; if (this.value && this.value[proxy_state_tree.IS_PROXY]) return proxyStateTree.rescope(this.value, tree); return this.value; } }; //#endregion exports.Derived = Derived; exports.IS_DERIVED = IS_DERIVED; exports.IS_DERIVED_CONSTRUCTOR = IS_DERIVED_CONSTRUCTOR; //# sourceMappingURL=derived.js.map