@xstate/react 2.0.0 → 4.0.0-alpha.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 +42 -0
- package/dist/declarations/src/fsm.d.ts +3 -0
- package/{es/index.js → dist/declarations/src/index.d.ts} +5 -5
- package/dist/declarations/src/types.d.ts +15 -0
- package/dist/declarations/src/useActor.d.ts +3 -0
- package/{es → dist/declarations/src}/useConstant.d.ts +1 -2
- package/dist/declarations/src/useInterpret.d.ts +13 -0
- package/dist/declarations/src/useMachine.d.ts +21 -0
- package/dist/declarations/src/useSelector.d.ts +2 -0
- package/{es → dist/declarations/src}/useSpawn.d.ts +9 -10
- package/dist/useConstant-644f0e11.cjs.dev.js +74 -0
- package/dist/useConstant-94bfdbb5.esm.js +71 -0
- package/dist/useConstant-eeb49d3f.cjs.prod.js +17 -0
- package/dist/xstate-react.cjs.d.ts +1 -0
- package/dist/xstate-react.cjs.dev.js +241 -0
- package/dist/xstate-react.cjs.js +7 -0
- package/dist/xstate-react.cjs.prod.js +231 -0
- package/dist/xstate-react.esm.js +229 -0
- package/fsm/dist/xstate-react-fsm.cjs.d.ts +1 -0
- package/fsm/dist/xstate-react-fsm.cjs.dev.js +98 -0
- package/fsm/dist/xstate-react-fsm.cjs.js +7 -0
- package/fsm/dist/xstate-react-fsm.cjs.prod.js +144 -0
- package/fsm/dist/xstate-react-fsm.esm.js +89 -0
- package/fsm/package.json +3 -5
- package/package.json +25 -48
- package/dist/xstate-react-fsm.umd.min.js +0 -28
- package/dist/xstate-react.umd.min.js +0 -28
- package/es/fsm.d.ts +0 -20
- package/es/fsm.js +0 -73
- package/es/index.d.ts +0 -6
- package/es/types.d.ts +0 -31
- package/es/types.js +0 -5
- package/es/useActor.d.ts +0 -9
- package/es/useActor.js +0 -79
- package/es/useConstant.js +0 -8
- package/es/useInterpret.d.ts +0 -13
- package/es/useInterpret.js +0 -116
- package/es/useMachine.d.ts +0 -24
- package/es/useMachine.js +0 -78
- package/es/useReactEffectActions.d.ts +0 -3
- package/es/useReactEffectActions.js +0 -76
- package/es/useSelector.d.ts +0 -3
- package/es/useSelector.js +0 -70
- package/es/useSpawn.js +0 -15
- package/es/utils.d.ts +0 -4
- package/es/utils.js +0 -53
- package/lib/fsm.d.ts +0 -20
- package/lib/fsm.js +0 -78
- package/lib/index.d.ts +0 -6
- package/lib/index.js +0 -15
- package/lib/types.d.ts +0 -31
- package/lib/types.js +0 -8
- package/lib/useActor.d.ts +0 -9
- package/lib/useActor.js +0 -84
- package/lib/useConstant.d.ts +0 -2
- package/lib/useConstant.js +0 -11
- package/lib/useInterpret.d.ts +0 -13
- package/lib/useInterpret.js +0 -120
- package/lib/useMachine.d.ts +0 -24
- package/lib/useMachine.js +0 -84
- package/lib/useReactEffectActions.d.ts +0 -3
- package/lib/useReactEffectActions.js +0 -80
- package/lib/useSelector.d.ts +0 -3
- package/lib/useSelector.js +0 -74
- package/lib/useSpawn.d.ts +0 -10
- package/lib/useSpawn.js +0 -19
- package/lib/utils.d.ts +0 -4
- package/lib/utils.js +0 -58
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var React = require('react');
|
|
6
|
+
var withSelector = require('use-sync-external-store/shim/with-selector');
|
|
7
|
+
var useIsomorphicLayoutEffect = require('use-isomorphic-layout-effect');
|
|
8
|
+
var useConstant = require('../../dist/useConstant-eeb49d3f.cjs.prod.js');
|
|
9
|
+
var fsm = require('@xstate/fsm');
|
|
10
|
+
|
|
11
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
|
|
12
|
+
|
|
13
|
+
var useIsomorphicLayoutEffect__default = /*#__PURE__*/_interopDefault(useIsomorphicLayoutEffect);
|
|
14
|
+
|
|
15
|
+
function _arrayWithHoles(arr) {
|
|
16
|
+
if (Array.isArray(arr)) return arr;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function _iterableToArrayLimit(arr, i) {
|
|
20
|
+
if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return;
|
|
21
|
+
var _arr = [];
|
|
22
|
+
var _n = true;
|
|
23
|
+
var _d = false;
|
|
24
|
+
var _e = undefined;
|
|
25
|
+
|
|
26
|
+
try {
|
|
27
|
+
for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
|
|
28
|
+
_arr.push(_s.value);
|
|
29
|
+
|
|
30
|
+
if (i && _arr.length === i) break;
|
|
31
|
+
}
|
|
32
|
+
} catch (err) {
|
|
33
|
+
_d = true;
|
|
34
|
+
_e = err;
|
|
35
|
+
} finally {
|
|
36
|
+
try {
|
|
37
|
+
if (!_n && _i["return"] != null) _i["return"]();
|
|
38
|
+
} finally {
|
|
39
|
+
if (_d) throw _e;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return _arr;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function _arrayLikeToArray(arr, len) {
|
|
47
|
+
if (len == null || len > arr.length) len = arr.length;
|
|
48
|
+
|
|
49
|
+
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
|
|
50
|
+
|
|
51
|
+
return arr2;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function _unsupportedIterableToArray(o, minLen) {
|
|
55
|
+
if (!o) return;
|
|
56
|
+
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
|
|
57
|
+
var n = Object.prototype.toString.call(o).slice(8, -1);
|
|
58
|
+
if (n === "Object" && o.constructor) n = o.constructor.name;
|
|
59
|
+
if (n === "Map" || n === "Set") return Array.from(o);
|
|
60
|
+
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function _nonIterableRest() {
|
|
64
|
+
throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function _slicedToArray(arr, i) {
|
|
68
|
+
return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function identity(a) {
|
|
72
|
+
return a;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
var getServiceState = function getServiceState(service) {
|
|
76
|
+
var currentValue;
|
|
77
|
+
service.subscribe(function (state) {
|
|
78
|
+
currentValue = state;
|
|
79
|
+
}).unsubscribe();
|
|
80
|
+
return currentValue;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
function useMachine(stateMachine, options) {
|
|
84
|
+
var persistedStateRef = React.useRef();
|
|
85
|
+
|
|
86
|
+
var _useConstant = useConstant.useConstant(function () {
|
|
87
|
+
var queue = [];
|
|
88
|
+
var service = fsm.interpret(fsm.createMachine(stateMachine.config, options ? options : stateMachine._options));
|
|
89
|
+
var send = service.send;
|
|
90
|
+
|
|
91
|
+
service.send = function (event) {
|
|
92
|
+
if (service.status === fsm.InterpreterStatus.NotStarted) {
|
|
93
|
+
queue.push(event);
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
send(event);
|
|
98
|
+
persistedStateRef.current = service.state;
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
return [service, queue];
|
|
102
|
+
}),
|
|
103
|
+
_useConstant2 = _slicedToArray(_useConstant, 2),
|
|
104
|
+
service = _useConstant2[0],
|
|
105
|
+
queue = _useConstant2[1]; // TODO: consider using `useInsertionEffect` if available
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
useIsomorphicLayoutEffect__default['default'](function () {
|
|
109
|
+
if (options) {
|
|
110
|
+
service._machine._options = options;
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
var useServiceResult = useService(service);
|
|
114
|
+
React.useEffect(function () {
|
|
115
|
+
service.start(persistedStateRef.current);
|
|
116
|
+
queue.forEach(service.send);
|
|
117
|
+
persistedStateRef.current = service.state;
|
|
118
|
+
return function () {
|
|
119
|
+
service.stop();
|
|
120
|
+
};
|
|
121
|
+
}, []);
|
|
122
|
+
return useServiceResult;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
var isEqual = function isEqual(_prevState, nextState) {
|
|
126
|
+
return nextState.changed === false;
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
function useService(service) {
|
|
130
|
+
var getSnapshot = React.useCallback(function () {
|
|
131
|
+
return getServiceState(service);
|
|
132
|
+
}, [service]);
|
|
133
|
+
var subscribe = React.useCallback(function (handleStoreChange) {
|
|
134
|
+
var _service$subscribe = service.subscribe(handleStoreChange),
|
|
135
|
+
unsubscribe = _service$subscribe.unsubscribe;
|
|
136
|
+
|
|
137
|
+
return unsubscribe;
|
|
138
|
+
}, [service]);
|
|
139
|
+
var storeSnapshot = withSelector.useSyncExternalStoreWithSelector(subscribe, getSnapshot, getSnapshot, identity, isEqual);
|
|
140
|
+
return [storeSnapshot, service.send, service];
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
exports.useMachine = useMachine;
|
|
144
|
+
exports.useService = useService;
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { useRef, useState, useEffect, useCallback } from 'react';
|
|
2
|
+
import { useSyncExternalStoreWithSelector } from 'use-sync-external-store/shim/with-selector';
|
|
3
|
+
import { _ as _slicedToArray, u as useConstant } from '../../dist/useConstant-94bfdbb5.esm.js';
|
|
4
|
+
import useIsomorphicLayoutEffect from 'use-isomorphic-layout-effect';
|
|
5
|
+
import { interpret, createMachine, InterpreterStatus } from '@xstate/fsm';
|
|
6
|
+
|
|
7
|
+
function identity(a) {
|
|
8
|
+
return a;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
var getServiceState = function getServiceState(service) {
|
|
12
|
+
var currentValue;
|
|
13
|
+
service.subscribe(function (state) {
|
|
14
|
+
currentValue = state;
|
|
15
|
+
}).unsubscribe();
|
|
16
|
+
return currentValue;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
function useMachine(stateMachine, options) {
|
|
20
|
+
var persistedStateRef = useRef();
|
|
21
|
+
|
|
22
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
23
|
+
var _useState = useState(stateMachine),
|
|
24
|
+
_useState2 = _slicedToArray(_useState, 1),
|
|
25
|
+
initialMachine = _useState2[0];
|
|
26
|
+
|
|
27
|
+
if (stateMachine !== initialMachine) {
|
|
28
|
+
console.warn('Machine given to `useMachine` has changed between renders. This is not supported and might lead to unexpected results.\n' + 'Please make sure that you pass the same Machine as argument each time.');
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
var _useConstant = useConstant(function () {
|
|
33
|
+
var queue = [];
|
|
34
|
+
var service = interpret(createMachine(stateMachine.config, options ? options : stateMachine._options));
|
|
35
|
+
var send = service.send;
|
|
36
|
+
|
|
37
|
+
service.send = function (event) {
|
|
38
|
+
if (service.status === InterpreterStatus.NotStarted) {
|
|
39
|
+
queue.push(event);
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
send(event);
|
|
44
|
+
persistedStateRef.current = service.state;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
return [service, queue];
|
|
48
|
+
}),
|
|
49
|
+
_useConstant2 = _slicedToArray(_useConstant, 2),
|
|
50
|
+
service = _useConstant2[0],
|
|
51
|
+
queue = _useConstant2[1]; // TODO: consider using `useInsertionEffect` if available
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
useIsomorphicLayoutEffect(function () {
|
|
55
|
+
if (options) {
|
|
56
|
+
service._machine._options = options;
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
var useServiceResult = useService(service);
|
|
60
|
+
useEffect(function () {
|
|
61
|
+
service.start(persistedStateRef.current);
|
|
62
|
+
queue.forEach(service.send);
|
|
63
|
+
persistedStateRef.current = service.state;
|
|
64
|
+
return function () {
|
|
65
|
+
service.stop();
|
|
66
|
+
};
|
|
67
|
+
}, []);
|
|
68
|
+
return useServiceResult;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
var isEqual = function isEqual(_prevState, nextState) {
|
|
72
|
+
return nextState.changed === false;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
function useService(service) {
|
|
76
|
+
var getSnapshot = useCallback(function () {
|
|
77
|
+
return getServiceState(service);
|
|
78
|
+
}, [service]);
|
|
79
|
+
var subscribe = useCallback(function (handleStoreChange) {
|
|
80
|
+
var _service$subscribe = service.subscribe(handleStoreChange),
|
|
81
|
+
unsubscribe = _service$subscribe.unsubscribe;
|
|
82
|
+
|
|
83
|
+
return unsubscribe;
|
|
84
|
+
}, [service]);
|
|
85
|
+
var storeSnapshot = useSyncExternalStoreWithSelector(subscribe, getSnapshot, getSnapshot, identity, isEqual);
|
|
86
|
+
return [storeSnapshot, service.send, service];
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export { useMachine, useService };
|
package/fsm/package.json
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xstate/react",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0-alpha.0",
|
|
4
4
|
"description": "XState tools for React",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"state",
|
|
@@ -15,46 +15,26 @@
|
|
|
15
15
|
"author": "David Khourshid <davidkpiano@gmail.com>",
|
|
16
16
|
"homepage": "https://github.com/statelyai/xstate/tree/main/packages/xstate-react#readme",
|
|
17
17
|
"license": "MIT",
|
|
18
|
-
"main": "
|
|
19
|
-
"module": "
|
|
20
|
-
"types": "
|
|
21
|
-
"typesVersions": {
|
|
22
|
-
"<4.0": {
|
|
23
|
-
"lib/index.d.ts": [
|
|
24
|
-
"index.v3.d.ts"
|
|
25
|
-
]
|
|
26
|
-
}
|
|
27
|
-
},
|
|
18
|
+
"main": "dist/xstate-react.cjs.js",
|
|
19
|
+
"module": "dist/xstate-react.esm.js",
|
|
20
|
+
"types": "dist/xstate-react.cjs.d.ts",
|
|
28
21
|
"sideEffects": false,
|
|
29
|
-
"directories": {
|
|
30
|
-
"lib": "lib",
|
|
31
|
-
"test": "test"
|
|
32
|
-
},
|
|
33
22
|
"files": [
|
|
34
|
-
"
|
|
35
|
-
"
|
|
36
|
-
"dist/**/*.js",
|
|
37
|
-
"es/**/*.js",
|
|
38
|
-
"es/**/*.d.ts",
|
|
39
|
-
"fsm/package.json"
|
|
23
|
+
"dist",
|
|
24
|
+
"fsm"
|
|
40
25
|
],
|
|
41
26
|
"repository": {
|
|
42
27
|
"type": "git",
|
|
43
28
|
"url": "git+ssh://git@github.com/statelyai/xstate.git"
|
|
44
29
|
},
|
|
45
|
-
"scripts": {
|
|
46
|
-
"clean": "rm -rf dist lib tsconfig.tsbuildinfo",
|
|
47
|
-
"build": "tsc && tsc --outDir es --module es2015 && rollup -c",
|
|
48
|
-
"test": "jest",
|
|
49
|
-
"prepare": "npm run build"
|
|
50
|
-
},
|
|
30
|
+
"scripts": {},
|
|
51
31
|
"bugs": {
|
|
52
32
|
"url": "https://github.com/statelyai/xstate/issues"
|
|
53
33
|
},
|
|
54
34
|
"peerDependencies": {
|
|
55
|
-
"@xstate/fsm": "^
|
|
56
|
-
"react": "^16.8.0 || ^17.0.0",
|
|
57
|
-
"xstate": "^
|
|
35
|
+
"@xstate/fsm": "^2.0.0",
|
|
36
|
+
"react": "^16.8.0 || ^17.0.0 || ^18.0.0",
|
|
37
|
+
"xstate": "^5.0.0-alpha.0"
|
|
58
38
|
},
|
|
59
39
|
"peerDependenciesMeta": {
|
|
60
40
|
"@xstate/fsm": {
|
|
@@ -66,28 +46,25 @@
|
|
|
66
46
|
},
|
|
67
47
|
"dependencies": {
|
|
68
48
|
"use-isomorphic-layout-effect": "^1.0.0",
|
|
69
|
-
"use-
|
|
49
|
+
"use-sync-external-store": "^1.0.0"
|
|
70
50
|
},
|
|
71
51
|
"devDependencies": {
|
|
72
|
-
"@
|
|
73
|
-
"@rollup/plugin-node-resolve": "^11.0.1",
|
|
74
|
-
"@testing-library/react": "^8.0.9",
|
|
52
|
+
"@testing-library/react": "^13.0.0",
|
|
75
53
|
"@types/jsdom": "^12.2.3",
|
|
76
|
-
"@types/react": "^
|
|
77
|
-
"@types/react-dom": "^
|
|
78
|
-
"@
|
|
79
|
-
"
|
|
54
|
+
"@types/react": "^17.0.43",
|
|
55
|
+
"@types/react-dom": "^17.0.14",
|
|
56
|
+
"@types/use-sync-external-store": "^0.0.3",
|
|
57
|
+
"@xstate/fsm": "2.0.0",
|
|
80
58
|
"jsdom": "^14.0.0",
|
|
81
59
|
"jsdom-global": "^3.0.2",
|
|
82
|
-
"
|
|
83
|
-
"react": "^
|
|
84
|
-
"
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
"
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
"xstate": "*"
|
|
60
|
+
"react": "^18.0.0",
|
|
61
|
+
"react-dom": "^18.0.0",
|
|
62
|
+
"xstate": "5.0.0-alpha.0"
|
|
63
|
+
},
|
|
64
|
+
"preconstruct": {
|
|
65
|
+
"entrypoints": [
|
|
66
|
+
"./index.ts",
|
|
67
|
+
"./fsm.ts"
|
|
68
|
+
]
|
|
92
69
|
}
|
|
93
70
|
}
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react"),require("@xstate/fsm")):"function"==typeof define&&define.amd?define(["exports","react","@xstate/fsm"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).XStateReactFSM={},e.React,e.XStateFSM)}(this,(function(e,t,r){"use strict";function n(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var u=n(t);
|
|
2
|
-
/*! *****************************************************************************
|
|
3
|
-
Copyright (c) Microsoft Corporation.
|
|
4
|
-
|
|
5
|
-
Permission to use, copy, modify, and/or distribute this software for any
|
|
6
|
-
purpose with or without fee is hereby granted.
|
|
7
|
-
|
|
8
|
-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
9
|
-
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
10
|
-
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
11
|
-
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
12
|
-
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
13
|
-
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
14
|
-
PERFORMANCE OF THIS SOFTWARE.
|
|
15
|
-
***************************************************************************** */function o(e){var t={exports:{}};return e(t,t.exports),t.exports
|
|
16
|
-
/*
|
|
17
|
-
object-assign
|
|
18
|
-
(c) Sindre Sorhus
|
|
19
|
-
@license MIT
|
|
20
|
-
*/}var i=Object.getOwnPropertySymbols,c=Object.prototype.hasOwnProperty,a=Object.prototype.propertyIsEnumerable;function f(e){if(null==e)throw new TypeError("Object.assign cannot be called with null or undefined");return Object(e)}var s=function(){try{if(!Object.assign)return!1;var e=new String("abc");if(e[5]="de","5"===Object.getOwnPropertyNames(e)[0])return!1;for(var t={},r=0;r<10;r++)t["_"+String.fromCharCode(r)]=r;if("0123456789"!==Object.getOwnPropertyNames(t).map((function(e){return t[e]})).join(""))return!1;var n={};return"abcdefghijklmnopqrst".split("").forEach((function(e){n[e]=e})),"abcdefghijklmnopqrst"===Object.keys(Object.assign({},n)).join("")}catch(e){return!1}}()?Object.assign:function(e,t){for(var r,n,u=f(e),o=1;o<arguments.length;o++){for(var s in r=Object(arguments[o]))c.call(r,s)&&(u[s]=r[s]);if(i){n=i(r);for(var l=0;l<n.length;l++)a.call(r,n[l])&&(u[n[l]]=r[n[l]])}}return u},l={useSubscription:function(e){var t=e.getCurrentValue,r=e.subscribe,n=u.default.useState((function(){return{getCurrentValue:t,subscribe:r,value:t()}}));e=n[0];var o=n[1];return n=e.value,e.getCurrentValue===t&&e.subscribe===r||(n=t(),o({getCurrentValue:t,subscribe:r,value:n})),u.default.useDebugValue(n),u.default.useEffect((function(){function e(){if(!n){var e=t();o((function(n){return n.getCurrentValue!==t||n.subscribe!==r||n.value===e?n:s({},n,{value:e})}))}}var n=!1,u=r(e);return e(),function(){n=!0,u()}}),[t,r]),n}},b=(o((function(e,t){})),o((function(e){e.exports=l})));
|
|
21
|
-
/** @license React vundefined
|
|
22
|
-
* use-subscription.production.min.js
|
|
23
|
-
*
|
|
24
|
-
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
25
|
-
*
|
|
26
|
-
* This source code is licensed under the MIT license found in the
|
|
27
|
-
* LICENSE file in the root directory of this source tree.
|
|
28
|
-
*/var p=function(e){var t;return e.subscribe((function(e){t=e})).unsubscribe(),t};e.useMachine=function(e,n){var u,o,i=(u=function(){return r.interpret(r.createMachine(e.config,n||e._options)).start()},(o=t.useRef()).current||(o.current={v:u()}),o.current.v),c=function(e,t){var r="function"==typeof Symbol&&e[Symbol.iterator];if(!r)return e;var n,u,o=r.call(e),i=[];try{for(;(void 0===t||t-- >0)&&!(n=o.next()).done;)i.push(n.value)}catch(e){u={error:e}}finally{try{n&&!n.done&&(r=o.return)&&r.call(o)}finally{if(u)throw u.error}}return i}(t.useState((function(){return p(i)})),2),a=c[0],f=c[1];return t.useEffect((function(){n&&(i._machine._options=n)})),t.useEffect((function(){return i.subscribe(f),function(){i.stop()}}),[]),[a,i.send,i]},e.useService=function(e){var r=t.useMemo((function(){var t=p(e);return{getCurrentValue:function(){return t},subscribe:function(r){return e.subscribe((function(e){!1!==e.changed&&(t=e,r())})).unsubscribe}}}),[e]);return[b.useSubscription(r),e.send,e]},Object.defineProperty(e,"__esModule",{value:!0})}));
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react"),require("xstate"),require("xstate/lib/behaviors")):"function"==typeof define&&define.amd?define(["exports","react","xstate","xstate/lib/behaviors"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).XStateReact={},e.React,e.XState,e.behaviors)}(this,(function(e,t,n,r){"use strict";function u(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var o,i=u(t),c=function(){return(c=Object.assign||function(e){for(var t,n=1,r=arguments.length;n<r;n++)for(var u in t=arguments[n])Object.prototype.hasOwnProperty.call(t,u)&&(e[u]=t[u]);return e}).apply(this,arguments)};
|
|
2
|
-
/*! *****************************************************************************
|
|
3
|
-
Copyright (c) Microsoft Corporation.
|
|
4
|
-
|
|
5
|
-
Permission to use, copy, modify, and/or distribute this software for any
|
|
6
|
-
purpose with or without fee is hereby granted.
|
|
7
|
-
|
|
8
|
-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
9
|
-
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
10
|
-
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
11
|
-
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
12
|
-
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
13
|
-
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
14
|
-
PERFORMANCE OF THIS SOFTWARE.
|
|
15
|
-
***************************************************************************** */function a(e,t){var n={};for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&t.indexOf(r)<0&&(n[r]=e[r]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var u=0;for(r=Object.getOwnPropertySymbols(e);u<r.length;u++)t.indexOf(r[u])<0&&Object.prototype.propertyIsEnumerable.call(e,r[u])&&(n[r[u]]=e[r[u]])}return n}function f(e,t){var n="function"==typeof Symbol&&e[Symbol.iterator];if(!n)return e;var r,u,o=n.call(e),i=[];try{for(;(void 0===t||t-- >0)&&!(r=o.next()).done;)i.push(r.value)}catch(e){u={error:e}}finally{try{r&&!r.done&&(n=o.return)&&n.call(o)}finally{if(u)throw u.error}}return i}function s(e,t,n){if(n||2===arguments.length)for(var r,u=0,o=t.length;u<o;u++)!r&&u in t||(r||(r=Array.prototype.slice.call(t,0,u)),r[u]=t[u]);return e.concat(r||Array.prototype.slice.call(t))}!function(e){e[e.Effect=1]="Effect",e[e.LayoutEffect=2]="LayoutEffect"}(o||(o={}));var l=t.useLayoutEffect;function v(e){var n=t.useRef();return n.current||(n.current={v:e()}),n.current.v}function p(e,t){var n,r,u=f([[],[]],2),o=u[0],i=u[1];try{for(var c=function(e){var t="function"==typeof Symbol&&Symbol.iterator,n=t&&e[t],r=0;if(n)return n.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&r>=e.length&&(e=void 0),{value:e&&e[r++],done:!e}}};throw new TypeError(t?"Object is not iterable.":"Symbol.iterator is not defined.")}(e),a=c.next();!a.done;a=c.next()){var s=a.value;t(s)?o.push(s):i.push(s)}}catch(e){n={error:e}}finally{try{a&&!a.done&&(r=c.return)&&r.call(c)}finally{if(n)throw n.error}}return[o,i]}function b(e,t){(0,e.exec)(t.context,t._event.data,{action:e,state:t,_event:t._event})()}function h(e){var n=t.useRef([]),r=t.useRef([]);l((function(){var t=e.subscribe((function(e){var t,u;if(e.actions.length){var i=f(p(e.actions.filter((function(e){return"function"==typeof e.exec&&"__effect"in e.exec})),(function(e){return e.exec.__effect===o.Effect})),2),c=i[0],a=i[1];(t=n.current).push.apply(t,s([],f(c.map((function(t){return[t,e]}))),!1)),(u=r.current).push.apply(u,s([],f(a.map((function(t){return[t,e]}))),!1))}}));return function(){t.unsubscribe()}}),[]),l((function(){for(;r.current.length;){var e=f(r.current.shift(),2);b(e[0],e[1])}})),t.useEffect((function(){for(;n.current.length;){var e=f(n.current.shift(),2);b(e[0],e[1])}}))}function y(e,t,n){if("object"==typeof e)return e;var r=function(){};return{next:e,error:t||r,complete:n||r}}function d(e){for(var t=[],r=1;r<arguments.length;r++)t[r-1]=arguments[r];var u=f(t,2),o=u[0],i=void 0===o?{}:o,s=u[1],p=v((function(){return"function"==typeof e?e():e})),b=i.context,d=i.guards,g=i.actions,O=i.services,j=i.delays,m=i.state,x=a(i,["context","guards","actions","services","delays","state"]),S=i.activities,w=v((function(){var e={context:b,guards:d,actions:g,activities:S,services:O,delays:j},t=p.withConfig(e,(function(){return c(c({},p.context),b)}));return n.interpret(t,c({deferEvents:!0},x))}));return l((function(){var e;return s&&(e=w.subscribe(y(s))),function(){null==e||e.unsubscribe()}}),[s]),l((function(){return w.start(m?n.State.create(m):void 0),function(){w.stop()}}),[]),l((function(){Object.assign(w.machine.options.actions,g),Object.assign(w.machine.options.guards,d),Object.assign(w.machine.options.activities,S),Object.assign(w.machine.options.services,O),Object.assign(w.machine.options.delays,j)}),[g,d,S,O,j]),h(w),w}function g(e,t){var n=function(){for(var t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];return function(){return e.apply(void 0,s([],f(t),!1))}};return Object.defineProperties(n,{name:{value:"effect:".concat(e.name)},__effect:{value:t}}),n}function O(e){return"state"in e}function j(e){return"deferred"in e}var m=function(){};function x(e){return"getSnapshot"in e?e.getSnapshot():O(e)?e.state:void 0}function S(e){var t={exports:{}};return e(t,t.exports),t.exports
|
|
16
|
-
/*
|
|
17
|
-
object-assign
|
|
18
|
-
(c) Sindre Sorhus
|
|
19
|
-
@license MIT
|
|
20
|
-
*/}var w=Object.getOwnPropertySymbols,E=Object.prototype.hasOwnProperty,C=Object.prototype.propertyIsEnumerable;function _(e){if(null==e)throw new TypeError("Object.assign cannot be called with null or undefined");return Object(e)}var P=function(){try{if(!Object.assign)return!1;var e=new String("abc");if(e[5]="de","5"===Object.getOwnPropertyNames(e)[0])return!1;for(var t={},n=0;n<10;n++)t["_"+String.fromCharCode(n)]=n;if("0123456789"!==Object.getOwnPropertyNames(t).map((function(e){return t[e]})).join(""))return!1;var r={};return"abcdefghijklmnopqrst".split("").forEach((function(e){r[e]=e})),"abcdefghijklmnopqrst"===Object.keys(Object.assign({},r)).join("")}catch(e){return!1}}()?Object.assign:function(e,t){for(var n,r,u=_(e),o=1;o<arguments.length;o++){for(var i in n=Object(arguments[o]))E.call(n,i)&&(u[i]=n[i]);if(w){r=w(n);for(var c=0;c<r.length;c++)C.call(n,r[c])&&(u[r[c]]=n[r[c]])}}return u},V={useSubscription:function(e){var t=e.getCurrentValue,n=e.subscribe,r=i.default.useState((function(){return{getCurrentValue:t,subscribe:n,value:t()}}));e=r[0];var u=r[1];return r=e.value,e.getCurrentValue===t&&e.subscribe===n||(r=t(),u({getCurrentValue:t,subscribe:n,value:r})),i.default.useDebugValue(r),i.default.useEffect((function(){function e(){if(!r){var e=t();u((function(r){return r.getCurrentValue!==t||r.subscribe!==n||r.value===e?r:P({},r,{value:e})}))}}var r=!1,o=n(e);return e(),function(){r=!0,o()}}),[t,n]),r}},R=(S((function(e,t){})),S((function(e){e.exports=V})));
|
|
21
|
-
/** @license React vundefined
|
|
22
|
-
* use-subscription.production.min.js
|
|
23
|
-
*
|
|
24
|
-
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
25
|
-
*
|
|
26
|
-
* This source code is licensed under the MIT license found in the
|
|
27
|
-
* LICENSE file in the root directory of this source tree.
|
|
28
|
-
*/var k=function(e,t){return e===t},q=function(e){return"state"in(n=e)&&"machine"in n?0!==(t=e).status?t.state:t.machine.initialState:O(e)?e.state:void 0;var t,n};e.asEffect=function(e){return g(e,o.Effect)},e.asLayoutEffect=function(e){return g(e,o.LayoutEffect)},e.useActor=function(e,n){void 0===n&&(n=x);var r=t.useRef(e),u=t.useRef([]),o=f(t.useState((function(){return n(e)})),2),i=o[0],c=o[1],a=v((function(){return function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];var n=e[0],o=r.current;j(o)&&o.deferred?u.current.push(n):o.send(n)}}));return l((function(){r.current=e,c(n(e));for(var t=e.subscribe({next:function(e){return c(e)},error:m,complete:m});u.current.length>0;){var o=u.current.shift();e.send(o)}return function(){t.unsubscribe()}}),[e]),[i,a]},e.useInterpret=d,e.useMachine=function(e){for(var r=[],u=1;u<arguments.length;u++)r[u-1]=arguments[u];var o=f(r,1),i=o[0],c=void 0===i?{}:i,a=t.useCallback((function(e){var t=void 0===e.changed&&Object.keys(e.children).length;(e.changed||t)&&p(e)}),[]),s=d(e,c,a),l=f(t.useState((function(){var e=s.machine.initialState;return c.state?n.State.create(c.state):e})),2),v=l[0],p=l[1];return[v,s.send,s]},e.useSelector=function(e,n,r,u){void 0===r&&(r=k),void 0===u&&(u=q);var o=t.useRef(n),i=t.useMemo((function(){var t,i=u(e),c=n(i);return{getSnapshot:function(){return i},getCurrentValue:function(){return c},setCurrentValue:function(e){c=e,null==t||t()},subscribe:function(n){t=n;var u=e.subscribe((function(e){i=e;var t=o.current(e);r(c,t)||(c=t,n())}));return function(){u.unsubscribe()}}}}),[e]),c=R.useSubscription(i),a=!1;if(o.current!==n){var f=n(i.getSnapshot());r(c,f)||(a=!0,c=f)}return l((function(){o.current=n,a&&i.setCurrentValue(c)})),c},e.useSpawn=function(e){return v((function(){return r.spawnBehavior(e)}))},Object.defineProperty(e,"__esModule",{value:!0})}));
|
package/es/fsm.d.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { StateMachine, EventObject, Typestate } from '@xstate/fsm';
|
|
2
|
-
export declare function useMachine<TContext extends object, TEvent extends EventObject = EventObject, TState extends Typestate<TContext> = {
|
|
3
|
-
value: any;
|
|
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
|
-
];
|
|
20
|
-
//# sourceMappingURL=fsm.d.ts.map
|
package/es/fsm.js
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
var __read = (this && this.__read) || function (o, n) {
|
|
2
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
3
|
-
if (!m) return o;
|
|
4
|
-
var i = m.call(o), r, ar = [], e;
|
|
5
|
-
try {
|
|
6
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
7
|
-
}
|
|
8
|
-
catch (error) { e = { error: error }; }
|
|
9
|
-
finally {
|
|
10
|
-
try {
|
|
11
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
12
|
-
}
|
|
13
|
-
finally { if (e) throw e.error; }
|
|
14
|
-
}
|
|
15
|
-
return ar;
|
|
16
|
-
};
|
|
17
|
-
import { useState, useEffect, useMemo } from 'react';
|
|
18
|
-
import { interpret, createMachine } from '@xstate/fsm';
|
|
19
|
-
import { useSubscription } from 'use-subscription';
|
|
20
|
-
import useConstant from './useConstant';
|
|
21
|
-
var getServiceState = function (service) {
|
|
22
|
-
var currentValue;
|
|
23
|
-
service
|
|
24
|
-
.subscribe(function (state) {
|
|
25
|
-
currentValue = state;
|
|
26
|
-
})
|
|
27
|
-
.unsubscribe();
|
|
28
|
-
return currentValue;
|
|
29
|
-
};
|
|
30
|
-
export function useMachine(stateMachine, options) {
|
|
31
|
-
if (process.env.NODE_ENV !== 'production') {
|
|
32
|
-
var _a = __read(useState(stateMachine), 1), initialMachine = _a[0];
|
|
33
|
-
if (stateMachine !== initialMachine) {
|
|
34
|
-
console.warn('Machine given to `useMachine` has changed between renders. This is not supported and might lead to unexpected results.\n' +
|
|
35
|
-
'Please make sure that you pass the same Machine as argument each time.');
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
var service = useConstant(function () {
|
|
39
|
-
return interpret(createMachine(stateMachine.config, options ? options : stateMachine._options)).start();
|
|
40
|
-
});
|
|
41
|
-
var _b = __read(useState(function () { return getServiceState(service); }), 2), state = _b[0], setState = _b[1];
|
|
42
|
-
useEffect(function () {
|
|
43
|
-
if (options) {
|
|
44
|
-
service._machine._options = options;
|
|
45
|
-
}
|
|
46
|
-
});
|
|
47
|
-
useEffect(function () {
|
|
48
|
-
service.subscribe(setState);
|
|
49
|
-
return function () {
|
|
50
|
-
service.stop();
|
|
51
|
-
};
|
|
52
|
-
}, []);
|
|
53
|
-
return [state, service.send, service];
|
|
54
|
-
}
|
|
55
|
-
export function useService(service) {
|
|
56
|
-
var subscription = useMemo(function () {
|
|
57
|
-
var currentState = getServiceState(service);
|
|
58
|
-
return {
|
|
59
|
-
getCurrentValue: function () { return currentState; },
|
|
60
|
-
subscribe: function (callback) {
|
|
61
|
-
var unsubscribe = service.subscribe(function (state) {
|
|
62
|
-
if (state.changed !== false) {
|
|
63
|
-
currentState = state;
|
|
64
|
-
callback();
|
|
65
|
-
}
|
|
66
|
-
}).unsubscribe;
|
|
67
|
-
return unsubscribe;
|
|
68
|
-
}
|
|
69
|
-
};
|
|
70
|
-
}, [service]);
|
|
71
|
-
var state = useSubscription(subscription);
|
|
72
|
-
return [state, service.send, service];
|
|
73
|
-
}
|
package/es/index.d.ts
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
export { useMachine, asEffect, asLayoutEffect } from './useMachine';
|
|
2
|
-
export { useActor } from './useActor';
|
|
3
|
-
export { useInterpret } from './useInterpret';
|
|
4
|
-
export { useSelector } from './useSelector';
|
|
5
|
-
export { useSpawn } from './useSpawn';
|
|
6
|
-
//# sourceMappingURL=index.d.ts.map
|
package/es/types.d.ts
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import { ActionMeta, ActionObject, EventObject, State, StateConfig } from 'xstate';
|
|
2
|
-
export declare type MaybeLazy<T> = T | (() => T);
|
|
3
|
-
export declare type NoInfer<T> = [T][T extends any ? 0 : any];
|
|
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
|
-
export interface UseMachineOptions<TContext, TEvent extends EventObject> {
|
|
17
|
-
/**
|
|
18
|
-
* If provided, will be merged with machine's `context`.
|
|
19
|
-
*/
|
|
20
|
-
context?: Partial<TContext>;
|
|
21
|
-
/**
|
|
22
|
-
* The state to rehydrate the machine to. The machine will
|
|
23
|
-
* start at this state instead of its `initialState`.
|
|
24
|
-
*/
|
|
25
|
-
state?: StateConfig<TContext, TEvent>;
|
|
26
|
-
}
|
|
27
|
-
export declare type ActionStateTuple<TContext, TEvent extends EventObject> = [
|
|
28
|
-
ReactActionObject<TContext, TEvent>,
|
|
29
|
-
State<TContext, TEvent>
|
|
30
|
-
];
|
|
31
|
-
//# sourceMappingURL=types.d.ts.map
|
package/es/types.js
DELETED
package/es/useActor.d.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { ActorRef, EventObject, Sender } from 'xstate';
|
|
2
|
-
export declare function isActorWithState<T extends ActorRef<any>>(actorRef: T): actorRef is T & {
|
|
3
|
-
state: any;
|
|
4
|
-
};
|
|
5
|
-
declare type EmittedFromActorRef<TActor extends ActorRef<any, any>> = TActor extends ActorRef<any, infer TEmitted> ? TEmitted : never;
|
|
6
|
-
export declare function useActor<TActor extends ActorRef<any, any>>(actorRef: TActor, getSnapshot?: (actor: TActor) => EmittedFromActorRef<TActor>): [EmittedFromActorRef<TActor>, TActor['send']];
|
|
7
|
-
export declare function useActor<TEvent extends EventObject, TEmitted>(actorRef: ActorRef<TEvent, TEmitted>, getSnapshot?: (actor: ActorRef<TEvent, TEmitted>) => TEmitted): [TEmitted, Sender<TEvent>];
|
|
8
|
-
export {};
|
|
9
|
-
//# sourceMappingURL=useActor.d.ts.map
|
package/es/useActor.js
DELETED
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
var __read = (this && this.__read) || function (o, n) {
|
|
2
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
3
|
-
if (!m) return o;
|
|
4
|
-
var i = m.call(o), r, ar = [], e;
|
|
5
|
-
try {
|
|
6
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
7
|
-
}
|
|
8
|
-
catch (error) { e = { error: error }; }
|
|
9
|
-
finally {
|
|
10
|
-
try {
|
|
11
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
12
|
-
}
|
|
13
|
-
finally { if (e) throw e.error; }
|
|
14
|
-
}
|
|
15
|
-
return ar;
|
|
16
|
-
};
|
|
17
|
-
import { useState, useRef } from 'react';
|
|
18
|
-
import useIsomorphicLayoutEffect from 'use-isomorphic-layout-effect';
|
|
19
|
-
import useConstant from './useConstant';
|
|
20
|
-
export function isActorWithState(actorRef) {
|
|
21
|
-
return 'state' in actorRef;
|
|
22
|
-
}
|
|
23
|
-
function isDeferredActor(actorRef) {
|
|
24
|
-
return 'deferred' in actorRef;
|
|
25
|
-
}
|
|
26
|
-
var noop = function () {
|
|
27
|
-
/* ... */
|
|
28
|
-
};
|
|
29
|
-
function defaultGetSnapshot(actorRef) {
|
|
30
|
-
return 'getSnapshot' in actorRef
|
|
31
|
-
? actorRef.getSnapshot()
|
|
32
|
-
: isActorWithState(actorRef)
|
|
33
|
-
? actorRef.state
|
|
34
|
-
: undefined;
|
|
35
|
-
}
|
|
36
|
-
export function useActor(actorRef, getSnapshot) {
|
|
37
|
-
if (getSnapshot === void 0) { getSnapshot = defaultGetSnapshot; }
|
|
38
|
-
var actorRefRef = useRef(actorRef);
|
|
39
|
-
var deferredEventsRef = useRef([]);
|
|
40
|
-
var _a = __read(useState(function () { return getSnapshot(actorRef); }), 2), current = _a[0], setCurrent = _a[1];
|
|
41
|
-
var send = useConstant(function () { return function () {
|
|
42
|
-
var args = [];
|
|
43
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
44
|
-
args[_i] = arguments[_i];
|
|
45
|
-
}
|
|
46
|
-
var event = args[0];
|
|
47
|
-
if (process.env.NODE_ENV !== 'production' && args.length > 1) {
|
|
48
|
-
console.warn("Unexpected payload: ".concat(JSON.stringify(args[1]), ". Only a single event object can be sent to actor send() functions."));
|
|
49
|
-
}
|
|
50
|
-
var currentActorRef = actorRefRef.current;
|
|
51
|
-
// If the previous actor is a deferred actor,
|
|
52
|
-
// queue the events so that they can be replayed
|
|
53
|
-
// on the non-deferred actor.
|
|
54
|
-
if (isDeferredActor(currentActorRef) && currentActorRef.deferred) {
|
|
55
|
-
deferredEventsRef.current.push(event);
|
|
56
|
-
}
|
|
57
|
-
else {
|
|
58
|
-
currentActorRef.send(event);
|
|
59
|
-
}
|
|
60
|
-
}; });
|
|
61
|
-
useIsomorphicLayoutEffect(function () {
|
|
62
|
-
actorRefRef.current = actorRef;
|
|
63
|
-
setCurrent(getSnapshot(actorRef));
|
|
64
|
-
var subscription = actorRef.subscribe({
|
|
65
|
-
next: function (emitted) { return setCurrent(emitted); },
|
|
66
|
-
error: noop,
|
|
67
|
-
complete: noop
|
|
68
|
-
});
|
|
69
|
-
// Dequeue deferred events from the previous deferred actorRef
|
|
70
|
-
while (deferredEventsRef.current.length > 0) {
|
|
71
|
-
var deferredEvent = deferredEventsRef.current.shift();
|
|
72
|
-
actorRef.send(deferredEvent);
|
|
73
|
-
}
|
|
74
|
-
return function () {
|
|
75
|
-
subscription.unsubscribe();
|
|
76
|
-
};
|
|
77
|
-
}, [actorRef]);
|
|
78
|
-
return [current, send];
|
|
79
|
-
}
|