12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
- function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
- function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
- function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
- function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
- /**
- * @typedef {Object} StateDefinitions
- * @property {{[event: string]: { target: string; actions?: Array<string> }}} [on]
- */
- /**
- * @typedef {Object} Options
- * @property {{[state: string]: StateDefinitions}} states
- * @property {object} context;
- * @property {string} initial
- */
- /**
- * @typedef {Object} Implementation
- * @property {{[actionName: string]: (ctx: object, event: any) => object}} actions
- */
- /**
- * A simplified `createMachine` from `@xstate/fsm` with the following differences:
- *
- * - the returned machine is technically a "service". No `interpret(machine).start()` is needed.
- * - the state definition only support `on` and target must be declared with { target: 'nextState', actions: [] } explicitly.
- * - event passed to `send` must be an object with `type` property.
- * - actions implementation will be [assign action](https://xstate.js.org/docs/guides/context.html#assign-action) if you return any value.
- * Do not return anything if you just want to invoke side effect.
- *
- * The goal of this custom function is to avoid installing the entire `'xstate/fsm'` package, while enabling modeling using
- * state machine. You can copy the first parameter into the editor at https://stately.ai/viz to visualize the state machine.
- *
- * @param {Options} options
- * @param {Implementation} implementation
- */
- function createMachine(_ref, _ref2) {
- var states = _ref.states,
- context = _ref.context,
- initial = _ref.initial;
- var actions = _ref2.actions;
- var currentState = initial;
- var currentContext = context;
- return {
- send: function send(event) {
- var currentStateOn = states[currentState].on;
- var transitionConfig = currentStateOn && currentStateOn[event.type];
- if (transitionConfig) {
- currentState = transitionConfig.target;
- if (transitionConfig.actions) {
- transitionConfig.actions.forEach(function (actName) {
- var actionImpl = actions[actName];
- var nextContextValue = actionImpl && actionImpl(currentContext, event);
- if (nextContextValue) {
- currentContext = _objectSpread(_objectSpread({}, currentContext), nextContextValue);
- }
- });
- }
- }
- }
- };
- }
- export default createMachine;
|