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