UNPKG

overmind

Version:
117 lines 4.54 kB
"use strict"; var _a, _b; Object.defineProperty(exports, "__esModule", { value: true }); exports.statemachine = exports.StateMachine = exports.deepCopy = void 0; const tslib_1 = require("tslib"); const is_plain_obj_1 = tslib_1.__importDefault(require("is-plain-obj")); const proxy_state_tree_1 = require("proxy-state-tree"); 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'); // We have to export here to avoid a circular dependency issue with "utils" function deepCopy(obj) { if (obj instanceof StateMachine) { return obj.clone(); } else if ((0, is_plain_obj_1.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; } exports.deepCopy = deepCopy; class StateMachine { clone() { return new StateMachine(this[TRANSITIONS], deepCopy(this[STATE]), deepCopy(this[BASE_STATE])); } dispose() { this[proxy_state_tree_1.VALUE][TRANSITION_LISTENERS] = []; Object.keys(this[proxy_state_tree_1.VALUE]).forEach((key) => { if (this[proxy_state_tree_1.VALUE][key] instanceof StateMachine) { this[key].dispose(); } }); this[proxy_state_tree_1.VALUE][IS_DISPOSED] = true; } constructor(transitions, state, baseState) { this[_a] = []; this[_b] = false; this[STATE] = state; this[BASE_STATE] = baseState; this[INITIAL_STATE] = state.current; this[BASE_STATE] = baseState; this[TRANSITIONS] = transitions; this[CURRENT_KEYS] = Object.keys(state); Object.assign(this, state, baseState); } send(type, data) { if (this[proxy_state_tree_1.VALUE][IS_DISPOSED]) { if (process.env.NODE_ENV === 'development') { console.warn(`Overmind - The statemachine at "${this[proxy_state_tree_1.PATH]}" has been disposed, but you tried to transition on it`); } return this; } const tree = this[proxy_state_tree_1.PROXY_TREE].master.mutationTree || this[proxy_state_tree_1.PROXY_TREE]; tree.enableMutations(); let result; if (typeof this[proxy_state_tree_1.VALUE][TRANSITIONS] === 'function') { const transition = this[proxy_state_tree_1.VALUE][TRANSITIONS]; result = transition({ type, data }, this); } else if (this[proxy_state_tree_1.VALUE][TRANSITIONS][this[proxy_state_tree_1.VALUE].current][type]) { const transition = this[proxy_state_tree_1.VALUE][TRANSITIONS][this[proxy_state_tree_1.VALUE].current][type]; result = transition(data, this); } if (result) { this[proxy_state_tree_1.VALUE][CURRENT_KEYS].forEach((key) => { if (key !== 'current') { delete this[key]; } }); this[proxy_state_tree_1.VALUE][CURRENT_KEYS] = Object.keys(result); Object.assign(this, result); this[proxy_state_tree_1.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_1.VALUE][TRANSITION_LISTENERS].push(listener); } } exports.StateMachine = StateMachine; _a = TRANSITION_LISTENERS, _b = IS_DISPOSED; function statemachine(transitions) { return { create(state, baseState) { return new StateMachine(transitions, state, baseState); }, }; } exports.statemachine = statemachine; //# sourceMappingURL=statemachine.js.map