UNPKG

overmind

Version:
109 lines (107 loc) 4.17 kB
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.js'); const require_utils = require('./utils.js'); let proxy_state_tree = require("proxy-state-tree"); let is_plain_obj = require("is-plain-obj"); is_plain_obj = require_rolldown_runtime.__toESM(is_plain_obj); //#region src/statemachine.ts const INITIAL_STATE = Symbol("INITIAL_STATE"); const TRANSITIONS = Symbol("TRANSITIONS"); const STATE = Symbol("STATE"); const IS_DISPOSED = Symbol("IS_DISPOSED"); const CURRENT_KEYS = Symbol("CURRENT_KEYS"); const BASE_STATE = Symbol("BASE_STATE"); const TRANSITION_LISTENERS = Symbol("TRANSITION_LISTENERS"); function deepCopy(obj) { if (require_utils.isStateMachine(obj)) return obj.clone(); else if ((0, is_plain_obj.default)(obj)) return Object.keys(obj).reduce((aggr, key) => { if (key === "__esModule") return aggr; const originalDescriptor = Object.getOwnPropertyDescriptor(obj, key); const isAGetter = originalDescriptor && "get" in originalDescriptor; const value = obj[key]; if (isAGetter) Object.defineProperty(aggr, key, originalDescriptor); else aggr[key] = deepCopy(value); return aggr; }, {}); else if (Array.isArray(obj)) return obj.map((item) => deepCopy(item)); return obj; } var StateMachine = class StateMachine { clone() { return new StateMachine(this[TRANSITIONS], deepCopy(this[STATE]), deepCopy(this[BASE_STATE])); } dispose() { this[proxy_state_tree.VALUE][TRANSITION_LISTENERS] = []; Object.keys(this[proxy_state_tree.VALUE]).forEach((key) => { if (this[proxy_state_tree.VALUE][key] instanceof StateMachine) this[key].dispose(); }); this[proxy_state_tree.VALUE][IS_DISPOSED] = true; } constructor(transitions, state, baseState) { this[TRANSITION_LISTENERS] = []; this[IS_DISPOSED] = false; this[STATE] = state; this[BASE_STATE] = baseState; this[INITIAL_STATE] = state.current; this[TRANSITIONS] = transitions; this[CURRENT_KEYS] = Object.keys(state); Object.assign(this, state, baseState); } send(type, data) { if (this[proxy_state_tree.VALUE][IS_DISPOSED]) { if (process.env.NODE_ENV === "development") console.warn(`Overmind - The statemachine at "${this[proxy_state_tree.PATH]}" has been disposed, but you tried to transition on it`); return this; } const tree = this[proxy_state_tree.PROXY_TREE].root.mutationTree || this[proxy_state_tree.PROXY_TREE]; tree.enableMutations(); let result; if (typeof this[proxy_state_tree.VALUE][TRANSITIONS] === "function") { const transition = this[proxy_state_tree.VALUE][TRANSITIONS]; result = transition({ type, data }, this); } else if (this[proxy_state_tree.VALUE][TRANSITIONS][this[proxy_state_tree.VALUE].current][type]) { const transition = this[proxy_state_tree.VALUE][TRANSITIONS][this[proxy_state_tree.VALUE].current][type]; result = transition(data, this); } if (result) { this[proxy_state_tree.VALUE].previousState = this[proxy_state_tree.VALUE].current; this[proxy_state_tree.VALUE][CURRENT_KEYS].forEach((key) => { if (key !== "current") delete this[key]; }); this[proxy_state_tree.VALUE][CURRENT_KEYS] = Object.keys(result); Object.assign(this, result); const devtools = tree.root.options.getDevtools?.(); if (devtools && typeof devtools.send === "function") devtools.send({ type: "machine:transition", data: { path: this[proxy_state_tree.PATH], fromState: this[proxy_state_tree.VALUE].previousState, toState: this[proxy_state_tree.VALUE].current, eventType: type, payload: data, timestamp: Date.now() } }); this[proxy_state_tree.VALUE][TRANSITION_LISTENERS].forEach((listener) => listener(this)); } tree.blockMutations(); return this; } matches(state) { if (this.current === state) return this; } onTransition(listener) { this[proxy_state_tree.VALUE][TRANSITION_LISTENERS].push(listener); } }; function statemachine(transitions) { return { create(state, baseState) { return new StateMachine(transitions, state, baseState); } }; } //#endregion exports.StateMachine = StateMachine; exports.deepCopy = deepCopy; exports.statemachine = statemachine; //# sourceMappingURL=statemachine.js.map