@xstate/react 4.0.0-alpha.2 → 4.0.0-beta.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +0 -8
- package/dist/declarations/src/createActorContext.d.ts +12 -9
- package/dist/declarations/src/index.d.ts +6 -7
- package/dist/declarations/src/useActor.d.ts +2 -3
- package/dist/declarations/src/useActorRef.d.ts +14 -0
- package/dist/declarations/src/useMachine.d.ts +8 -9
- package/dist/xstate-react.cjs.d.mts +2 -0
- package/dist/xstate-react.cjs.d.mts.map +1 -0
- package/dist/xstate-react.cjs.d.ts +1 -0
- package/dist/xstate-react.cjs.d.ts.map +1 -0
- package/dist/xstate-react.cjs.js +187 -4
- package/dist/xstate-react.cjs.mjs +8 -0
- package/dist/xstate-react.development.cjs.js +199 -0
- package/dist/xstate-react.development.cjs.mjs +8 -0
- package/dist/xstate-react.development.esm.js +168 -0
- package/dist/xstate-react.esm.js +91 -253
- package/package.json +21 -21
- package/dist/declarations/src/fsm.d.ts +0 -3
- package/dist/declarations/src/types.d.ts +0 -15
- package/dist/declarations/src/useConstant.d.ts +0 -1
- package/dist/declarations/src/useInterpret.d.ts +0 -12
- package/dist/declarations/src/useSpawn.d.ts +0 -9
- package/dist/useConstant-0013a606.esm.js +0 -68
- package/dist/useConstant-c09b427a.cjs.prod.js +0 -15
- package/dist/useConstant-ff65b597.cjs.dev.js +0 -71
- package/dist/xstate-react.cjs.dev.js +0 -335
- package/dist/xstate-react.cjs.prod.js +0 -327
- package/fsm/dist/xstate-react-fsm.cjs.d.ts +0 -1
- package/fsm/dist/xstate-react-fsm.cjs.dev.js +0 -87
- package/fsm/dist/xstate-react-fsm.cjs.js +0 -7
- package/fsm/dist/xstate-react-fsm.cjs.prod.js +0 -134
- package/fsm/dist/xstate-react-fsm.esm.js +0 -78
- package/fsm/package.json +0 -4
package/README.md
CHANGED
|
@@ -21,14 +21,6 @@ npm i xstate @xstate/react
|
|
|
21
21
|
|
|
22
22
|
By using the global variable `XStateReact`
|
|
23
23
|
|
|
24
|
-
or
|
|
25
|
-
|
|
26
|
-
```html
|
|
27
|
-
<script src="https://unpkg.com/@xstate/react/dist/xstate-react-fsm.umd.min.js"></script>
|
|
28
|
-
```
|
|
29
|
-
|
|
30
|
-
By using the global variable `XStateReactFSM`
|
|
31
|
-
|
|
32
24
|
2. Import the `useMachine` hook:
|
|
33
25
|
|
|
34
26
|
```js
|
|
@@ -1,15 +1,18 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
|
-
import { ActorRefFrom, AnyStateMachine,
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
useSelector: <T>(selector: (snapshot: SnapshotFrom<
|
|
6
|
-
useActorRef: () => ActorRefFrom<
|
|
2
|
+
import { ActorRefFrom, AnyStateMachine, SnapshotFrom, ActorOptions, AreAllImplementationsAssumedToBeProvided, MarkAllImplementationsAsProvided, StateMachine, AnyActorLogic } from 'xstate';
|
|
3
|
+
type ToMachinesWithProvidedImplementations<TMachine extends AnyStateMachine> = TMachine extends StateMachine<infer TContext, infer TEvent, infer TActor, infer TAction, infer TGuard, infer TDelay, infer TTag, infer TInput, infer TOutput, infer TResolvedTypesMeta> ? StateMachine<TContext, TEvent, TActor, TAction, TGuard, TDelay, TTag, TInput, TOutput, AreAllImplementationsAssumedToBeProvided<TResolvedTypesMeta> extends false ? MarkAllImplementationsAsProvided<TResolvedTypesMeta> : TResolvedTypesMeta> : never;
|
|
4
|
+
export declare function createActorContext<TLogic extends AnyActorLogic>(actorLogic: TLogic, interpreterOptions?: ActorOptions<TLogic>): {
|
|
5
|
+
useSelector: <T>(selector: (snapshot: SnapshotFrom<TLogic>) => T, compare?: (a: T, b: T) => boolean) => T;
|
|
6
|
+
useActorRef: () => ActorRefFrom<TLogic>;
|
|
7
7
|
Provider: (props: {
|
|
8
8
|
children: React.ReactNode;
|
|
9
|
-
|
|
10
|
-
} & (AreAllImplementationsAssumedToBeProvided<
|
|
11
|
-
|
|
9
|
+
options?: ActorOptions<TLogic>;
|
|
10
|
+
} & (TLogic extends AnyStateMachine ? AreAllImplementationsAssumedToBeProvided<TLogic['__TResolvedTypesMeta']> extends true ? {
|
|
11
|
+
logic?: TLogic;
|
|
12
12
|
} : {
|
|
13
|
-
|
|
13
|
+
logic: ToMachinesWithProvidedImplementations<TLogic>;
|
|
14
|
+
} : {
|
|
15
|
+
logic?: TLogic;
|
|
14
16
|
})) => React.ReactElement<any, any>;
|
|
15
17
|
};
|
|
18
|
+
export {};
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export {
|
|
3
|
-
export {
|
|
4
|
-
export {
|
|
5
|
-
export {
|
|
6
|
-
export {
|
|
7
|
-
export { createActorContext } from './createActorContext.ts';
|
|
1
|
+
export { useActor } from "./useActor.js";
|
|
2
|
+
export { useActorRef } from "./useActorRef.js";
|
|
3
|
+
export { useSelector } from "./useSelector.js";
|
|
4
|
+
export { shallowEqual } from "./shallowEqual.js";
|
|
5
|
+
export { createActorContext } from "./createActorContext.js";
|
|
6
|
+
export { useMachine } from "./useMachine.js";
|
|
@@ -1,3 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare function useActor<
|
|
3
|
-
export declare function useActor<TEvent extends EventObject, TSnapshot>(actorRef: ActorRef<TEvent, TSnapshot>): [TSnapshot, (event: TEvent) => void];
|
|
1
|
+
import { ActorRefFrom, AnyActorLogic, ActorOptions, SnapshotFrom } from 'xstate';
|
|
2
|
+
export declare function useActor<TLogic extends AnyActorLogic>(logic: TLogic, options?: ActorOptions<TLogic>): [SnapshotFrom<TLogic>, ActorRefFrom<TLogic>['send'], ActorRefFrom<TLogic>];
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { AnyActorLogic, AnyActor, AnyStateMachine, AreAllImplementationsAssumedToBeProvided, InternalMachineImplementations, ActorRefFrom, ActorOptions, Observer, StateFrom, SnapshotFrom, TODO } from 'xstate';
|
|
2
|
+
export declare function useIdleInterpreter(machine: AnyActorLogic, options: Partial<ActorOptions<AnyActorLogic>>): AnyActor;
|
|
3
|
+
type RestParams<TLogic extends AnyActorLogic> = TLogic extends AnyStateMachine ? AreAllImplementationsAssumedToBeProvided<TLogic['__TResolvedTypesMeta']> extends false ? [
|
|
4
|
+
options: ActorOptions<TLogic> & InternalMachineImplementations<TLogic['__TContext'], TLogic['__TEvent'], TODO, TODO, TODO, TLogic['__TResolvedTypesMeta'], true>,
|
|
5
|
+
observerOrListener?: Observer<StateFrom<TLogic>> | ((value: StateFrom<TLogic>) => void)
|
|
6
|
+
] : [
|
|
7
|
+
options?: ActorOptions<TLogic> & InternalMachineImplementations<TLogic['__TContext'], TLogic['__TEvent'], TODO, TODO, TODO, TLogic['__TResolvedTypesMeta']>,
|
|
8
|
+
observerOrListener?: Observer<StateFrom<TLogic>> | ((value: StateFrom<TLogic>) => void)
|
|
9
|
+
] : [
|
|
10
|
+
options?: ActorOptions<TLogic>,
|
|
11
|
+
observerOrListener?: Observer<SnapshotFrom<TLogic>> | ((value: SnapshotFrom<TLogic>) => void)
|
|
12
|
+
];
|
|
13
|
+
export declare function useActorRef<TLogic extends AnyActorLogic>(machine: TLogic, ...[options, observerOrListener]: RestParams<TLogic>): ActorRefFrom<TLogic>;
|
|
14
|
+
export {};
|
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import { AnyStateMachine, AreAllImplementationsAssumedToBeProvided,
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
] : [
|
|
6
|
-
|
|
1
|
+
import { ActorRefFrom, AnyStateMachine, AreAllImplementationsAssumedToBeProvided, ActorOptions, MissingImplementationsError, StateFrom } from 'xstate';
|
|
2
|
+
/**
|
|
3
|
+
* @alias useActor
|
|
4
|
+
*/
|
|
5
|
+
export declare function useMachine<TMachine extends AnyStateMachine>(machine: AreAllImplementationsAssumedToBeProvided<TMachine['__TResolvedTypesMeta']> extends true ? TMachine : MissingImplementationsError<TMachine['__TResolvedTypesMeta']>, options?: ActorOptions<TMachine>): [
|
|
6
|
+
StateFrom<TMachine>,
|
|
7
|
+
ActorRefFrom<TMachine>['send'],
|
|
8
|
+
ActorRefFrom<TMachine>
|
|
7
9
|
];
|
|
8
|
-
type UseMachineReturn<TMachine extends AnyStateMachine, TInterpreter = InterpreterFrom<TMachine>> = [StateFrom<TMachine>, Prop<TInterpreter, 'send'>, TInterpreter];
|
|
9
|
-
export declare function useMachine<TMachine extends AnyStateMachine>(getMachine: MaybeLazy<TMachine>, ...[options]: RestParams<TMachine>): UseMachineReturn<TMachine>;
|
|
10
|
-
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"xstate-react.cjs.d.mts","sourceRoot":"","sources":["./declarations/src/index.d.ts"],"names":[],"mappings":"AAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"xstate-react.cjs.d.ts","sourceRoot":"","sources":["./declarations/src/index.d.ts"],"names":[],"mappings":"AAAA"}
|
package/dist/xstate-react.cjs.js
CHANGED
|
@@ -1,7 +1,190 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var React = require('react');
|
|
6
|
+
var shim = require('use-sync-external-store/shim');
|
|
7
|
+
var xstate = require('xstate');
|
|
8
|
+
var useIsomorphicLayoutEffect = require('use-isomorphic-layout-effect');
|
|
9
|
+
var withSelector = require('use-sync-external-store/shim/with-selector');
|
|
10
|
+
|
|
11
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
|
|
12
|
+
|
|
13
|
+
function _interopNamespace(e) {
|
|
14
|
+
if (e && e.__esModule) return e;
|
|
15
|
+
var n = Object.create(null);
|
|
16
|
+
if (e) {
|
|
17
|
+
Object.keys(e).forEach(function (k) {
|
|
18
|
+
if (k !== 'default') {
|
|
19
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
20
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
21
|
+
enumerable: true,
|
|
22
|
+
get: function () { return e[k]; }
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
n["default"] = e;
|
|
28
|
+
return Object.freeze(n);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
var React__namespace = /*#__PURE__*/_interopNamespace(React);
|
|
32
|
+
var useIsomorphicLayoutEffect__default = /*#__PURE__*/_interopDefault(useIsomorphicLayoutEffect);
|
|
33
|
+
|
|
34
|
+
function useConstant(fn) {
|
|
35
|
+
const ref = React__namespace.useRef();
|
|
36
|
+
if (!ref.current) {
|
|
37
|
+
ref.current = {
|
|
38
|
+
v: fn()
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
return ref.current.v;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function useIdleInterpreter(machine, options) {
|
|
45
|
+
const actorRef = useConstant(() => {
|
|
46
|
+
return xstate.createActor(machine, options);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
// TODO: consider using `useAsapEffect` that would do this in `useInsertionEffect` is that's available
|
|
50
|
+
useIsomorphicLayoutEffect__default["default"](() => {
|
|
51
|
+
actorRef.logic.implementations = machine.implementations;
|
|
52
|
+
});
|
|
53
|
+
return actorRef;
|
|
54
|
+
}
|
|
55
|
+
function useActorRef(machine, ...[options = {}, observerOrListener]) {
|
|
56
|
+
const actorRef = useIdleInterpreter(machine, options);
|
|
57
|
+
React.useEffect(() => {
|
|
58
|
+
if (!observerOrListener) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
let sub = actorRef.subscribe(xstate.toObserver(observerOrListener));
|
|
62
|
+
return () => {
|
|
63
|
+
sub.unsubscribe();
|
|
64
|
+
};
|
|
65
|
+
}, [observerOrListener]);
|
|
66
|
+
React.useEffect(() => {
|
|
67
|
+
actorRef.start();
|
|
68
|
+
return () => {
|
|
69
|
+
actorRef.stop();
|
|
70
|
+
actorRef.status = xstate.ActorStatus.NotStarted;
|
|
71
|
+
actorRef._initState();
|
|
72
|
+
};
|
|
73
|
+
}, []);
|
|
74
|
+
return actorRef;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function useActor(logic, options = {}) {
|
|
78
|
+
const actorRef = useIdleInterpreter(logic, options);
|
|
79
|
+
const getSnapshot = React.useCallback(() => {
|
|
80
|
+
return actorRef.getSnapshot();
|
|
81
|
+
}, [actorRef]);
|
|
82
|
+
const subscribe = React.useCallback(handleStoreChange => {
|
|
83
|
+
const {
|
|
84
|
+
unsubscribe
|
|
85
|
+
} = actorRef.subscribe(handleStoreChange);
|
|
86
|
+
return unsubscribe;
|
|
87
|
+
}, [actorRef]);
|
|
88
|
+
const actorSnapshot = shim.useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
|
|
89
|
+
React.useEffect(() => {
|
|
90
|
+
actorRef.start();
|
|
91
|
+
return () => {
|
|
92
|
+
actorRef.stop();
|
|
93
|
+
actorRef.status = xstate.ActorStatus.NotStarted;
|
|
94
|
+
actorRef._initState();
|
|
95
|
+
};
|
|
96
|
+
}, [actorRef]);
|
|
97
|
+
return [actorSnapshot, actorRef.send, actorRef];
|
|
7
98
|
}
|
|
99
|
+
|
|
100
|
+
function defaultCompare(a, b) {
|
|
101
|
+
return a === b;
|
|
102
|
+
}
|
|
103
|
+
function useSelector(actor, selector, compare = defaultCompare) {
|
|
104
|
+
const subscribe = React.useCallback(handleStoreChange => {
|
|
105
|
+
const {
|
|
106
|
+
unsubscribe
|
|
107
|
+
} = actor.subscribe(handleStoreChange);
|
|
108
|
+
return unsubscribe;
|
|
109
|
+
}, [actor]);
|
|
110
|
+
const boundGetSnapshot = React.useCallback(() => actor.getSnapshot(), [actor]);
|
|
111
|
+
const selectedSnapshot = withSelector.useSyncExternalStoreWithSelector(subscribe, boundGetSnapshot, boundGetSnapshot, selector, compare);
|
|
112
|
+
return selectedSnapshot;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// From https://github.com/reduxjs/react-redux/blob/720f0ba79236cdc3e1115f4ef9a7760a21784b48/src/utils/shallowEqual.ts
|
|
116
|
+
function is(x, y) {
|
|
117
|
+
if (x === y) {
|
|
118
|
+
return x !== 0 || y !== 0 || 1 / x === 1 / y;
|
|
119
|
+
} else {
|
|
120
|
+
return x !== x && y !== y;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
function shallowEqual(objA, objB) {
|
|
124
|
+
if (is(objA, objB)) return true;
|
|
125
|
+
if (typeof objA !== 'object' || objA === null || typeof objB !== 'object' || objB === null) {
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
const keysA = Object.keys(objA);
|
|
129
|
+
const keysB = Object.keys(objB);
|
|
130
|
+
if (keysA.length !== keysB.length) return false;
|
|
131
|
+
for (let i = 0; i < keysA.length; i++) {
|
|
132
|
+
if (!Object.prototype.hasOwnProperty.call(objB, keysA[i]) || !is(objA[keysA[i]], objB[keysA[i]])) {
|
|
133
|
+
return false;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return true;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function createActorContext(actorLogic, interpreterOptions) {
|
|
140
|
+
const ReactContext = /*#__PURE__*/React__namespace.createContext(null);
|
|
141
|
+
const OriginalProvider = ReactContext.Provider;
|
|
142
|
+
function Provider({
|
|
143
|
+
children,
|
|
144
|
+
logic: providedLogic = actorLogic,
|
|
145
|
+
machine,
|
|
146
|
+
options: providedOptions = interpreterOptions
|
|
147
|
+
}) {
|
|
148
|
+
if (machine) {
|
|
149
|
+
throw new Error(`The "machine" prop has been deprecated. Please use "logic" instead.`);
|
|
150
|
+
}
|
|
151
|
+
const actor = useActorRef(providedLogic, providedOptions);
|
|
152
|
+
return /*#__PURE__*/React__namespace.createElement(OriginalProvider, {
|
|
153
|
+
value: actor,
|
|
154
|
+
children
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// TODO: add properties to actor ref to make more descriptive
|
|
159
|
+
Provider.displayName = `ActorProvider`;
|
|
160
|
+
function useContext() {
|
|
161
|
+
const actor = React__namespace.useContext(ReactContext);
|
|
162
|
+
if (!actor) {
|
|
163
|
+
throw new Error(`You used a hook from "${Provider.displayName}" but it's not inside a <${Provider.displayName}> component.`);
|
|
164
|
+
}
|
|
165
|
+
return actor;
|
|
166
|
+
}
|
|
167
|
+
function useSelector$1(selector, compare) {
|
|
168
|
+
const actor = useContext();
|
|
169
|
+
return useSelector(actor, selector, compare);
|
|
170
|
+
}
|
|
171
|
+
return {
|
|
172
|
+
Provider: Provider,
|
|
173
|
+
useActorRef: useContext,
|
|
174
|
+
useSelector: useSelector$1
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* @alias useActor
|
|
180
|
+
*/
|
|
181
|
+
function useMachine(machine, options = {}) {
|
|
182
|
+
return useActor(machine, options);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
exports.createActorContext = createActorContext;
|
|
186
|
+
exports.shallowEqual = shallowEqual;
|
|
187
|
+
exports.useActor = useActor;
|
|
188
|
+
exports.useActorRef = useActorRef;
|
|
189
|
+
exports.useMachine = useMachine;
|
|
190
|
+
exports.useSelector = useSelector;
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var React = require('react');
|
|
6
|
+
var shim = require('use-sync-external-store/shim');
|
|
7
|
+
var xstate = require('xstate');
|
|
8
|
+
var useIsomorphicLayoutEffect = require('use-isomorphic-layout-effect');
|
|
9
|
+
var withSelector = require('use-sync-external-store/shim/with-selector');
|
|
10
|
+
|
|
11
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
|
|
12
|
+
|
|
13
|
+
function _interopNamespace(e) {
|
|
14
|
+
if (e && e.__esModule) return e;
|
|
15
|
+
var n = Object.create(null);
|
|
16
|
+
if (e) {
|
|
17
|
+
Object.keys(e).forEach(function (k) {
|
|
18
|
+
if (k !== 'default') {
|
|
19
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
20
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
21
|
+
enumerable: true,
|
|
22
|
+
get: function () { return e[k]; }
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
n["default"] = e;
|
|
28
|
+
return Object.freeze(n);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
var React__namespace = /*#__PURE__*/_interopNamespace(React);
|
|
32
|
+
var useIsomorphicLayoutEffect__default = /*#__PURE__*/_interopDefault(useIsomorphicLayoutEffect);
|
|
33
|
+
|
|
34
|
+
function useConstant(fn) {
|
|
35
|
+
const ref = React__namespace.useRef();
|
|
36
|
+
if (!ref.current) {
|
|
37
|
+
ref.current = {
|
|
38
|
+
v: fn()
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
return ref.current.v;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function useIdleInterpreter(machine, options) {
|
|
45
|
+
{
|
|
46
|
+
const [initialMachine] = React.useState(machine);
|
|
47
|
+
if (machine.config !== initialMachine.config) {
|
|
48
|
+
console.warn(`Actor logic has changed between renders. This is not supported and may lead to invalid snapshots.`);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
const actorRef = useConstant(() => {
|
|
52
|
+
return xstate.createActor(machine, options);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
// TODO: consider using `useAsapEffect` that would do this in `useInsertionEffect` is that's available
|
|
56
|
+
useIsomorphicLayoutEffect__default["default"](() => {
|
|
57
|
+
actorRef.logic.implementations = machine.implementations;
|
|
58
|
+
});
|
|
59
|
+
return actorRef;
|
|
60
|
+
}
|
|
61
|
+
function useActorRef(machine, ...[options = {}, observerOrListener]) {
|
|
62
|
+
const actorRef = useIdleInterpreter(machine, options);
|
|
63
|
+
React.useEffect(() => {
|
|
64
|
+
if (!observerOrListener) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
let sub = actorRef.subscribe(xstate.toObserver(observerOrListener));
|
|
68
|
+
return () => {
|
|
69
|
+
sub.unsubscribe();
|
|
70
|
+
};
|
|
71
|
+
}, [observerOrListener]);
|
|
72
|
+
React.useEffect(() => {
|
|
73
|
+
actorRef.start();
|
|
74
|
+
return () => {
|
|
75
|
+
actorRef.stop();
|
|
76
|
+
actorRef.status = xstate.ActorStatus.NotStarted;
|
|
77
|
+
actorRef._initState();
|
|
78
|
+
};
|
|
79
|
+
}, []);
|
|
80
|
+
return actorRef;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function useActor(logic, options = {}) {
|
|
84
|
+
if (!!logic && 'send' in logic && typeof logic.send === 'function') {
|
|
85
|
+
throw new Error(`useActor() expects actor logic (e.g. a machine), but received an ActorRef. Use the useSelector(actorRef, ...) hook instead to read the ActorRef's snapshot.`);
|
|
86
|
+
}
|
|
87
|
+
const actorRef = useIdleInterpreter(logic, options);
|
|
88
|
+
const getSnapshot = React.useCallback(() => {
|
|
89
|
+
return actorRef.getSnapshot();
|
|
90
|
+
}, [actorRef]);
|
|
91
|
+
const subscribe = React.useCallback(handleStoreChange => {
|
|
92
|
+
const {
|
|
93
|
+
unsubscribe
|
|
94
|
+
} = actorRef.subscribe(handleStoreChange);
|
|
95
|
+
return unsubscribe;
|
|
96
|
+
}, [actorRef]);
|
|
97
|
+
const actorSnapshot = shim.useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
|
|
98
|
+
React.useEffect(() => {
|
|
99
|
+
actorRef.start();
|
|
100
|
+
return () => {
|
|
101
|
+
actorRef.stop();
|
|
102
|
+
actorRef.status = xstate.ActorStatus.NotStarted;
|
|
103
|
+
actorRef._initState();
|
|
104
|
+
};
|
|
105
|
+
}, [actorRef]);
|
|
106
|
+
return [actorSnapshot, actorRef.send, actorRef];
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function defaultCompare(a, b) {
|
|
110
|
+
return a === b;
|
|
111
|
+
}
|
|
112
|
+
function useSelector(actor, selector, compare = defaultCompare) {
|
|
113
|
+
const subscribe = React.useCallback(handleStoreChange => {
|
|
114
|
+
const {
|
|
115
|
+
unsubscribe
|
|
116
|
+
} = actor.subscribe(handleStoreChange);
|
|
117
|
+
return unsubscribe;
|
|
118
|
+
}, [actor]);
|
|
119
|
+
const boundGetSnapshot = React.useCallback(() => actor.getSnapshot(), [actor]);
|
|
120
|
+
const selectedSnapshot = withSelector.useSyncExternalStoreWithSelector(subscribe, boundGetSnapshot, boundGetSnapshot, selector, compare);
|
|
121
|
+
return selectedSnapshot;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// From https://github.com/reduxjs/react-redux/blob/720f0ba79236cdc3e1115f4ef9a7760a21784b48/src/utils/shallowEqual.ts
|
|
125
|
+
function is(x, y) {
|
|
126
|
+
if (x === y) {
|
|
127
|
+
return x !== 0 || y !== 0 || 1 / x === 1 / y;
|
|
128
|
+
} else {
|
|
129
|
+
return x !== x && y !== y;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
function shallowEqual(objA, objB) {
|
|
133
|
+
if (is(objA, objB)) return true;
|
|
134
|
+
if (typeof objA !== 'object' || objA === null || typeof objB !== 'object' || objB === null) {
|
|
135
|
+
return false;
|
|
136
|
+
}
|
|
137
|
+
const keysA = Object.keys(objA);
|
|
138
|
+
const keysB = Object.keys(objB);
|
|
139
|
+
if (keysA.length !== keysB.length) return false;
|
|
140
|
+
for (let i = 0; i < keysA.length; i++) {
|
|
141
|
+
if (!Object.prototype.hasOwnProperty.call(objB, keysA[i]) || !is(objA[keysA[i]], objB[keysA[i]])) {
|
|
142
|
+
return false;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return true;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function createActorContext(actorLogic, interpreterOptions) {
|
|
149
|
+
const ReactContext = /*#__PURE__*/React__namespace.createContext(null);
|
|
150
|
+
const OriginalProvider = ReactContext.Provider;
|
|
151
|
+
function Provider({
|
|
152
|
+
children,
|
|
153
|
+
logic: providedLogic = actorLogic,
|
|
154
|
+
machine,
|
|
155
|
+
options: providedOptions = interpreterOptions
|
|
156
|
+
}) {
|
|
157
|
+
if (machine) {
|
|
158
|
+
throw new Error(`The "machine" prop has been deprecated. Please use "logic" instead.`);
|
|
159
|
+
}
|
|
160
|
+
const actor = useActorRef(providedLogic, providedOptions);
|
|
161
|
+
return /*#__PURE__*/React__namespace.createElement(OriginalProvider, {
|
|
162
|
+
value: actor,
|
|
163
|
+
children
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// TODO: add properties to actor ref to make more descriptive
|
|
168
|
+
Provider.displayName = `ActorProvider`;
|
|
169
|
+
function useContext() {
|
|
170
|
+
const actor = React__namespace.useContext(ReactContext);
|
|
171
|
+
if (!actor) {
|
|
172
|
+
throw new Error(`You used a hook from "${Provider.displayName}" but it's not inside a <${Provider.displayName}> component.`);
|
|
173
|
+
}
|
|
174
|
+
return actor;
|
|
175
|
+
}
|
|
176
|
+
function useSelector$1(selector, compare) {
|
|
177
|
+
const actor = useContext();
|
|
178
|
+
return useSelector(actor, selector, compare);
|
|
179
|
+
}
|
|
180
|
+
return {
|
|
181
|
+
Provider: Provider,
|
|
182
|
+
useActorRef: useContext,
|
|
183
|
+
useSelector: useSelector$1
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* @alias useActor
|
|
189
|
+
*/
|
|
190
|
+
function useMachine(machine, options = {}) {
|
|
191
|
+
return useActor(machine, options);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
exports.createActorContext = createActorContext;
|
|
195
|
+
exports.shallowEqual = shallowEqual;
|
|
196
|
+
exports.useActor = useActor;
|
|
197
|
+
exports.useActorRef = useActorRef;
|
|
198
|
+
exports.useMachine = useMachine;
|
|
199
|
+
exports.useSelector = useSelector;
|