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