fsm.js 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. 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; }
  2. 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; }
  3. 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; }
  4. function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
  5. 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); }
  6. /**
  7. * @typedef {Object} StateDefinitions
  8. * @property {{[event: string]: { target: string; actions?: Array<string> }}} [on]
  9. */
  10. /**
  11. * @typedef {Object} Options
  12. * @property {{[state: string]: StateDefinitions}} states
  13. * @property {object} context;
  14. * @property {string} initial
  15. */
  16. /**
  17. * @typedef {Object} Implementation
  18. * @property {{[actionName: string]: (ctx: object, event: any) => object}} actions
  19. */
  20. /**
  21. * A simplified `createMachine` from `@xstate/fsm` with the following differences:
  22. *
  23. * - the returned machine is technically a "service". No `interpret(machine).start()` is needed.
  24. * - the state definition only support `on` and target must be declared with { target: 'nextState', actions: [] } explicitly.
  25. * - event passed to `send` must be an object with `type` property.
  26. * - actions implementation will be [assign action](https://xstate.js.org/docs/guides/context.html#assign-action) if you return any value.
  27. * Do not return anything if you just want to invoke side effect.
  28. *
  29. * The goal of this custom function is to avoid installing the entire `'xstate/fsm'` package, while enabling modeling using
  30. * state machine. You can copy the first parameter into the editor at https://stately.ai/viz to visualize the state machine.
  31. *
  32. * @param {Options} options
  33. * @param {Implementation} implementation
  34. */
  35. function createMachine(_ref, _ref2) {
  36. var states = _ref.states,
  37. context = _ref.context,
  38. initial = _ref.initial;
  39. var actions = _ref2.actions;
  40. var currentState = initial;
  41. var currentContext = context;
  42. return {
  43. send: function send(event) {
  44. var currentStateOn = states[currentState].on;
  45. var transitionConfig = currentStateOn && currentStateOn[event.type];
  46. if (transitionConfig) {
  47. currentState = transitionConfig.target;
  48. if (transitionConfig.actions) {
  49. transitionConfig.actions.forEach(function (actName) {
  50. var actionImpl = actions[actName];
  51. var nextContextValue = actionImpl && actionImpl(currentContext, event);
  52. if (nextContextValue) {
  53. currentContext = _objectSpread(_objectSpread({}, currentContext), nextContextValue);
  54. }
  55. });
  56. }
  57. }
  58. }
  59. };
  60. }
  61. export default createMachine;