UNPKG

@benev/slate

Version:
36 lines 1.02 kB
import { Slice } from "./parts/slice.js"; import { deep } from "../tools/deep/deep.js"; export class StateTree { #state; #readable; #onChange; #circularity_lock = false; #make_frozen_clone() { return deep.freeze(structuredClone(this.#state)); } constructor(state, onChange = () => { }) { this.#state = structuredClone(state); this.#readable = this.#make_frozen_clone(); this.#onChange = onChange; } get state() { return this.#readable; } transmute(fun) { if (this.#circularity_lock) throw new Error("circular error"); this.#circularity_lock = true; this.#state = fun(structuredClone(this.#state)); this.#readable = this.#make_frozen_clone(); this.#onChange(); this.#circularity_lock = false; } slice({ getter, setter }) { return new Slice({ parent: this, getter, setter, }); } } //# sourceMappingURL=state_tree.js.map