-
Notifications
You must be signed in to change notification settings - Fork 0
/
fsm.js
94 lines (94 loc) · 3.3 KB
/
fsm.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const index = require("../../fsm/es/index.js");
const require$$0 = require("react");
const useIsomorphicLayoutEffect_browser_esm = require("../../../use-isomorphic-layout-effect/dist/use-isomorphic-layout-effect.browser.esm.js");
require("../../../use-sync-external-store/shim/with-selector.js");
const useConstant = require("./useConstant.js");
const withSelector = require("../../../../_virtual/with-selector.js");
var __read = globalThis && globalThis.__read || function(o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m)
return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done)
ar.push(r.value);
} catch (error) {
e = { error };
} finally {
try {
if (r && !r.done && (m = i["return"]))
m.call(i);
} finally {
if (e)
throw e.error;
}
}
return ar;
};
function identity(a) {
return a;
}
var getServiceState = function(service) {
var currentValue;
service.subscribe(function(state) {
currentValue = state;
}).unsubscribe();
return currentValue;
};
function useMachine(stateMachine, options) {
var persistedStateRef = require$$0.useRef();
if (process.env.NODE_ENV !== "production") {
var _a = __read(require$$0.useState(stateMachine), 1), initialMachine = _a[0];
if (stateMachine !== initialMachine) {
console.warn("Machine given to `useMachine` has changed between renders. This is not supported and might lead to unexpected results.\nPlease make sure that you pass the same Machine as argument each time.");
}
}
var _b = __read(useConstant(function() {
var queue2 = [];
var service2 = index.interpret(index.createMachine(stateMachine.config, options ? options : stateMachine._options));
var send = service2.send;
service2.send = function(event) {
if (service2.status === index.InterpreterStatus.NotStarted) {
queue2.push(event);
return;
}
send(event);
persistedStateRef.current = service2.state;
};
return [service2, queue2];
}), 2), service = _b[0], queue = _b[1];
useIsomorphicLayoutEffect_browser_esm(function() {
if (options) {
service._machine._options = options;
}
});
var useServiceResult = useService(service);
require$$0.useEffect(function() {
service.start(persistedStateRef.current);
queue.forEach(service.send);
persistedStateRef.current = service.state;
return function() {
service.stop();
};
}, []);
return useServiceResult;
}
var isEqual = function(_prevState, nextState) {
return nextState.changed === false;
};
function useService(service) {
var getSnapshot = require$$0.useCallback(function() {
return getServiceState(service);
}, [service]);
var subscribe = require$$0.useCallback(function(handleStoreChange) {
var unsubscribe = service.subscribe(handleStoreChange).unsubscribe;
return unsubscribe;
}, [service]);
var storeSnapshot = withSelector.withSelector.exports.useSyncExternalStoreWithSelector(subscribe, getSnapshot, getSnapshot, identity, isEqual);
return [storeSnapshot, service.send, service];
}
exports.useMachine = useMachine;
exports.useService = useService;
//# sourceMappingURL=fsm.js.map