UNPKG

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