UNPKG

3.62 kBJavaScriptView Raw
1import { interpret } from 'xstate';
2import { atom } from 'jotai';
3
4var __defProp = Object.defineProperty;
5var __getOwnPropSymbols = Object.getOwnPropertySymbols;
6var __hasOwnProp = Object.prototype.hasOwnProperty;
7var __propIsEnum = Object.prototype.propertyIsEnumerable;
8var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
9var __spreadValues = (a, b) => {
10 for (var prop in b || (b = {}))
11 if (__hasOwnProp.call(b, prop))
12 __defNormalProp(a, prop, b[prop]);
13 if (__getOwnPropSymbols)
14 for (var prop of __getOwnPropSymbols(b)) {
15 if (__propIsEnum.call(b, prop))
16 __defNormalProp(a, prop, b[prop]);
17 }
18 return a;
19};
20var __objRest = (source, exclude) => {
21 var target = {};
22 for (var prop in source)
23 if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
24 target[prop] = source[prop];
25 if (source != null && __getOwnPropSymbols)
26 for (var prop of __getOwnPropSymbols(source)) {
27 if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
28 target[prop] = source[prop];
29 }
30 return target;
31};
32function atomWithMachine(getMachine, getOptions) {
33 const cachedMachineAtom = atom(null);
34 const machineAtom = atom((get) => {
35 const cachedMachine = get(cachedMachineAtom);
36 if (cachedMachine) {
37 return cachedMachine;
38 }
39 let initializing = true;
40 const safeGet = (a) => {
41 if (initializing) {
42 return get(a);
43 }
44 throw new Error("get not allowed after initialization");
45 };
46 const machine = typeof getMachine === "function" ? getMachine(safeGet) : getMachine;
47 const options = typeof getOptions === "function" ? getOptions(safeGet) : getOptions;
48 initializing = false;
49 const _a = options || {}, {
50 guards,
51 actions,
52 activities,
53 services,
54 delays
55 } = _a, interpreterOptions = __objRest(_a, [
56 "guards",
57 "actions",
58 "activities",
59 "services",
60 "delays"
61 ]);
62 const machineConfig = __spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues({}, guards && { guards }), actions && { actions }), activities && { activities }), services && { services }), delays && { delays });
63 const machineWithConfig = machine.withConfig(machineConfig, machine.context);
64 const service = interpret(machineWithConfig, interpreterOptions);
65 return { machine: machineWithConfig, service };
66 }, (get, set, _arg) => {
67 set(cachedMachineAtom, get(machineAtom));
68 });
69 machineAtom.onMount = (commit) => {
70 commit();
71 };
72 const cachedMachineStateAtom = atom(null);
73 const machineStateAtom = atom((get) => {
74 var _a;
75 return (_a = get(cachedMachineStateAtom)) != null ? _a : get(machineAtom).machine.initialState;
76 }, (get, set, registerCleanup) => {
77 const { service } = get(machineAtom);
78 service.onTransition((nextState) => {
79 set(cachedMachineStateAtom, nextState);
80 });
81 service.start();
82 registerCleanup(() => {
83 service.stop();
84 });
85 });
86 machineStateAtom.onMount = (initialize) => {
87 let unsub;
88 initialize((cleanup) => {
89 if (unsub === false) {
90 cleanup();
91 } else {
92 unsub = cleanup;
93 }
94 });
95 return () => {
96 if (unsub) {
97 unsub();
98 }
99 unsub = false;
100 };
101 };
102 const machineStateWithServiceAtom = atom((get) => get(machineStateAtom), (get, _set, event) => {
103 const { service } = get(machineAtom);
104 service.send(event);
105 });
106 return machineStateWithServiceAtom;
107}
108
109export { atomWithMachine };