UNPKG

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