UNPKG

4.09 kBJavaScriptView Raw
1'use strict';
2
3var xstate = require('xstate');
4var jotai = require('jotai');
5
6function _extends() {
7 _extends = Object.assign ? Object.assign.bind() : function (target) {
8 for (var i = 1; i < arguments.length; i++) {
9 var source = arguments[i];
10 for (var key in source) {
11 if (Object.prototype.hasOwnProperty.call(source, key)) {
12 target[key] = source[key];
13 }
14 }
15 }
16 return target;
17 };
18 return _extends.apply(this, arguments);
19}
20function _objectWithoutPropertiesLoose(source, excluded) {
21 if (source == null) return {};
22 var target = {};
23 var sourceKeys = Object.keys(source);
24 var key, i;
25 for (i = 0; i < sourceKeys.length; i++) {
26 key = sourceKeys[i];
27 if (excluded.indexOf(key) >= 0) continue;
28 target[key] = source[key];
29 }
30 return target;
31}
32
33var _excluded = ["guards", "actions", "services", "delays", "context"];
34var RESTART = Symbol();
35function atomWithMachine(getMachine, getOptions) {
36 var cachedMachineAtom = jotai.atom(null);
37 var machineAtom = jotai.atom(function (get) {
38 var cachedMachine = get(cachedMachineAtom);
39 if (cachedMachine) {
40 return cachedMachine;
41 }
42 var initializing = true;
43 var safeGet = function safeGet(a) {
44 if (initializing) {
45 return get(a);
46 }
47 throw new Error('get not allowed after initialization');
48 };
49 var machine = isGetter(getMachine) ? getMachine(safeGet) : getMachine;
50 var options = isGetter(getOptions) ? getOptions(safeGet) : getOptions;
51 initializing = false;
52 var _ref = options || {},
53 guards = _ref.guards,
54 actions = _ref.actions,
55 services = _ref.services,
56 delays = _ref.delays,
57 context = _ref.context,
58 interpreterOptions = _objectWithoutPropertiesLoose(_ref, _excluded);
59 var machineConfig = _extends({}, guards && {
60 guards: guards
61 }, actions && {
62 actions: actions
63 }, services && {
64 services: services
65 }, delays && {
66 delays: delays
67 });
68 var machineWithConfig = machine.withConfig(machineConfig, function () {
69 return _extends({}, machine.context, context);
70 });
71 var service = xstate.interpret(machineWithConfig, interpreterOptions);
72 return {
73 machine: machineWithConfig,
74 service: service
75 };
76 }, function (get, set, _arg) {
77 set(cachedMachineAtom, get(machineAtom));
78 });
79 machineAtom.onMount = function (commit) {
80 commit();
81 };
82 var cachedMachineStateAtom = jotai.atom(null);
83 var machineStateAtom = jotai.atom(function (get) {
84 var _get;
85 return (_get = get(cachedMachineStateAtom)) != null ? _get : get(machineAtom).machine.initialState;
86 }, function (get, set, registerCleanup) {
87 var _get2 = get(machineAtom),
88 service = _get2.service;
89 service.onTransition(function (nextState) {
90 set(cachedMachineStateAtom, nextState);
91 });
92 service.start();
93 registerCleanup(function () {
94 var _get3 = get(machineAtom),
95 service = _get3.service;
96 service.stop();
97 });
98 });
99 machineStateAtom.onMount = function (initialize) {
100 var unsub;
101 initialize(function (cleanup) {
102 if (unsub === false) {
103 cleanup();
104 } else {
105 unsub = cleanup;
106 }
107 });
108 return function () {
109 if (unsub) {
110 unsub();
111 }
112 unsub = false;
113 };
114 };
115 var machineStateWithServiceAtom = jotai.atom(function (get) {
116 return get(machineStateAtom);
117 }, function (get, set, event) {
118 var _get4 = get(machineAtom),
119 service = _get4.service;
120 if (event === RESTART) {
121 service.stop();
122 set(cachedMachineAtom, null);
123 set(machineAtom, null);
124 var _get5 = get(machineAtom),
125 newService = _get5.service;
126 newService.onTransition(function (nextState) {
127 set(cachedMachineStateAtom, nextState);
128 });
129 newService.start();
130 } else {
131 service.send(event);
132 }
133 });
134 return machineStateWithServiceAtom;
135}
136var isGetter = function isGetter(v) {
137 return typeof v === 'function';
138};
139
140exports.RESTART = RESTART;
141exports.atomWithMachine = atomWithMachine;