@xstate/react 2.0.1 → 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 +25 -0
- 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 -1
- package/es/index.js +1 -1
- package/es/types.d.ts +1 -16
- package/es/types.js +1 -5
- package/es/useActor.js +12 -31
- package/es/useInterpret.d.ts +2 -1
- package/es/useInterpret.js +32 -43
- package/es/useMachine.d.ts +2 -4
- package/es/useMachine.js +39 -48
- package/es/useSelector.js +11 -52
- package/lib/fsm.d.ts +3 -19
- package/lib/fsm.js +34 -25
- package/lib/index.d.ts +1 -1
- package/lib/index.js +1 -3
- package/lib/types.d.ts +1 -16
- package/lib/types.js +0 -6
- package/lib/useActor.js +11 -30
- package/lib/useInterpret.d.ts +2 -1
- package/lib/useInterpret.js +32 -42
- package/lib/useMachine.d.ts +2 -4
- package/lib/useMachine.js +37 -48
- package/lib/useSelector.js +10 -51
- package/package.json +12 -11
- package/es/useReactEffectActions.d.ts +0 -3
- package/es/useReactEffectActions.js +0 -76
- package/lib/useReactEffectActions.d.ts +0 -3
- package/lib/useReactEffectActions.js +0 -80
package/es/useMachine.js
CHANGED
|
@@ -14,41 +14,12 @@ var __read = (this && this.__read) || function (o, n) {
|
|
|
14
14
|
}
|
|
15
15
|
return ar;
|
|
16
16
|
};
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
}
|
|
24
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
25
|
-
};
|
|
26
|
-
import { useCallback, useState } from 'react';
|
|
27
|
-
import { State } from 'xstate';
|
|
28
|
-
import { ReactEffectType } from './types';
|
|
29
|
-
import { useInterpret } from './useInterpret';
|
|
30
|
-
function createReactActionFunction(exec, tag) {
|
|
31
|
-
var effectExec = function () {
|
|
32
|
-
var args = [];
|
|
33
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
34
|
-
args[_i] = arguments[_i];
|
|
35
|
-
}
|
|
36
|
-
// don't execute; just return
|
|
37
|
-
return function () {
|
|
38
|
-
return exec.apply(void 0, __spreadArray([], __read(args), false));
|
|
39
|
-
};
|
|
40
|
-
};
|
|
41
|
-
Object.defineProperties(effectExec, {
|
|
42
|
-
name: { value: "effect:".concat(exec.name) },
|
|
43
|
-
__effect: { value: tag }
|
|
44
|
-
});
|
|
45
|
-
return effectExec;
|
|
46
|
-
}
|
|
47
|
-
export function asEffect(exec) {
|
|
48
|
-
return createReactActionFunction(exec, ReactEffectType.Effect);
|
|
49
|
-
}
|
|
50
|
-
export function asLayoutEffect(exec) {
|
|
51
|
-
return createReactActionFunction(exec, ReactEffectType.LayoutEffect);
|
|
17
|
+
import { useCallback, useEffect } from 'react';
|
|
18
|
+
import { useSyncExternalStoreWithSelector } from 'use-sync-external-store/shim/with-selector';
|
|
19
|
+
import { InterpreterStatus, State } from 'xstate';
|
|
20
|
+
import { useIdleInterpreter } from './useInterpret';
|
|
21
|
+
function identity(a) {
|
|
22
|
+
return a;
|
|
52
23
|
}
|
|
53
24
|
export function useMachine(getMachine) {
|
|
54
25
|
var _a = [];
|
|
@@ -56,23 +27,43 @@ export function useMachine(getMachine) {
|
|
|
56
27
|
_a[_i - 1] = arguments[_i];
|
|
57
28
|
}
|
|
58
29
|
var _b = __read(_a, 1), _c = _b[0], options = _c === void 0 ? {} : _c;
|
|
59
|
-
|
|
30
|
+
// using `useIdleInterpreter` allows us to subscribe to the service *before* we start it
|
|
31
|
+
// so we don't miss any notifications
|
|
32
|
+
var service = useIdleInterpreter(getMachine, options);
|
|
33
|
+
var getSnapshot = useCallback(function () {
|
|
34
|
+
if (service.status === InterpreterStatus.NotStarted) {
|
|
35
|
+
return (options.state
|
|
36
|
+
? State.create(options.state)
|
|
37
|
+
: service.machine.initialState);
|
|
38
|
+
}
|
|
39
|
+
return service.state;
|
|
40
|
+
}, [service]);
|
|
41
|
+
var isEqual = useCallback(function (prevState, nextState) {
|
|
42
|
+
if (service.status === InterpreterStatus.NotStarted) {
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
60
45
|
// Only change the current state if:
|
|
61
46
|
// - the incoming state is the "live" initial state (since it might have new actors)
|
|
62
47
|
// - OR the incoming state actually changed.
|
|
63
48
|
//
|
|
64
49
|
// The "live" initial state will have .changed === undefined.
|
|
65
|
-
var initialStateChanged = nextState.changed === undefined &&
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
50
|
+
var initialStateChanged = nextState.changed === undefined &&
|
|
51
|
+
(Object.keys(nextState.children).length > 0 ||
|
|
52
|
+
typeof prevState.changed === 'boolean');
|
|
53
|
+
return !(nextState.changed || initialStateChanged);
|
|
54
|
+
}, [service]);
|
|
55
|
+
var subscribe = useCallback(function (handleStoreChange) {
|
|
56
|
+
var unsubscribe = service.subscribe(handleStoreChange).unsubscribe;
|
|
57
|
+
return unsubscribe;
|
|
58
|
+
}, [service]);
|
|
59
|
+
var storeSnapshot = useSyncExternalStoreWithSelector(subscribe, getSnapshot, getSnapshot, identity, isEqual);
|
|
60
|
+
useEffect(function () {
|
|
61
|
+
var rehydratedState = options.state;
|
|
62
|
+
service.start(rehydratedState ? State.create(rehydratedState) : undefined);
|
|
63
|
+
return function () {
|
|
64
|
+
service.stop();
|
|
65
|
+
service.status = InterpreterStatus.NotStarted;
|
|
66
|
+
};
|
|
69
67
|
}, []);
|
|
70
|
-
|
|
71
|
-
var _d = __read(useState(function () {
|
|
72
|
-
var initialState = service.machine.initialState;
|
|
73
|
-
return (options.state
|
|
74
|
-
? State.create(options.state)
|
|
75
|
-
: initialState);
|
|
76
|
-
}), 2), state = _d[0], setState = _d[1];
|
|
77
|
-
return [state, service.send, service];
|
|
68
|
+
return [storeSnapshot, service.send, service];
|
|
78
69
|
}
|
package/es/useSelector.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import useIsomorphicLayoutEffect from 'use-isomorphic-layout-effect';
|
|
1
|
+
import { useCallback } from 'react';
|
|
2
|
+
import { useSyncExternalStoreWithSelector } from 'use-sync-external-store/shim/with-selector';
|
|
4
3
|
import { isActorWithState } from './useActor';
|
|
5
4
|
import { getServiceSnapshot } from './utils';
|
|
6
5
|
function isService(actor) {
|
|
@@ -17,54 +16,14 @@ var defaultGetSnapshot = function (a) {
|
|
|
17
16
|
export function useSelector(actor, selector, compare, getSnapshot) {
|
|
18
17
|
if (compare === void 0) { compare = defaultCompare; }
|
|
19
18
|
if (getSnapshot === void 0) { getSnapshot = defaultGetSnapshot; }
|
|
20
|
-
var
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
var current = selector(snapshot);
|
|
24
|
-
var notifySubscriber;
|
|
25
|
-
return {
|
|
26
|
-
getSnapshot: function () { return snapshot; },
|
|
27
|
-
getCurrentValue: function () { return current; },
|
|
28
|
-
setCurrentValue: function (newCurrent) {
|
|
29
|
-
current = newCurrent;
|
|
30
|
-
notifySubscriber === null || notifySubscriber === void 0 ? void 0 : notifySubscriber();
|
|
31
|
-
},
|
|
32
|
-
subscribe: function (callback) {
|
|
33
|
-
notifySubscriber = callback;
|
|
34
|
-
var sub = actor.subscribe(function (emitted) {
|
|
35
|
-
snapshot = emitted;
|
|
36
|
-
var next = latestSelectorRef.current(emitted);
|
|
37
|
-
if (!compare(current, next)) {
|
|
38
|
-
current = next;
|
|
39
|
-
callback();
|
|
40
|
-
}
|
|
41
|
-
});
|
|
42
|
-
return function () {
|
|
43
|
-
sub.unsubscribe();
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
|
-
// intentionally omit `getSnapshot` and `compare`
|
|
48
|
-
// - `getSnapshot`: it is only supposed to read the "initial" snapshot of an actor
|
|
49
|
-
// - `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)
|
|
19
|
+
var subscribe = useCallback(function (handleStoreChange) {
|
|
20
|
+
var unsubscribe = actor.subscribe(handleStoreChange).unsubscribe;
|
|
21
|
+
return unsubscribe;
|
|
50
22
|
}, [actor]);
|
|
51
|
-
var
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
currentSelected = selected;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
useIsomorphicLayoutEffect(function () {
|
|
61
|
-
latestSelectorRef.current = selector;
|
|
62
|
-
// this condition should not be required, but setState bailouts are currently buggy: https://github.com/facebook/react/issues/22654
|
|
63
|
-
if (currentChanged) {
|
|
64
|
-
// required so we don't cause a rerender by setting state (this could create infinite rerendering loop with inline selectors)
|
|
65
|
-
// 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
|
|
66
|
-
subscription.setCurrentValue(currentSelected);
|
|
67
|
-
}
|
|
68
|
-
});
|
|
69
|
-
return currentSelected;
|
|
23
|
+
var boundGetSnapshot = useCallback(function () { return getSnapshot(actor); }, [
|
|
24
|
+
actor,
|
|
25
|
+
getSnapshot
|
|
26
|
+
]);
|
|
27
|
+
var selectedSnapshot = useSyncExternalStoreWithSelector(subscribe, boundGetSnapshot, boundGetSnapshot, selector, compare);
|
|
28
|
+
return selectedSnapshot;
|
|
70
29
|
}
|
package/lib/fsm.d.ts
CHANGED
|
@@ -1,20 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare function useMachine<
|
|
3
|
-
|
|
4
|
-
context: TContext;
|
|
5
|
-
}>(stateMachine: StateMachine.Machine<TContext, TEvent, TState>, options?: {
|
|
6
|
-
actions?: StateMachine.ActionMap<TContext, TEvent>;
|
|
7
|
-
}): [
|
|
8
|
-
StateMachine.State<TContext, TEvent, TState>,
|
|
9
|
-
StateMachine.Service<TContext, TEvent, TState>['send'],
|
|
10
|
-
StateMachine.Service<TContext, TEvent, TState>
|
|
11
|
-
];
|
|
12
|
-
export declare function useService<TContext extends object, TEvent extends EventObject = EventObject, TState extends Typestate<TContext> = {
|
|
13
|
-
value: any;
|
|
14
|
-
context: TContext;
|
|
15
|
-
}>(service: StateMachine.Service<TContext, TEvent, TState>): [
|
|
16
|
-
StateMachine.State<TContext, TEvent, TState>,
|
|
17
|
-
StateMachine.Service<TContext, TEvent, TState>['send'],
|
|
18
|
-
StateMachine.Service<TContext, TEvent, TState>
|
|
19
|
-
];
|
|
1
|
+
import { MachineImplementationsFrom, ServiceFrom, StateFrom, StateMachine } from '@xstate/fsm';
|
|
2
|
+
export declare function useMachine<TMachine extends StateMachine.AnyMachine>(stateMachine: TMachine, options?: MachineImplementationsFrom<TMachine>): [StateFrom<TMachine>, ServiceFrom<TMachine>['send'], ServiceFrom<TMachine>];
|
|
3
|
+
export declare function useService<TService extends StateMachine.AnyService>(service: TService): [StateFrom<TService>, TService['send'], TService];
|
|
20
4
|
//# sourceMappingURL=fsm.d.ts.map
|
package/lib/fsm.js
CHANGED
|
@@ -17,10 +17,14 @@ var __read = (this && this.__read) || function (o, n) {
|
|
|
17
17
|
};
|
|
18
18
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
19
19
|
exports.useService = exports.useMachine = void 0;
|
|
20
|
-
var react_1 = require("react");
|
|
21
20
|
var fsm_1 = require("@xstate/fsm");
|
|
22
|
-
var
|
|
21
|
+
var react_1 = require("react");
|
|
22
|
+
var use_isomorphic_layout_effect_1 = require("use-isomorphic-layout-effect");
|
|
23
|
+
var with_selector_1 = require("use-sync-external-store/shim/with-selector");
|
|
23
24
|
var useConstant_1 = require("./useConstant");
|
|
25
|
+
function identity(a) {
|
|
26
|
+
return a;
|
|
27
|
+
}
|
|
24
28
|
var getServiceState = function (service) {
|
|
25
29
|
var currentValue;
|
|
26
30
|
service
|
|
@@ -31,6 +35,7 @@ var getServiceState = function (service) {
|
|
|
31
35
|
return currentValue;
|
|
32
36
|
};
|
|
33
37
|
function useMachine(stateMachine, options) {
|
|
38
|
+
var persistedStateRef = (0, react_1.useRef)();
|
|
34
39
|
if (process.env.NODE_ENV !== 'production') {
|
|
35
40
|
var _a = __read((0, react_1.useState)(stateMachine), 1), initialMachine = _a[0];
|
|
36
41
|
if (stateMachine !== initialMachine) {
|
|
@@ -38,41 +43,45 @@ function useMachine(stateMachine, options) {
|
|
|
38
43
|
'Please make sure that you pass the same Machine as argument each time.');
|
|
39
44
|
}
|
|
40
45
|
}
|
|
41
|
-
var
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
+
var _b = __read((0, useConstant_1.default)(function () {
|
|
47
|
+
var queue = [];
|
|
48
|
+
var service = (0, fsm_1.interpret)((0, fsm_1.createMachine)(stateMachine.config, options ? options : stateMachine._options));
|
|
49
|
+
var send = service.send;
|
|
50
|
+
service.send = function (event) {
|
|
51
|
+
if (service.status === fsm_1.InterpreterStatus.NotStarted) {
|
|
52
|
+
queue.push(event);
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
send(event);
|
|
56
|
+
persistedStateRef.current = service.state;
|
|
57
|
+
};
|
|
58
|
+
return [service, queue];
|
|
59
|
+
}), 2), service = _b[0], queue = _b[1];
|
|
60
|
+
(0, use_isomorphic_layout_effect_1.default)(function () {
|
|
46
61
|
if (options) {
|
|
47
62
|
service._machine._options = options;
|
|
48
63
|
}
|
|
49
64
|
});
|
|
65
|
+
var useServiceResult = useService(service);
|
|
50
66
|
(0, react_1.useEffect)(function () {
|
|
51
|
-
service.
|
|
67
|
+
service.start(persistedStateRef.current);
|
|
68
|
+
queue.forEach(service.send);
|
|
69
|
+
persistedStateRef.current = service.state;
|
|
52
70
|
return function () {
|
|
53
71
|
service.stop();
|
|
54
72
|
};
|
|
55
73
|
}, []);
|
|
56
|
-
return
|
|
74
|
+
return useServiceResult;
|
|
57
75
|
}
|
|
58
76
|
exports.useMachine = useMachine;
|
|
77
|
+
var isEqual = function (_prevState, nextState) { return nextState.changed === false; };
|
|
59
78
|
function useService(service) {
|
|
60
|
-
var
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
subscribe: function (callback) {
|
|
65
|
-
var unsubscribe = service.subscribe(function (state) {
|
|
66
|
-
if (state.changed !== false) {
|
|
67
|
-
currentState = state;
|
|
68
|
-
callback();
|
|
69
|
-
}
|
|
70
|
-
}).unsubscribe;
|
|
71
|
-
return unsubscribe;
|
|
72
|
-
}
|
|
73
|
-
};
|
|
79
|
+
var getSnapshot = (0, react_1.useCallback)(function () { return getServiceState(service); }, [service]);
|
|
80
|
+
var subscribe = (0, react_1.useCallback)(function (handleStoreChange) {
|
|
81
|
+
var unsubscribe = service.subscribe(handleStoreChange).unsubscribe;
|
|
82
|
+
return unsubscribe;
|
|
74
83
|
}, [service]);
|
|
75
|
-
var
|
|
76
|
-
return [
|
|
84
|
+
var storeSnapshot = (0, with_selector_1.useSyncExternalStoreWithSelector)(subscribe, getSnapshot, getSnapshot, identity, isEqual);
|
|
85
|
+
return [storeSnapshot, service.send, service];
|
|
77
86
|
}
|
|
78
87
|
exports.useService = useService;
|
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.useSpawn = exports.useSelector = exports.useInterpret = exports.useActor = exports.
|
|
3
|
+
exports.useSpawn = exports.useSelector = exports.useInterpret = exports.useActor = exports.useMachine = void 0;
|
|
4
4
|
var useMachine_1 = require("./useMachine");
|
|
5
5
|
Object.defineProperty(exports, "useMachine", { enumerable: true, get: function () { return useMachine_1.useMachine; } });
|
|
6
|
-
Object.defineProperty(exports, "asEffect", { enumerable: true, get: function () { return useMachine_1.asEffect; } });
|
|
7
|
-
Object.defineProperty(exports, "asLayoutEffect", { enumerable: true, get: function () { return useMachine_1.asLayoutEffect; } });
|
|
8
6
|
var useActor_1 = require("./useActor");
|
|
9
7
|
Object.defineProperty(exports, "useActor", { enumerable: true, get: function () { return useActor_1.useActor; } });
|
|
10
8
|
var useInterpret_1 = require("./useInterpret");
|
package/lib/types.d.ts
CHANGED
|
@@ -1,18 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { EventObject, StateConfig } from 'xstate';
|
|
2
2
|
export declare type MaybeLazy<T> = T | (() => T);
|
|
3
3
|
export declare type NoInfer<T> = [T][T extends any ? 0 : any];
|
|
4
4
|
export declare type Prop<T, K> = K extends keyof T ? T[K] : never;
|
|
5
|
-
export declare enum ReactEffectType {
|
|
6
|
-
Effect = 1,
|
|
7
|
-
LayoutEffect = 2
|
|
8
|
-
}
|
|
9
|
-
export interface ReactActionFunction<TContext, TEvent extends EventObject> {
|
|
10
|
-
(context: TContext, event: TEvent, meta: ActionMeta<TContext, TEvent>): () => void;
|
|
11
|
-
__effect: ReactEffectType;
|
|
12
|
-
}
|
|
13
|
-
export interface ReactActionObject<TContext, TEvent extends EventObject> extends ActionObject<TContext, TEvent> {
|
|
14
|
-
exec: ReactActionFunction<TContext, TEvent>;
|
|
15
|
-
}
|
|
16
5
|
export interface UseMachineOptions<TContext, TEvent extends EventObject> {
|
|
17
6
|
/**
|
|
18
7
|
* If provided, will be merged with machine's `context`.
|
|
@@ -24,8 +13,4 @@ export interface UseMachineOptions<TContext, TEvent extends EventObject> {
|
|
|
24
13
|
*/
|
|
25
14
|
state?: StateConfig<TContext, TEvent>;
|
|
26
15
|
}
|
|
27
|
-
export declare type ActionStateTuple<TContext, TEvent extends EventObject> = [
|
|
28
|
-
ReactActionObject<TContext, TEvent>,
|
|
29
|
-
State<TContext, TEvent>
|
|
30
|
-
];
|
|
31
16
|
//# sourceMappingURL=types.d.ts.map
|
package/lib/types.js
CHANGED
|
@@ -1,8 +1,2 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ReactEffectType = void 0;
|
|
4
|
-
var ReactEffectType;
|
|
5
|
-
(function (ReactEffectType) {
|
|
6
|
-
ReactEffectType[ReactEffectType["Effect"] = 1] = "Effect";
|
|
7
|
-
ReactEffectType[ReactEffectType["LayoutEffect"] = 2] = "LayoutEffect";
|
|
8
|
-
})(ReactEffectType = exports.ReactEffectType || (exports.ReactEffectType = {}));
|
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()
|
|
@@ -41,7 +23,15 @@ function useActor(actorRef, getSnapshot) {
|
|
|
41
23
|
if (getSnapshot === void 0) { getSnapshot = defaultGetSnapshot; }
|
|
42
24
|
var actorRefRef = (0, react_1.useRef)(actorRef);
|
|
43
25
|
var deferredEventsRef = (0, react_1.useRef)([]);
|
|
44
|
-
var
|
|
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);
|
|
45
35
|
var send = (0, useConstant_1.default)(function () { return function () {
|
|
46
36
|
var args = [];
|
|
47
37
|
for (var _i = 0; _i < arguments.length; _i++) {
|
|
@@ -64,21 +54,12 @@ function useActor(actorRef, getSnapshot) {
|
|
|
64
54
|
}; });
|
|
65
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,6 +1,7 @@
|
|
|
1
|
-
import { AnyStateMachine, AreAllImplementationsAssumedToBeProvided, InternalMachineOptions, InterpreterFrom, InterpreterOptions, Observer, StateFrom } from 'xstate';
|
|
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
|
+
export declare function useIdleInterpreter(getMachine: MaybeLazy<AnyStateMachine>, options: Partial<InterpreterOptions> & Partial<UseMachineOptions<unknown, never>> & Partial<MachineOptions<unknown, never>>): AnyInterpreter;
|
|
4
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
7
|
observerOrListener?: Observer<StateFrom<TMachine>> | ((value: StateFrom<TMachine>) => void)
|
package/lib/useInterpret.js
CHANGED
|
@@ -38,45 +38,24 @@ 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
|
-
// copied from core/src/utils.ts
|
|
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];
|
|
46
|
+
function useIdleInterpreter(getMachine, options) {
|
|
66
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
|
-
// it's not defined in `TypegenMachineOptions` so we can't just unpack this property here freely
|
|
79
|
-
var activities = options.activities;
|
|
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"]);
|
|
80
59
|
var service = (0, useConstant_1.default)(function () {
|
|
81
60
|
var machineConfig = {
|
|
82
61
|
context: context,
|
|
@@ -87,23 +66,8 @@ 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 (0, xstate_1.interpret)(machineWithConfig,
|
|
69
|
+
return (0, xstate_1.interpret)(machineWithConfig, interpreterOptions);
|
|
91
70
|
});
|
|
92
|
-
(0, 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
|
-
(0, 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.
|
|
@@ -114,7 +78,33 @@ function useInterpret(getMachine) {
|
|
|
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`.
|