@xstate/react 2.0.0-pr2674-2021926101023 → 3.0.0
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/CHANGELOG.md +65 -28
- package/dist/xstate-react-fsm.umd.min.js +41 -15
- package/dist/xstate-react.umd.min.js +34 -23
- package/es/fsm.d.ts +3 -19
- package/es/fsm.js +35 -26
- package/es/index.d.ts +1 -2
- package/es/index.js +1 -2
- package/es/types.d.ts +1 -16
- package/es/types.js +1 -5
- package/es/useActor.js +13 -32
- package/es/useInterpret.d.ts +6 -5
- package/es/useInterpret.js +32 -43
- package/es/useMachine.d.ts +5 -7
- package/es/useMachine.js +39 -44
- package/es/useSelector.js +12 -48
- package/es/useSpawn.js +1 -1
- package/es/utils.d.ts +2 -0
- package/es/utils.js +3 -0
- package/lib/fsm.d.ts +3 -19
- package/lib/fsm.js +36 -27
- package/lib/index.d.ts +1 -2
- package/lib/index.js +1 -5
- package/lib/types.d.ts +1 -16
- package/lib/types.js +0 -6
- package/lib/useActor.js +16 -35
- package/lib/useInterpret.d.ts +6 -5
- package/lib/useInterpret.js +35 -45
- package/lib/useMachine.d.ts +5 -7
- package/lib/useMachine.js +37 -44
- package/lib/useSelector.js +13 -49
- package/lib/useSpawn.js +3 -3
- package/lib/utils.d.ts +2 -0
- package/lib/utils.js +5 -1
- package/package.json +17 -16
- package/dist/xstate-react.cjs.js +0 -16
- package/es/useReactEffectActions.d.ts +0 -3
- package/es/useReactEffectActions.js +0 -72
- package/es/useService.d.ts +0 -16
- package/es/useService.js +0 -36
- package/lib/useReactEffectActions.d.ts +0 -3
- package/lib/useReactEffectActions.js +0 -76
- package/lib/useService.d.ts +0 -16
- package/lib/useService.js +0 -41
package/lib/useActor.js
CHANGED
|
@@ -1,25 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __read = (this && this.__read) || function (o, n) {
|
|
3
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
4
|
-
if (!m) return o;
|
|
5
|
-
var i = m.call(o), r, ar = [], e;
|
|
6
|
-
try {
|
|
7
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
8
|
-
}
|
|
9
|
-
catch (error) { e = { error: error }; }
|
|
10
|
-
finally {
|
|
11
|
-
try {
|
|
12
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
13
|
-
}
|
|
14
|
-
finally { if (e) throw e.error; }
|
|
15
|
-
}
|
|
16
|
-
return ar;
|
|
17
|
-
};
|
|
18
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
19
3
|
exports.useActor = exports.isActorWithState = void 0;
|
|
20
4
|
var react_1 = require("react");
|
|
21
5
|
var use_isomorphic_layout_effect_1 = require("use-isomorphic-layout-effect");
|
|
22
6
|
var useConstant_1 = require("./useConstant");
|
|
7
|
+
var shim_1 = require("use-sync-external-store/shim");
|
|
23
8
|
function isActorWithState(actorRef) {
|
|
24
9
|
return 'state' in actorRef;
|
|
25
10
|
}
|
|
@@ -27,9 +12,6 @@ exports.isActorWithState = isActorWithState;
|
|
|
27
12
|
function isDeferredActor(actorRef) {
|
|
28
13
|
return 'deferred' in actorRef;
|
|
29
14
|
}
|
|
30
|
-
var noop = function () {
|
|
31
|
-
/* ... */
|
|
32
|
-
};
|
|
33
15
|
function defaultGetSnapshot(actorRef) {
|
|
34
16
|
return 'getSnapshot' in actorRef
|
|
35
17
|
? actorRef.getSnapshot()
|
|
@@ -39,17 +21,25 @@ function defaultGetSnapshot(actorRef) {
|
|
|
39
21
|
}
|
|
40
22
|
function useActor(actorRef, getSnapshot) {
|
|
41
23
|
if (getSnapshot === void 0) { getSnapshot = defaultGetSnapshot; }
|
|
42
|
-
var actorRefRef = react_1.useRef(actorRef);
|
|
43
|
-
var deferredEventsRef = react_1.useRef([]);
|
|
44
|
-
var
|
|
45
|
-
|
|
24
|
+
var actorRefRef = (0, react_1.useRef)(actorRef);
|
|
25
|
+
var deferredEventsRef = (0, react_1.useRef)([]);
|
|
26
|
+
var subscribe = (0, react_1.useCallback)(function (handleStoreChange) {
|
|
27
|
+
var unsubscribe = actorRef.subscribe(handleStoreChange).unsubscribe;
|
|
28
|
+
return unsubscribe;
|
|
29
|
+
}, [actorRef]);
|
|
30
|
+
var boundGetSnapshot = (0, react_1.useCallback)(function () { return getSnapshot(actorRef); }, [
|
|
31
|
+
actorRef,
|
|
32
|
+
getSnapshot
|
|
33
|
+
]);
|
|
34
|
+
var storeSnapshot = (0, shim_1.useSyncExternalStore)(subscribe, boundGetSnapshot, boundGetSnapshot);
|
|
35
|
+
var send = (0, useConstant_1.default)(function () { return function () {
|
|
46
36
|
var args = [];
|
|
47
37
|
for (var _i = 0; _i < arguments.length; _i++) {
|
|
48
38
|
args[_i] = arguments[_i];
|
|
49
39
|
}
|
|
50
40
|
var event = args[0];
|
|
51
41
|
if (process.env.NODE_ENV !== 'production' && args.length > 1) {
|
|
52
|
-
console.warn("Unexpected payload: "
|
|
42
|
+
console.warn("Unexpected payload: ".concat(JSON.stringify(args[1]), ". Only a single event object can be sent to actor send() functions."));
|
|
53
43
|
}
|
|
54
44
|
var currentActorRef = actorRefRef.current;
|
|
55
45
|
// If the previous actor is a deferred actor,
|
|
@@ -62,23 +52,14 @@ function useActor(actorRef, getSnapshot) {
|
|
|
62
52
|
currentActorRef.send(event);
|
|
63
53
|
}
|
|
64
54
|
}; });
|
|
65
|
-
use_isomorphic_layout_effect_1.default(function () {
|
|
55
|
+
(0, use_isomorphic_layout_effect_1.default)(function () {
|
|
66
56
|
actorRefRef.current = actorRef;
|
|
67
|
-
setCurrent(getSnapshot(actorRef));
|
|
68
|
-
var subscription = actorRef.subscribe({
|
|
69
|
-
next: function (emitted) { return setCurrent(emitted); },
|
|
70
|
-
error: noop,
|
|
71
|
-
complete: noop
|
|
72
|
-
});
|
|
73
57
|
// Dequeue deferred events from the previous deferred actorRef
|
|
74
58
|
while (deferredEventsRef.current.length > 0) {
|
|
75
59
|
var deferredEvent = deferredEventsRef.current.shift();
|
|
76
60
|
actorRef.send(deferredEvent);
|
|
77
61
|
}
|
|
78
|
-
return function () {
|
|
79
|
-
subscription.unsubscribe();
|
|
80
|
-
};
|
|
81
62
|
}, [actorRef]);
|
|
82
|
-
return [
|
|
63
|
+
return [storeSnapshot, send];
|
|
83
64
|
}
|
|
84
65
|
exports.useActor = useActor;
|
package/lib/useInterpret.d.ts
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
|
-
import { AreAllImplementationsAssumedToBeProvided, InternalMachineOptions, InterpreterFrom, InterpreterOptions,
|
|
1
|
+
import { AnyInterpreter, AnyStateMachine, AreAllImplementationsAssumedToBeProvided, InternalMachineOptions, InterpreterFrom, InterpreterOptions, MachineOptions, Observer, StateFrom } from 'xstate';
|
|
2
2
|
import { MaybeLazy } from './types';
|
|
3
3
|
import { UseMachineOptions } from './useMachine';
|
|
4
|
-
declare
|
|
4
|
+
export declare function useIdleInterpreter(getMachine: MaybeLazy<AnyStateMachine>, options: Partial<InterpreterOptions> & Partial<UseMachineOptions<unknown, never>> & Partial<MachineOptions<unknown, never>>): AnyInterpreter;
|
|
5
|
+
declare type RestParams<TMachine extends AnyStateMachine> = AreAllImplementationsAssumedToBeProvided<TMachine['__TResolvedTypesMeta']> extends false ? [
|
|
5
6
|
options: InterpreterOptions & UseMachineOptions<TMachine['__TContext'], TMachine['__TEvent']> & InternalMachineOptions<TMachine['__TContext'], TMachine['__TEvent'], TMachine['__TResolvedTypesMeta'], true>,
|
|
6
|
-
observerOrListener?: Observer<
|
|
7
|
+
observerOrListener?: Observer<StateFrom<TMachine>> | ((value: StateFrom<TMachine>) => void)
|
|
7
8
|
] : [
|
|
8
9
|
options?: InterpreterOptions & UseMachineOptions<TMachine['__TContext'], TMachine['__TEvent']> & InternalMachineOptions<TMachine['__TContext'], TMachine['__TEvent'], TMachine['__TResolvedTypesMeta']>,
|
|
9
|
-
observerOrListener?: Observer<
|
|
10
|
+
observerOrListener?: Observer<StateFrom<TMachine>> | ((value: StateFrom<TMachine>) => void)
|
|
10
11
|
];
|
|
11
|
-
export declare function useInterpret<TMachine extends
|
|
12
|
+
export declare function useInterpret<TMachine extends AnyStateMachine>(getMachine: MaybeLazy<TMachine>, ...[options, observerOrListener]: RestParams<TMachine>): InterpreterFrom<TMachine>;
|
|
12
13
|
export {};
|
|
13
14
|
//# sourceMappingURL=useInterpret.d.ts.map
|
package/lib/useInterpret.js
CHANGED
|
@@ -38,46 +38,25 @@ var __read = (this && this.__read) || function (o, n) {
|
|
|
38
38
|
return ar;
|
|
39
39
|
};
|
|
40
40
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
41
|
-
exports.useInterpret = void 0;
|
|
41
|
+
exports.useInterpret = exports.useIdleInterpreter = void 0;
|
|
42
42
|
var react_1 = require("react");
|
|
43
43
|
var use_isomorphic_layout_effect_1 = require("use-isomorphic-layout-effect");
|
|
44
44
|
var xstate_1 = require("xstate");
|
|
45
45
|
var useConstant_1 = require("./useConstant");
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
// it avoids a breaking change between this package and XState which is its peer dep
|
|
49
|
-
function toObserver(nextHandler, errorHandler, completionHandler) {
|
|
50
|
-
if (typeof nextHandler === 'object') {
|
|
51
|
-
return nextHandler;
|
|
52
|
-
}
|
|
53
|
-
var noop = function () { return void 0; };
|
|
54
|
-
return {
|
|
55
|
-
next: nextHandler,
|
|
56
|
-
error: errorHandler || noop,
|
|
57
|
-
complete: completionHandler || noop
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
function useInterpret(getMachine) {
|
|
61
|
-
var _a = [];
|
|
62
|
-
for (var _i = 1; _i < arguments.length; _i++) {
|
|
63
|
-
_a[_i - 1] = arguments[_i];
|
|
64
|
-
}
|
|
65
|
-
var _b = __read(_a, 2), _c = _b[0], options = _c === void 0 ? {} : _c, observerOrListener = _b[1];
|
|
66
|
-
var machine = useConstant_1.default(function () {
|
|
46
|
+
function useIdleInterpreter(getMachine, options) {
|
|
47
|
+
var machine = (0, useConstant_1.default)(function () {
|
|
67
48
|
return typeof getMachine === 'function' ? getMachine() : getMachine;
|
|
68
49
|
});
|
|
69
50
|
if (process.env.NODE_ENV !== 'production' &&
|
|
70
51
|
typeof getMachine !== 'function') {
|
|
71
|
-
var
|
|
52
|
+
var _a = __read((0, react_1.useState)(machine), 1), initialMachine = _a[0];
|
|
72
53
|
if (getMachine !== initialMachine) {
|
|
73
54
|
console.warn('Machine given to `useMachine` has changed between renders. This is not supported and might lead to unexpected results.\n' +
|
|
74
55
|
'Please make sure that you pass the same Machine as argument each time.');
|
|
75
56
|
}
|
|
76
57
|
}
|
|
77
|
-
var context = options.context, guards = options.guards, actions = options.actions, services = options.services, delays = options.delays, rehydratedState = options.state, interpreterOptions = __rest(options, ["context", "guards", "actions", "services", "delays", "state"]);
|
|
78
|
-
|
|
79
|
-
var activities = options.activities;
|
|
80
|
-
var service = useConstant_1.default(function () {
|
|
58
|
+
var context = options.context, guards = options.guards, actions = options.actions, activities = options.activities, services = options.services, delays = options.delays, rehydratedState = options.state, interpreterOptions = __rest(options, ["context", "guards", "actions", "activities", "services", "delays", "state"]);
|
|
59
|
+
var service = (0, useConstant_1.default)(function () {
|
|
81
60
|
var machineConfig = {
|
|
82
61
|
context: context,
|
|
83
62
|
guards: guards,
|
|
@@ -87,34 +66,45 @@ function useInterpret(getMachine) {
|
|
|
87
66
|
delays: delays
|
|
88
67
|
};
|
|
89
68
|
var machineWithConfig = machine.withConfig(machineConfig, function () { return (__assign(__assign({}, machine.context), context)); });
|
|
90
|
-
return xstate_1.interpret(machineWithConfig,
|
|
69
|
+
return (0, xstate_1.interpret)(machineWithConfig, interpreterOptions);
|
|
91
70
|
});
|
|
92
|
-
use_isomorphic_layout_effect_1.default(function () {
|
|
93
|
-
var sub;
|
|
94
|
-
if (observerOrListener) {
|
|
95
|
-
sub = service.subscribe(toObserver(observerOrListener));
|
|
96
|
-
}
|
|
97
|
-
return function () {
|
|
98
|
-
sub === null || sub === void 0 ? void 0 : sub.unsubscribe();
|
|
99
|
-
};
|
|
100
|
-
}, [observerOrListener]);
|
|
101
|
-
use_isomorphic_layout_effect_1.default(function () {
|
|
102
|
-
service.start(rehydratedState ? xstate_1.State.create(rehydratedState) : undefined);
|
|
103
|
-
return function () {
|
|
104
|
-
service.stop();
|
|
105
|
-
};
|
|
106
|
-
}, []);
|
|
107
71
|
// Make sure options are kept updated when they change.
|
|
108
72
|
// This mutation assignment is safe because the service instance is only used
|
|
109
73
|
// in one place -- this hook's caller.
|
|
110
|
-
use_isomorphic_layout_effect_1.default(function () {
|
|
74
|
+
(0, use_isomorphic_layout_effect_1.default)(function () {
|
|
111
75
|
Object.assign(service.machine.options.actions, actions);
|
|
112
76
|
Object.assign(service.machine.options.guards, guards);
|
|
113
77
|
Object.assign(service.machine.options.activities, activities);
|
|
114
78
|
Object.assign(service.machine.options.services, services);
|
|
115
79
|
Object.assign(service.machine.options.delays, delays);
|
|
116
80
|
}, [actions, guards, activities, services, delays]);
|
|
117
|
-
|
|
81
|
+
return service;
|
|
82
|
+
}
|
|
83
|
+
exports.useIdleInterpreter = useIdleInterpreter;
|
|
84
|
+
function useInterpret(getMachine) {
|
|
85
|
+
var _a = [];
|
|
86
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
|
87
|
+
_a[_i - 1] = arguments[_i];
|
|
88
|
+
}
|
|
89
|
+
var _b = __read(_a, 2), _c = _b[0], options = _c === void 0 ? {} : _c, observerOrListener = _b[1];
|
|
90
|
+
var service = useIdleInterpreter(getMachine, options);
|
|
91
|
+
(0, react_1.useEffect)(function () {
|
|
92
|
+
if (!observerOrListener) {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
var sub = service.subscribe((0, xstate_1.toObserver)(observerOrListener));
|
|
96
|
+
return function () {
|
|
97
|
+
sub.unsubscribe();
|
|
98
|
+
};
|
|
99
|
+
}, [observerOrListener]);
|
|
100
|
+
(0, react_1.useEffect)(function () {
|
|
101
|
+
var rehydratedState = options.state;
|
|
102
|
+
service.start(rehydratedState ? xstate_1.State.create(rehydratedState) : undefined);
|
|
103
|
+
return function () {
|
|
104
|
+
service.stop();
|
|
105
|
+
service.status = xstate_1.InterpreterStatus.NotStarted;
|
|
106
|
+
};
|
|
107
|
+
}, []);
|
|
118
108
|
return service;
|
|
119
109
|
}
|
|
120
110
|
exports.useInterpret = useInterpret;
|
package/lib/useMachine.d.ts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { MaybeLazy, Prop
|
|
3
|
-
export declare function asEffect<TContext, TEvent extends EventObject>(exec: ActionFunction<TContext, TEvent>): ReactActionFunction<TContext, TEvent>;
|
|
4
|
-
export declare function asLayoutEffect<TContext, TEvent extends EventObject>(exec: ActionFunction<TContext, TEvent>): ReactActionFunction<TContext, TEvent>;
|
|
1
|
+
import { AnyStateMachine, AreAllImplementationsAssumedToBeProvided, EventObject, InternalMachineOptions, InterpreterFrom, InterpreterOptions, StateConfig, StateFrom } from 'xstate';
|
|
2
|
+
import { MaybeLazy, Prop } from './types';
|
|
5
3
|
export interface UseMachineOptions<TContext, TEvent extends EventObject> {
|
|
6
4
|
/**
|
|
7
5
|
* If provided, will be merged with machine's `context`.
|
|
@@ -13,12 +11,12 @@ export interface UseMachineOptions<TContext, TEvent extends EventObject> {
|
|
|
13
11
|
*/
|
|
14
12
|
state?: StateConfig<TContext, TEvent>;
|
|
15
13
|
}
|
|
16
|
-
declare type RestParams<TMachine extends
|
|
14
|
+
declare type RestParams<TMachine extends AnyStateMachine> = AreAllImplementationsAssumedToBeProvided<TMachine['__TResolvedTypesMeta']> extends false ? [
|
|
17
15
|
options: InterpreterOptions & UseMachineOptions<TMachine['__TContext'], TMachine['__TEvent']> & InternalMachineOptions<TMachine['__TContext'], TMachine['__TEvent'], TMachine['__TResolvedTypesMeta'], true>
|
|
18
16
|
] : [
|
|
19
17
|
options?: InterpreterOptions & UseMachineOptions<TMachine['__TContext'], TMachine['__TEvent']> & InternalMachineOptions<TMachine['__TContext'], TMachine['__TEvent'], TMachine['__TResolvedTypesMeta']>
|
|
20
18
|
];
|
|
21
|
-
declare type UseMachineReturn<TMachine extends
|
|
22
|
-
export declare function useMachine<TMachine extends
|
|
19
|
+
declare type UseMachineReturn<TMachine extends AnyStateMachine, TInterpreter = InterpreterFrom<TMachine>> = [StateFrom<TMachine>, Prop<TInterpreter, 'send'>, TInterpreter];
|
|
20
|
+
export declare function useMachine<TMachine extends AnyStateMachine>(getMachine: MaybeLazy<TMachine>, ...[options]: RestParams<TMachine>): UseMachineReturn<TMachine>;
|
|
23
21
|
export {};
|
|
24
22
|
//# sourceMappingURL=useMachine.d.ts.map
|
package/lib/useMachine.js
CHANGED
|
@@ -15,66 +15,59 @@ var __read = (this && this.__read) || function (o, n) {
|
|
|
15
15
|
}
|
|
16
16
|
return ar;
|
|
17
17
|
};
|
|
18
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from) {
|
|
19
|
-
for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
|
|
20
|
-
to[j] = from[i];
|
|
21
|
-
return to;
|
|
22
|
-
};
|
|
23
18
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
24
|
-
exports.useMachine =
|
|
19
|
+
exports.useMachine = void 0;
|
|
25
20
|
var react_1 = require("react");
|
|
21
|
+
var with_selector_1 = require("use-sync-external-store/shim/with-selector");
|
|
26
22
|
var xstate_1 = require("xstate");
|
|
27
|
-
var types_1 = require("./types");
|
|
28
23
|
var useInterpret_1 = require("./useInterpret");
|
|
29
|
-
function
|
|
30
|
-
|
|
31
|
-
var args = [];
|
|
32
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
33
|
-
args[_i] = arguments[_i];
|
|
34
|
-
}
|
|
35
|
-
// don't execute; just return
|
|
36
|
-
return function () {
|
|
37
|
-
return exec.apply(void 0, __spreadArray([], __read(args)));
|
|
38
|
-
};
|
|
39
|
-
};
|
|
40
|
-
Object.defineProperties(effectExec, {
|
|
41
|
-
name: { value: "effect:" + exec.name },
|
|
42
|
-
__effect: { value: tag }
|
|
43
|
-
});
|
|
44
|
-
return effectExec;
|
|
45
|
-
}
|
|
46
|
-
function asEffect(exec) {
|
|
47
|
-
return createReactActionFunction(exec, types_1.ReactEffectType.Effect);
|
|
48
|
-
}
|
|
49
|
-
exports.asEffect = asEffect;
|
|
50
|
-
function asLayoutEffect(exec) {
|
|
51
|
-
return createReactActionFunction(exec, types_1.ReactEffectType.LayoutEffect);
|
|
24
|
+
function identity(a) {
|
|
25
|
+
return a;
|
|
52
26
|
}
|
|
53
|
-
exports.asLayoutEffect = asLayoutEffect;
|
|
54
27
|
function useMachine(getMachine) {
|
|
55
28
|
var _a = [];
|
|
56
29
|
for (var _i = 1; _i < arguments.length; _i++) {
|
|
57
30
|
_a[_i - 1] = arguments[_i];
|
|
58
31
|
}
|
|
59
32
|
var _b = __read(_a, 1), _c = _b[0], options = _c === void 0 ? {} : _c;
|
|
60
|
-
|
|
33
|
+
// using `useIdleInterpreter` allows us to subscribe to the service *before* we start it
|
|
34
|
+
// so we don't miss any notifications
|
|
35
|
+
var service = (0, useInterpret_1.useIdleInterpreter)(getMachine, options);
|
|
36
|
+
var getSnapshot = (0, react_1.useCallback)(function () {
|
|
37
|
+
if (service.status === xstate_1.InterpreterStatus.NotStarted) {
|
|
38
|
+
return (options.state
|
|
39
|
+
? xstate_1.State.create(options.state)
|
|
40
|
+
: service.machine.initialState);
|
|
41
|
+
}
|
|
42
|
+
return service.state;
|
|
43
|
+
}, [service]);
|
|
44
|
+
var isEqual = (0, react_1.useCallback)(function (prevState, nextState) {
|
|
45
|
+
if (service.status === xstate_1.InterpreterStatus.NotStarted) {
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
61
48
|
// Only change the current state if:
|
|
62
49
|
// - the incoming state is the "live" initial state (since it might have new actors)
|
|
63
50
|
// - OR the incoming state actually changed.
|
|
64
51
|
//
|
|
65
52
|
// The "live" initial state will have .changed === undefined.
|
|
66
|
-
var initialStateChanged = nextState.changed === undefined &&
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
53
|
+
var initialStateChanged = nextState.changed === undefined &&
|
|
54
|
+
(Object.keys(nextState.children).length > 0 ||
|
|
55
|
+
typeof prevState.changed === 'boolean');
|
|
56
|
+
return !(nextState.changed || initialStateChanged);
|
|
57
|
+
}, [service]);
|
|
58
|
+
var subscribe = (0, react_1.useCallback)(function (handleStoreChange) {
|
|
59
|
+
var unsubscribe = service.subscribe(handleStoreChange).unsubscribe;
|
|
60
|
+
return unsubscribe;
|
|
61
|
+
}, [service]);
|
|
62
|
+
var storeSnapshot = (0, with_selector_1.useSyncExternalStoreWithSelector)(subscribe, getSnapshot, getSnapshot, identity, isEqual);
|
|
63
|
+
(0, react_1.useEffect)(function () {
|
|
64
|
+
var rehydratedState = options.state;
|
|
65
|
+
service.start(rehydratedState ? xstate_1.State.create(rehydratedState) : undefined);
|
|
66
|
+
return function () {
|
|
67
|
+
service.stop();
|
|
68
|
+
service.status = xstate_1.InterpreterStatus.NotStarted;
|
|
69
|
+
};
|
|
70
70
|
}, []);
|
|
71
|
-
|
|
72
|
-
var _d = __read(react_1.useState(function () {
|
|
73
|
-
var initialState = service.machine.initialState;
|
|
74
|
-
return (options.state
|
|
75
|
-
? xstate_1.State.create(options.state)
|
|
76
|
-
: initialState);
|
|
77
|
-
}), 2), state = _d[0], setState = _d[1];
|
|
78
|
-
return [state, service.send, service];
|
|
71
|
+
return [storeSnapshot, service.send, service];
|
|
79
72
|
}
|
|
80
73
|
exports.useMachine = useMachine;
|
package/lib/useSelector.js
CHANGED
|
@@ -2,68 +2,32 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.useSelector = void 0;
|
|
4
4
|
var react_1 = require("react");
|
|
5
|
-
var
|
|
6
|
-
var use_isomorphic_layout_effect_1 = require("use-isomorphic-layout-effect");
|
|
5
|
+
var with_selector_1 = require("use-sync-external-store/shim/with-selector");
|
|
7
6
|
var useActor_1 = require("./useActor");
|
|
8
|
-
var
|
|
7
|
+
var utils_1 = require("./utils");
|
|
9
8
|
function isService(actor) {
|
|
10
9
|
return 'state' in actor && 'machine' in actor;
|
|
11
10
|
}
|
|
12
11
|
var defaultCompare = function (a, b) { return a === b; };
|
|
13
12
|
var defaultGetSnapshot = function (a) {
|
|
14
13
|
return isService(a)
|
|
15
|
-
?
|
|
16
|
-
: useActor_1.isActorWithState(a)
|
|
14
|
+
? (0, utils_1.getServiceSnapshot)(a)
|
|
15
|
+
: (0, useActor_1.isActorWithState)(a)
|
|
17
16
|
? a.state
|
|
18
17
|
: undefined;
|
|
19
18
|
};
|
|
20
19
|
function useSelector(actor, selector, compare, getSnapshot) {
|
|
21
20
|
if (compare === void 0) { compare = defaultCompare; }
|
|
22
21
|
if (getSnapshot === void 0) { getSnapshot = defaultGetSnapshot; }
|
|
23
|
-
var
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
var current = selector(snapshot);
|
|
27
|
-
var notifySubscriber;
|
|
28
|
-
return {
|
|
29
|
-
getSnapshot: function () { return snapshot; },
|
|
30
|
-
getCurrentValue: function () { return current; },
|
|
31
|
-
setCurrentValue: function (newCurrent) {
|
|
32
|
-
current = newCurrent;
|
|
33
|
-
notifySubscriber === null || notifySubscriber === void 0 ? void 0 : notifySubscriber();
|
|
34
|
-
},
|
|
35
|
-
subscribe: function (callback) {
|
|
36
|
-
notifySubscriber = callback;
|
|
37
|
-
var sub = actor.subscribe(function (emitted) {
|
|
38
|
-
snapshot = emitted;
|
|
39
|
-
var next = latestSelectorRef.current(emitted);
|
|
40
|
-
if (!compare(current, next)) {
|
|
41
|
-
current = next;
|
|
42
|
-
callback();
|
|
43
|
-
}
|
|
44
|
-
});
|
|
45
|
-
return function () {
|
|
46
|
-
sub.unsubscribe();
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
};
|
|
50
|
-
// intentionally omit `getSnapshot` and `compare`
|
|
51
|
-
// - `getSnapshot`: it is only supposed to read the "initial" snapshot of an actor
|
|
52
|
-
// - `compare`: is really supposed to be idempotent and the same throughout the lifetime of this hook (the same assumption is made in React Redux v7)
|
|
22
|
+
var subscribe = (0, react_1.useCallback)(function (handleStoreChange) {
|
|
23
|
+
var unsubscribe = actor.subscribe(handleStoreChange).unsubscribe;
|
|
24
|
+
return unsubscribe;
|
|
53
25
|
}, [actor]);
|
|
54
|
-
var
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
}
|
|
61
|
-
use_isomorphic_layout_effect_1.default(function () {
|
|
62
|
-
latestSelectorRef.current = selector;
|
|
63
|
-
// required so we don't cause a rerender by setting state (this could create infinite rerendering loop with inline selectors)
|
|
64
|
-
// at the same time we need to update the value within the subscription so new emits can compare against what has been returned to the user as current value
|
|
65
|
-
subscription.setCurrentValue(currentSelected);
|
|
66
|
-
});
|
|
67
|
-
return currentSelected;
|
|
26
|
+
var boundGetSnapshot = (0, react_1.useCallback)(function () { return getSnapshot(actor); }, [
|
|
27
|
+
actor,
|
|
28
|
+
getSnapshot
|
|
29
|
+
]);
|
|
30
|
+
var selectedSnapshot = (0, with_selector_1.useSyncExternalStoreWithSelector)(subscribe, boundGetSnapshot, boundGetSnapshot, selector, compare);
|
|
31
|
+
return selectedSnapshot;
|
|
68
32
|
}
|
|
69
33
|
exports.useSelector = useSelector;
|
package/lib/useSpawn.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.useSpawn = void 0;
|
|
4
|
-
var
|
|
4
|
+
var xstate_1 = require("xstate");
|
|
5
5
|
var useConstant_1 = require("./useConstant");
|
|
6
6
|
/**
|
|
7
7
|
* React hook that spawns an `ActorRef` with the specified `behavior`.
|
|
@@ -11,8 +11,8 @@ var useConstant_1 = require("./useConstant");
|
|
|
11
11
|
* @returns An ActorRef with the specified `behavior`
|
|
12
12
|
*/
|
|
13
13
|
function useSpawn(behavior) {
|
|
14
|
-
var actorRef = useConstant_1.default(function () {
|
|
15
|
-
return
|
|
14
|
+
var actorRef = (0, useConstant_1.default)(function () {
|
|
15
|
+
return (0, xstate_1.spawnBehavior)(behavior);
|
|
16
16
|
});
|
|
17
17
|
return actorRef;
|
|
18
18
|
}
|
package/lib/utils.d.ts
CHANGED
|
@@ -1,2 +1,4 @@
|
|
|
1
|
+
import { Interpreter } from 'xstate';
|
|
1
2
|
export declare function partition<T, A extends T, B extends T>(items: T[], predicate: (item: T) => item is A): [A[], B[]];
|
|
3
|
+
export declare function getServiceSnapshot<TService extends Interpreter<any, any, any, any>>(service: TService): TService['state'];
|
|
2
4
|
//# sourceMappingURL=utils.d.ts.map
|
package/lib/utils.js
CHANGED
|
@@ -27,7 +27,7 @@ var __values = (this && this.__values) || function(o) {
|
|
|
27
27
|
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
28
28
|
};
|
|
29
29
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
-
exports.partition = void 0;
|
|
30
|
+
exports.getServiceSnapshot = exports.partition = void 0;
|
|
31
31
|
function partition(items, predicate) {
|
|
32
32
|
var e_1, _a;
|
|
33
33
|
var _b = __read([[], []], 2), truthy = _b[0], falsy = _b[1];
|
|
@@ -52,3 +52,7 @@ function partition(items, predicate) {
|
|
|
52
52
|
return [truthy, falsy];
|
|
53
53
|
}
|
|
54
54
|
exports.partition = partition;
|
|
55
|
+
function getServiceSnapshot(service) {
|
|
56
|
+
return service.status !== 0 ? service.state : service.machine.initialState;
|
|
57
|
+
}
|
|
58
|
+
exports.getServiceSnapshot = getServiceSnapshot;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xstate/react",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "XState tools for React",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"state",
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
"hook"
|
|
14
14
|
],
|
|
15
15
|
"author": "David Khourshid <davidkpiano@gmail.com>",
|
|
16
|
-
"homepage": "https://github.com/
|
|
16
|
+
"homepage": "https://github.com/statelyai/xstate/tree/main/packages/xstate-react#readme",
|
|
17
17
|
"license": "MIT",
|
|
18
18
|
"main": "lib/index.js",
|
|
19
19
|
"module": "es/index.js",
|
|
@@ -40,21 +40,21 @@
|
|
|
40
40
|
],
|
|
41
41
|
"repository": {
|
|
42
42
|
"type": "git",
|
|
43
|
-
"url": "git+ssh://git@github.com/
|
|
43
|
+
"url": "git+ssh://git@github.com/statelyai/xstate.git"
|
|
44
44
|
},
|
|
45
45
|
"scripts": {
|
|
46
46
|
"clean": "rm -rf dist lib tsconfig.tsbuildinfo",
|
|
47
47
|
"build": "tsc && tsc --outDir es --module es2015 && rollup -c",
|
|
48
48
|
"test": "jest",
|
|
49
|
-
"
|
|
49
|
+
"prepare": "npm run build"
|
|
50
50
|
},
|
|
51
51
|
"bugs": {
|
|
52
|
-
"url": "https://github.com/
|
|
52
|
+
"url": "https://github.com/statelyai/xstate/issues"
|
|
53
53
|
},
|
|
54
54
|
"peerDependencies": {
|
|
55
|
-
"@xstate/fsm": "^
|
|
56
|
-
"react": "^16.8.0 || ^17.0.0",
|
|
57
|
-
"xstate": "^4.
|
|
55
|
+
"@xstate/fsm": "^2.0.0",
|
|
56
|
+
"react": "^16.8.0 || ^17.0.0 || ^18.0.0",
|
|
57
|
+
"xstate": "^4.31.0"
|
|
58
58
|
},
|
|
59
59
|
"peerDependenciesMeta": {
|
|
60
60
|
"@xstate/fsm": {
|
|
@@ -66,28 +66,29 @@
|
|
|
66
66
|
},
|
|
67
67
|
"dependencies": {
|
|
68
68
|
"use-isomorphic-layout-effect": "^1.0.0",
|
|
69
|
-
"use-
|
|
69
|
+
"use-sync-external-store": "^1.0.0"
|
|
70
70
|
},
|
|
71
71
|
"devDependencies": {
|
|
72
72
|
"@rollup/plugin-commonjs": "^17.0.0",
|
|
73
73
|
"@rollup/plugin-node-resolve": "^11.0.1",
|
|
74
|
-
"@testing-library/react": "^
|
|
74
|
+
"@testing-library/react": "^13.0.0",
|
|
75
75
|
"@types/jsdom": "^12.2.3",
|
|
76
|
-
"@types/react": "^
|
|
77
|
-
"@types/react-dom": "^
|
|
76
|
+
"@types/react": "^17.0.43",
|
|
77
|
+
"@types/react-dom": "^17.0.14",
|
|
78
|
+
"@types/use-sync-external-store": "^0.0.3",
|
|
78
79
|
"@xstate/fsm": "*",
|
|
79
80
|
"jest": "^26.6.3",
|
|
80
81
|
"jsdom": "^14.0.0",
|
|
81
82
|
"jsdom-global": "^3.0.2",
|
|
82
83
|
"lerna-alias": "3.0.3-0",
|
|
83
|
-
"react": "^
|
|
84
|
-
"react-dom": "^
|
|
85
|
-
"rollup": "^2.
|
|
84
|
+
"react": "^18.0.0",
|
|
85
|
+
"react-dom": "^18.0.0",
|
|
86
|
+
"rollup": "^2.69.0",
|
|
86
87
|
"rollup-plugin-replace": "^2.2.0",
|
|
87
88
|
"rollup-plugin-terser": "^5.1.2",
|
|
88
89
|
"rollup-plugin-typescript2": "^0.30.0",
|
|
89
90
|
"ts-jest": "^26.5.6",
|
|
90
|
-
"typescript": "^4.
|
|
91
|
+
"typescript": "^4.5.2",
|
|
91
92
|
"xstate": "*"
|
|
92
93
|
}
|
|
93
94
|
}
|
package/dist/xstate-react.cjs.js
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// this file might look strange and you might be wondering what it's for
|
|
3
|
-
// it's lets you import your source files by importing this entrypoint
|
|
4
|
-
// as you would import it if it was built with preconstruct build
|
|
5
|
-
// this file is slightly different to some others though
|
|
6
|
-
// it has a require hook which compiles your code with Babel
|
|
7
|
-
// this means that you don't have to set up @babel/register or anything like that
|
|
8
|
-
// but you can still require this module and it'll be compiled
|
|
9
|
-
|
|
10
|
-
// this bit of code imports the require hook and registers it
|
|
11
|
-
let unregister = require("../../../node_modules/@preconstruct/hook").___internalHook(typeof __dirname === 'undefined' ? undefined : __dirname, "../../..", "..");
|
|
12
|
-
|
|
13
|
-
// this re-exports the source file
|
|
14
|
-
module.exports = require("../src/index.ts");
|
|
15
|
-
|
|
16
|
-
unregister();
|