@xstate/react 2.0.0-pr2674-2021926101023 → 2.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 +34 -28
- package/dist/xstate-react.umd.min.js +3 -3
- package/es/index.d.ts +0 -1
- package/es/index.js +0 -1
- package/es/useActor.js +1 -1
- package/es/useInterpret.d.ts +2 -2
- package/es/useMachine.d.ts +3 -3
- package/es/useMachine.js +10 -6
- package/es/useReactEffectActions.js +11 -7
- package/es/useSelector.js +9 -4
- package/es/utils.d.ts +2 -0
- package/es/utils.js +3 -0
- package/lib/fsm.js +8 -8
- package/lib/index.d.ts +0 -1
- package/lib/index.js +1 -3
- package/lib/useActor.js +6 -6
- package/lib/useInterpret.d.ts +2 -2
- package/lib/useInterpret.js +8 -8
- package/lib/useMachine.d.ts +3 -3
- package/lib/useMachine.js +13 -9
- package/lib/useReactEffectActions.js +17 -13
- package/lib/useSelector.js +15 -10
- package/lib/useSpawn.js +2 -2
- package/lib/utils.d.ts +2 -0
- package/lib/utils.js +5 -1
- package/package.json +8 -8
- package/dist/xstate-react.cjs.js +0 -16
- package/es/useService.d.ts +0 -16
- package/es/useService.js +0 -36
- package/lib/useService.d.ts +0 -16
- package/lib/useService.js +0 -41
package/CHANGELOG.md
CHANGED
|
@@ -1,24 +1,30 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
-
## 2.0.0
|
|
3
|
+
## 2.0.0
|
|
4
4
|
|
|
5
5
|
### Major Changes
|
|
6
6
|
|
|
7
|
-
- e5a8b8dff
|
|
8
|
-
author: @mattpocock
|
|
7
|
+
- [#2674](https://github.com/statelyai/xstate/pull/2674) [`e5a8b8dff`](https://github.com/statelyai/xstate/commit/e5a8b8dffb88cffbdca26683099ffdf5f1b01c8d) Thanks [@Andarist](https://github.com/Andarist), [@mattpocock](https://github.com/mattpocock)! - To avoid breaking any consumers and to leverage the newly introduced typegen support, the major version of this package had to be bumped. While you can still use it with older versions of TS, the typegen support in this package requires TS version 4.0 or greater.
|
|
9
8
|
|
|
10
|
-
|
|
9
|
+
When using hooks from `@xstate/react` it's recommended to skip providing explicit generics to them. Note that that generics list has changed since v1 and we now only accept a single generic, `TMachine`.
|
|
10
|
+
|
|
11
|
+
* [#2674](https://github.com/statelyai/xstate/pull/2674) [`ab919d300`](https://github.com/statelyai/xstate/commit/ab919d300f6d2b78871d3399ec58a697c4268d9b) Thanks [@Andarist](https://github.com/Andarist)! - Removed already deprecated `useService` from `@xstate/react`. You can replace its usage with `useActor`.
|
|
12
|
+
|
|
13
|
+
### Patch Changes
|
|
14
|
+
|
|
15
|
+
- [#2957](https://github.com/statelyai/xstate/pull/2957) [`8550ddda7`](https://github.com/statelyai/xstate/commit/8550ddda73e2ad291e19173d7fa8d13e3336fbb9) Thanks [@davidkpiano](https://github.com/davidkpiano)! - The repository links have been updated from `github.com/davidkpiano` to `github.com/statelyai`.
|
|
16
|
+
|
|
17
|
+
## 1.6.3
|
|
11
18
|
|
|
12
19
|
### Patch Changes
|
|
13
20
|
|
|
14
|
-
-
|
|
15
|
-
- xstate@4.27.0-pr2674-2021926101023
|
|
21
|
+
- [#2767](https://github.com/statelyai/xstate/pull/2767) [`c1503b121`](https://github.com/statelyai/xstate/commit/c1503b1219d995ebf0f45de46036c5a1d7e6442f) Thanks [@Andarist](https://github.com/Andarist)! - Fixed an over-rendering issue in the `useSelector`.
|
|
16
22
|
|
|
17
23
|
## 1.6.2
|
|
18
24
|
|
|
19
25
|
### Patch Changes
|
|
20
26
|
|
|
21
|
-
- [#2736](https://github.com/statelyai/xstate/pull/2736) [`2246ae051`](https://github.com/statelyai/xstate/commit/2246ae051663f261b4750d7adba57f008ec28f1d) Thanks [@Andarist](https://github.com/Andarist), [@davidkpiano](https://github.com/
|
|
27
|
+
- [#2736](https://github.com/statelyai/xstate/pull/2736) [`2246ae051`](https://github.com/statelyai/xstate/commit/2246ae051663f261b4750d7adba57f008ec28f1d) Thanks [@Andarist](https://github.com/Andarist), [@davidkpiano](https://github.com/statelyai), [@VanTanev](https://github.com/VanTanev)! - The `useSelector(...)` hook now works as expected when the `actor` passed in changes. The hook will properly subscribe to the new `actor` and select the desired value. See [#2702](https://github.com/statelyai/xstate/issues/2702)
|
|
22
28
|
|
|
23
29
|
* [#2685](https://github.com/statelyai/xstate/pull/2685) [`469268d39`](https://github.com/statelyai/xstate/commit/469268d39fbc23996599773adfc4ca824b48585f) Thanks [@farskid](https://github.com/farskid), [@Andarist](https://github.com/Andarist)! - Fixed a regression with a development-only warning not being shown when a machine reference is updated during the hook lifecycle. This usually happens when machine options are dependent on external values and they're passed via `withConfig`.
|
|
24
30
|
|
|
@@ -114,13 +120,13 @@
|
|
|
114
120
|
|
|
115
121
|
### Patch Changes
|
|
116
122
|
|
|
117
|
-
- [`453acacb`](https://github.com/statelyai/xstate/commit/453acacbec364531a2851f183c3ab446d7db0e84) [#2389](https://github.com/statelyai/xstate/pull/2389) Thanks [@davidkpiano](https://github.com/
|
|
123
|
+
- [`453acacb`](https://github.com/statelyai/xstate/commit/453acacbec364531a2851f183c3ab446d7db0e84) [#2389](https://github.com/statelyai/xstate/pull/2389) Thanks [@davidkpiano](https://github.com/statelyai)! - An internal issue where the `spawnBehavior` import for the `useSpawn(...)` hook was broken internally has been fixed.
|
|
118
124
|
|
|
119
125
|
## 1.5.0
|
|
120
126
|
|
|
121
127
|
### Minor Changes
|
|
122
128
|
|
|
123
|
-
- [`432b60f7`](https://github.com/statelyai/xstate/commit/432b60f7bcbcee9510e0d86311abbfd75b1a674e) [#2280](https://github.com/statelyai/xstate/pull/2280) Thanks [@davidkpiano](https://github.com/
|
|
129
|
+
- [`432b60f7`](https://github.com/statelyai/xstate/commit/432b60f7bcbcee9510e0d86311abbfd75b1a674e) [#2280](https://github.com/statelyai/xstate/pull/2280) Thanks [@davidkpiano](https://github.com/statelyai)! - Just like `useInvoke(...)`, other types of actors can now be spawned from _behaviors_ using `useSpawn(...)`:
|
|
124
130
|
|
|
125
131
|
```tsx
|
|
126
132
|
import { fromReducer } from 'xstate/lib/behaviors';
|
|
@@ -174,7 +180,7 @@
|
|
|
174
180
|
|
|
175
181
|
### Minor Changes
|
|
176
182
|
|
|
177
|
-
- [`849ec56c`](https://github.com/
|
|
183
|
+
- [`849ec56c`](https://github.com/statelyai/xstate/commit/849ec56c2a9db34e65a30af94e68a7a7a50b4158) [#2286](https://github.com/statelyai/xstate/pull/2286) Thanks [@davidkpiano](https://github.com/statelyai)! - The `useService(...)` hook will be deprecated, since services are also actors. In future versions, the `useActor(...)` hook should be used instead:
|
|
178
184
|
|
|
179
185
|
```diff
|
|
180
186
|
-const [state, send] = useService(service);
|
|
@@ -183,25 +189,25 @@
|
|
|
183
189
|
|
|
184
190
|
### Patch Changes
|
|
185
191
|
|
|
186
|
-
- [`ea3aaffb`](https://github.com/
|
|
192
|
+
- [`ea3aaffb`](https://github.com/statelyai/xstate/commit/ea3aaffb906b34a42bb2736c7b91d54ffe9ed882) [#2326](https://github.com/statelyai/xstate/pull/2326) Thanks [@davidkpiano](https://github.com/statelyai)! - The `send` type returned in the tuple from `useActor(someService)` was an incorrect `never` type; this has been fixed.
|
|
187
193
|
|
|
188
194
|
## 1.3.4
|
|
189
195
|
|
|
190
196
|
### Patch Changes
|
|
191
197
|
|
|
192
|
-
- [`aa3c2991`](https://github.com/
|
|
198
|
+
- [`aa3c2991`](https://github.com/statelyai/xstate/commit/aa3c29916b7382fbcf1a3efb183ca1e8eb625480) [#2223](https://github.com/statelyai/xstate/pull/2223) Thanks [@davidkpiano](https://github.com/statelyai)! - Support for actor refs with the `.getSnapshot()` method (added for spawned actors in XState version 4.19) is now supported in the `useActor(...)` hook.
|
|
193
199
|
|
|
194
200
|
## 1.3.3
|
|
195
201
|
|
|
196
202
|
### Patch Changes
|
|
197
203
|
|
|
198
|
-
- [`27e7242c`](https://github.com/
|
|
204
|
+
- [`27e7242c`](https://github.com/statelyai/xstate/commit/27e7242c24146de85cf618a658b400a3241fa7d7) [#2112](https://github.com/statelyai/xstate/pull/2112) Thanks [@davidkpiano](https://github.com/statelyai)! - The `executeEffect` function is no longer exported (was meant to be internal and is useless as a public function anyway). This also fixes a circular dependency issue.
|
|
199
205
|
|
|
200
206
|
## 1.3.2
|
|
201
207
|
|
|
202
208
|
### Patch Changes
|
|
203
209
|
|
|
204
|
-
- [`bb5e81ea`](https://github.com/
|
|
210
|
+
- [`bb5e81ea`](https://github.com/statelyai/xstate/commit/bb5e81eaa1ecba1fd54a7677ce9eaee9bd695964) [#2050](https://github.com/statelyai/xstate/pull/2050) Thanks [@theKashey](https://github.com/theKashey)! - Added an explicit entrypoint for `@xstate/react/fsm` which you can use instead of `@xstate/react/lib/fsm`. This is the only specifier that will be supported in the future - the other one will be dropped in the next major version.
|
|
205
211
|
|
|
206
212
|
```diff
|
|
207
213
|
-import { useMachine } from '@xstate/react/lib/fsm'
|
|
@@ -212,15 +218,15 @@
|
|
|
212
218
|
|
|
213
219
|
### Patch Changes
|
|
214
220
|
|
|
215
|
-
- [`b076b253`](https://github.com/
|
|
221
|
+
- [`b076b253`](https://github.com/statelyai/xstate/commit/b076b25364224874f62e8065892be40dfbb28030) [#1947](https://github.com/statelyai/xstate/pull/1947) Thanks [@lukekarrys](https://github.com/lukekarrys)! - Fix typing of the service returned from the fsm useMachine hook by passing it Typestate
|
|
216
222
|
|
|
217
|
-
* [`9b5dc784`](https://github.com/
|
|
223
|
+
* [`9b5dc784`](https://github.com/statelyai/xstate/commit/9b5dc7843c44f50bcca0ffccb843b3d50cef6ddc) [#1950](https://github.com/statelyai/xstate/pull/1950) Thanks [@Andarist](https://github.com/Andarist)! - Fixed an issue with `toObserver` being internally imported from `xstate/lib/utils` which has broken UMD build and the declared peer dep contract.
|
|
218
224
|
|
|
219
225
|
## 1.3.0
|
|
220
226
|
|
|
221
227
|
### Minor Changes
|
|
222
228
|
|
|
223
|
-
- [`577ae023`](https://github.com/
|
|
229
|
+
- [`577ae023`](https://github.com/statelyai/xstate/commit/577ae02384926b49e876011c4393f212b49066f8) [#1915](https://github.com/statelyai/xstate/pull/1915) Thanks [@davidkpiano](https://github.com/statelyai)! - New hook: `useInterpret(machine)`, which is a low-level hook that interprets the `machine` and returns the `service`:
|
|
224
230
|
|
|
225
231
|
```js
|
|
226
232
|
import { useInterpret } from '@xstate/react';
|
|
@@ -233,7 +239,7 @@
|
|
|
233
239
|
};
|
|
234
240
|
```
|
|
235
241
|
|
|
236
|
-
* [`577ae023`](https://github.com/
|
|
242
|
+
* [`577ae023`](https://github.com/statelyai/xstate/commit/577ae02384926b49e876011c4393f212b49066f8) [#1915](https://github.com/statelyai/xstate/pull/1915) Thanks [@davidkpiano](https://github.com/statelyai)! - New hook: `useSelector(actor, selector)`, which subscribes to `actor` and returns the selected state derived from `selector(snapshot)`:
|
|
237
243
|
|
|
238
244
|
```js
|
|
239
245
|
import { useSelector } from '@xstate/react';
|
|
@@ -249,25 +255,25 @@
|
|
|
249
255
|
|
|
250
256
|
### Patch Changes
|
|
251
257
|
|
|
252
|
-
- [`4b31cefb`](https://github.com/
|
|
258
|
+
- [`4b31cefb`](https://github.com/statelyai/xstate/commit/4b31cefb3d3497e5515314046639df7e27dbe9e8) [#1780](https://github.com/statelyai/xstate/pull/1780) Thanks [@Andarist](https://github.com/Andarist)! - Fixed an issue with some external packages not being bundled correctly into the UMD bundles.
|
|
253
259
|
|
|
254
260
|
## 1.2.1
|
|
255
261
|
|
|
256
262
|
### Patch Changes
|
|
257
263
|
|
|
258
|
-
- [`a16a5f2f`](https://github.com/
|
|
264
|
+
- [`a16a5f2f`](https://github.com/statelyai/xstate/commit/a16a5f2ff5ba9d4d7834ec3ca2d0adecf5d6a870) [#1756](https://github.com/statelyai/xstate/pull/1756) Thanks [@dimitardanailov](https://github.com/dimitardanailov)! - Fixed an issue with `process` references not being removed correctly from the UMD bundles.
|
|
259
265
|
|
|
260
266
|
## 1.2.0
|
|
261
267
|
|
|
262
268
|
### Minor Changes
|
|
263
269
|
|
|
264
|
-
- [`dd98296e`](https://github.com/
|
|
270
|
+
- [`dd98296e`](https://github.com/statelyai/xstate/commit/dd98296e9fcbae905da2395e67e876e28be7c774) [#1738](https://github.com/statelyai/xstate/pull/1738) Thanks [@dimitardanailov](https://github.com/dimitardanailov)! - Added UMD bundle.
|
|
265
271
|
|
|
266
272
|
## 1.1.0
|
|
267
273
|
|
|
268
274
|
### Minor Changes
|
|
269
275
|
|
|
270
|
-
- [`89f9c27c`](https://github.com/
|
|
276
|
+
- [`89f9c27c`](https://github.com/statelyai/xstate/commit/89f9c27c453dc56bdfdf49c8ea1f0f87ff1f9b67) [#1622](https://github.com/statelyai/xstate/pull/1622) Thanks [@davidkpiano](https://github.com/statelyai)! - Spawned/invoked actors and interpreters are now typed as extending `ActorRef` rather than `Actor` or `Interpreter`. This unification of types should make it more straightforward to provide actor types in React:
|
|
271
277
|
|
|
272
278
|
```ts
|
|
273
279
|
import { ActorRef } from 'xstate';
|
|
@@ -309,15 +315,15 @@
|
|
|
309
315
|
|
|
310
316
|
### Patch Changes
|
|
311
317
|
|
|
312
|
-
- [`27db2950`](https://github.com/
|
|
318
|
+
- [`27db2950`](https://github.com/statelyai/xstate/commit/27db295064d42cacb89ff10d55f39eb7609148e1) [#1636](https://github.com/statelyai/xstate/pull/1636) Thanks [@Andarist](https://github.com/Andarist)! - Allow React 17 in the specified peer dependency range.
|
|
313
319
|
|
|
314
320
|
## 1.0.2
|
|
315
321
|
|
|
316
322
|
### Patch Changes
|
|
317
323
|
|
|
318
|
-
- [`c7927083`](https://github.com/
|
|
324
|
+
- [`c7927083`](https://github.com/statelyai/xstate/commit/c7927083a651e3c51952ade2ffda793df0391bf6) [#1516](https://github.com/statelyai/xstate/pull/1516) Thanks [@davidkpiano](https://github.com/statelyai)! - The `send` function returned from the `useService()` now can take two arguments (an event type and payload), to match the behavior of `@xstate/react` version 0.x.
|
|
319
325
|
|
|
320
|
-
* [`db77623a`](https://github.com/
|
|
326
|
+
* [`db77623a`](https://github.com/statelyai/xstate/commit/db77623a48955d762cffa9b624f438220add5eed) [#1516](https://github.com/statelyai/xstate/pull/1516) Thanks [@davidkpiano](https://github.com/statelyai)! - The `send` value returned from the `useService()` hook will now accept a payload, which matches the signature of the `send` value returned from the `useMachine()` hook:
|
|
321
327
|
|
|
322
328
|
```js
|
|
323
329
|
const [state, send] = useService(someService);
|
|
@@ -331,15 +337,15 @@
|
|
|
331
337
|
send({ type: 'ADD', value: 3 });
|
|
332
338
|
```
|
|
333
339
|
|
|
334
|
-
- [`93f6db02`](https://github.com/
|
|
340
|
+
- [`93f6db02`](https://github.com/statelyai/xstate/commit/93f6db02a2d56ec997198ddef0af3d7730bb79bb) [#1594](https://github.com/statelyai/xstate/pull/1594) Thanks [@Andarist](https://github.com/Andarist)! - Fixed an issue with internal `setState` in `useService` being called with 2 arguments instead of 1.
|
|
335
341
|
|
|
336
|
-
* [`72b0880e`](https://github.com/
|
|
342
|
+
* [`72b0880e`](https://github.com/statelyai/xstate/commit/72b0880e6444ae009adca72088872bb5c0760ce3) [#1504](https://github.com/statelyai/xstate/pull/1504) Thanks [@Andarist](https://github.com/Andarist)! - Fixed issue with `useService` returning an initial state for services in their final states.
|
|
337
343
|
|
|
338
344
|
## 1.0.1
|
|
339
345
|
|
|
340
346
|
### Patch Changes
|
|
341
347
|
|
|
342
|
-
- [`c0bd0407`](https://github.com/
|
|
348
|
+
- [`c0bd0407`](https://github.com/statelyai/xstate/commit/c0bd040767dcac20ed690e49a8725b4f1011dd5d) [#1493](https://github.com/statelyai/xstate/pull/1493) Thanks [@davidkpiano](https://github.com/statelyai)! - There will now be a descriptive error when trying to use an actor-like object in the `useService()` hook, where `useActor()` should be preferred:
|
|
343
349
|
|
|
344
350
|
> Attempted to use an actor-like object instead of a service in the useService() hook. Please use the useActor() hook instead.
|
|
345
351
|
|
|
@@ -12,12 +12,12 @@
|
|
|
12
12
|
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
13
13
|
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
14
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){for(var
|
|
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
16
|
/*
|
|
17
17
|
object-assign
|
|
18
18
|
(c) Sindre Sorhus
|
|
19
19
|
@license MIT
|
|
20
|
-
*/}var
|
|
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
21
|
/** @license React vundefined
|
|
22
22
|
* use-subscription.production.min.js
|
|
23
23
|
*
|
|
@@ -25,4 +25,4 @@
|
|
|
25
25
|
*
|
|
26
26
|
* This source code is licensed under the MIT license found in the
|
|
27
27
|
* LICENSE file in the root directory of this source tree.
|
|
28
|
-
*/var
|
|
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/index.d.ts
CHANGED
package/es/index.js
CHANGED
package/es/useActor.js
CHANGED
|
@@ -45,7 +45,7 @@ export function useActor(actorRef, getSnapshot) {
|
|
|
45
45
|
}
|
|
46
46
|
var event = args[0];
|
|
47
47
|
if (process.env.NODE_ENV !== 'production' && args.length > 1) {
|
|
48
|
-
console.warn("Unexpected payload: "
|
|
48
|
+
console.warn("Unexpected payload: ".concat(JSON.stringify(args[1]), ". Only a single event object can be sent to actor send() functions."));
|
|
49
49
|
}
|
|
50
50
|
var currentActorRef = actorRefRef.current;
|
|
51
51
|
// If the previous actor is a deferred actor,
|
package/es/useInterpret.d.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { AreAllImplementationsAssumedToBeProvided, InternalMachineOptions, InterpreterFrom, InterpreterOptions, Observer, State, StateMachine } from 'xstate';
|
|
2
2
|
import { MaybeLazy } from './types';
|
|
3
3
|
import { UseMachineOptions } from './useMachine';
|
|
4
|
-
declare type RestParams<TMachine extends StateMachine<any, any, any, any, any, any>> = AreAllImplementationsAssumedToBeProvided<TMachine['__TResolvedTypesMeta']> extends false ? [
|
|
4
|
+
declare type RestParams<TMachine extends StateMachine<any, any, any, any, any, any, any>> = AreAllImplementationsAssumedToBeProvided<TMachine['__TResolvedTypesMeta']> extends false ? [
|
|
5
5
|
options: InterpreterOptions & UseMachineOptions<TMachine['__TContext'], TMachine['__TEvent']> & InternalMachineOptions<TMachine['__TContext'], TMachine['__TEvent'], TMachine['__TResolvedTypesMeta'], true>,
|
|
6
6
|
observerOrListener?: Observer<State<TMachine['__TContext'], TMachine['__TEvent'], any, TMachine['__TTypestate'], TMachine['__TResolvedTypesMeta']>> | ((value: State<TMachine['__TContext'], TMachine['__TEvent'], any, TMachine['__TTypestate'], TMachine['__TResolvedTypesMeta']>) => void)
|
|
7
7
|
] : [
|
|
8
8
|
options?: InterpreterOptions & UseMachineOptions<TMachine['__TContext'], TMachine['__TEvent']> & InternalMachineOptions<TMachine['__TContext'], TMachine['__TEvent'], TMachine['__TResolvedTypesMeta']>,
|
|
9
9
|
observerOrListener?: Observer<State<TMachine['__TContext'], TMachine['__TEvent'], any, TMachine['__TTypestate'], TMachine['__TResolvedTypesMeta']>> | ((value: State<TMachine['__TContext'], TMachine['__TEvent'], any, TMachine['__TTypestate'], TMachine['__TResolvedTypesMeta']>) => void)
|
|
10
10
|
];
|
|
11
|
-
export declare function useInterpret<TMachine extends StateMachine<any, any, any, any, any, any>>(getMachine: MaybeLazy<TMachine>, ...[options, observerOrListener]: RestParams<TMachine>): InterpreterFrom<TMachine>;
|
|
11
|
+
export declare function useInterpret<TMachine extends StateMachine<any, any, any, any, any, any, any>>(getMachine: MaybeLazy<TMachine>, ...[options, observerOrListener]: RestParams<TMachine>): InterpreterFrom<TMachine>;
|
|
12
12
|
export {};
|
|
13
13
|
//# sourceMappingURL=useInterpret.d.ts.map
|
package/es/useMachine.d.ts
CHANGED
|
@@ -13,12 +13,12 @@ export interface UseMachineOptions<TContext, TEvent extends EventObject> {
|
|
|
13
13
|
*/
|
|
14
14
|
state?: StateConfig<TContext, TEvent>;
|
|
15
15
|
}
|
|
16
|
-
declare type RestParams<TMachine extends StateMachine<any, any, any, any, any, any>> = AreAllImplementationsAssumedToBeProvided<TMachine['__TResolvedTypesMeta']> extends false ? [
|
|
16
|
+
declare type RestParams<TMachine extends StateMachine<any, any, any, any, any, any, any>> = AreAllImplementationsAssumedToBeProvided<TMachine['__TResolvedTypesMeta']> extends false ? [
|
|
17
17
|
options: InterpreterOptions & UseMachineOptions<TMachine['__TContext'], TMachine['__TEvent']> & InternalMachineOptions<TMachine['__TContext'], TMachine['__TEvent'], TMachine['__TResolvedTypesMeta'], true>
|
|
18
18
|
] : [
|
|
19
19
|
options?: InterpreterOptions & UseMachineOptions<TMachine['__TContext'], TMachine['__TEvent']> & InternalMachineOptions<TMachine['__TContext'], TMachine['__TEvent'], TMachine['__TResolvedTypesMeta']>
|
|
20
20
|
];
|
|
21
|
-
declare type UseMachineReturn<TMachine extends StateMachine<any, any, any, any, any, any>, TInterpreter = InterpreterFrom<TMachine>> = [StateFrom<TMachine>, Prop<TInterpreter, 'send'>, TInterpreter];
|
|
22
|
-
export declare function useMachine<TMachine extends StateMachine<any, any, any, any, any, any>>(getMachine: MaybeLazy<TMachine>, ...[options]: RestParams<TMachine>): UseMachineReturn<TMachine>;
|
|
21
|
+
declare type UseMachineReturn<TMachine extends StateMachine<any, any, any, any, any, any, any>, TInterpreter = InterpreterFrom<TMachine>> = [StateFrom<TMachine>, Prop<TInterpreter, 'send'>, TInterpreter];
|
|
22
|
+
export declare function useMachine<TMachine extends StateMachine<any, any, any, any, any, any, any>>(getMachine: MaybeLazy<TMachine>, ...[options]: RestParams<TMachine>): UseMachineReturn<TMachine>;
|
|
23
23
|
export {};
|
|
24
24
|
//# sourceMappingURL=useMachine.d.ts.map
|
package/es/useMachine.js
CHANGED
|
@@ -14,10 +14,14 @@ var __read = (this && this.__read) || function (o, n) {
|
|
|
14
14
|
}
|
|
15
15
|
return ar;
|
|
16
16
|
};
|
|
17
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from) {
|
|
18
|
-
for (var i = 0,
|
|
19
|
-
|
|
20
|
-
|
|
17
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
18
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
19
|
+
if (ar || !(i in from)) {
|
|
20
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
21
|
+
ar[i] = from[i];
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
21
25
|
};
|
|
22
26
|
import { useCallback, useState } from 'react';
|
|
23
27
|
import { State } from 'xstate';
|
|
@@ -31,11 +35,11 @@ function createReactActionFunction(exec, tag) {
|
|
|
31
35
|
}
|
|
32
36
|
// don't execute; just return
|
|
33
37
|
return function () {
|
|
34
|
-
return exec.apply(void 0, __spreadArray([], __read(args)));
|
|
38
|
+
return exec.apply(void 0, __spreadArray([], __read(args), false));
|
|
35
39
|
};
|
|
36
40
|
};
|
|
37
41
|
Object.defineProperties(effectExec, {
|
|
38
|
-
name: { value: "effect:"
|
|
42
|
+
name: { value: "effect:".concat(exec.name) },
|
|
39
43
|
__effect: { value: tag }
|
|
40
44
|
});
|
|
41
45
|
return effectExec;
|
|
@@ -14,10 +14,14 @@ var __read = (this && this.__read) || function (o, n) {
|
|
|
14
14
|
}
|
|
15
15
|
return ar;
|
|
16
16
|
};
|
|
17
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from) {
|
|
18
|
-
for (var i = 0,
|
|
19
|
-
|
|
20
|
-
|
|
17
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
18
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
19
|
+
if (ar || !(i in from)) {
|
|
20
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
21
|
+
ar[i] = from[i];
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
21
25
|
};
|
|
22
26
|
import { useEffect, useRef } from 'react';
|
|
23
27
|
import useIsomorphicLayoutEffect from 'use-isomorphic-layout-effect';
|
|
@@ -46,8 +50,8 @@ export function useReactEffectActions(service) {
|
|
|
46
50
|
var _c = __read(partition(reactEffectActions, function (action) {
|
|
47
51
|
return action.exec.__effect === ReactEffectType.Effect;
|
|
48
52
|
}), 2), effectActions = _c[0], layoutEffectActions = _c[1];
|
|
49
|
-
(_a = effectActionsRef.current).push.apply(_a, __spreadArray([], __read(effectActions.map(function (effectAction) { return [effectAction, currentState]; }))));
|
|
50
|
-
(_b = layoutEffectActionsRef.current).push.apply(_b, __spreadArray([], __read(layoutEffectActions.map(function (layoutEffectAction) { return [layoutEffectAction, currentState]; }))));
|
|
53
|
+
(_a = effectActionsRef.current).push.apply(_a, __spreadArray([], __read(effectActions.map(function (effectAction) { return [effectAction, currentState]; })), false));
|
|
54
|
+
(_b = layoutEffectActionsRef.current).push.apply(_b, __spreadArray([], __read(layoutEffectActions.map(function (layoutEffectAction) { return [layoutEffectAction, currentState]; })), false));
|
|
51
55
|
}
|
|
52
56
|
});
|
|
53
57
|
return function () {
|
|
@@ -62,7 +66,7 @@ export function useReactEffectActions(service) {
|
|
|
62
66
|
var _a = __read(layoutEffectActionsRef.current.shift(), 2), layoutEffectAction = _a[0], effectState = _a[1];
|
|
63
67
|
executeEffect(layoutEffectAction, effectState);
|
|
64
68
|
}
|
|
65
|
-
}); // https://github.com/
|
|
69
|
+
}); // https://github.com/statelyai/xstate/pull/1202#discussion_r429677773
|
|
66
70
|
useEffect(function () {
|
|
67
71
|
while (effectActionsRef.current.length) {
|
|
68
72
|
var _a = __read(effectActionsRef.current.shift(), 2), effectAction = _a[0], effectState = _a[1];
|
package/es/useSelector.js
CHANGED
|
@@ -2,7 +2,7 @@ import { useMemo, useRef } from 'react';
|
|
|
2
2
|
import { useSubscription } from 'use-subscription';
|
|
3
3
|
import useIsomorphicLayoutEffect from 'use-isomorphic-layout-effect';
|
|
4
4
|
import { isActorWithState } from './useActor';
|
|
5
|
-
import { getServiceSnapshot } from './
|
|
5
|
+
import { getServiceSnapshot } from './utils';
|
|
6
6
|
function isService(actor) {
|
|
7
7
|
return 'state' in actor && 'machine' in actor;
|
|
8
8
|
}
|
|
@@ -49,17 +49,22 @@ export function useSelector(actor, selector, compare, getSnapshot) {
|
|
|
49
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)
|
|
50
50
|
}, [actor]);
|
|
51
51
|
var currentSelected = useSubscription(subscription);
|
|
52
|
+
var currentChanged = false;
|
|
52
53
|
if (latestSelectorRef.current !== selector) {
|
|
53
54
|
var selected = selector(subscription.getSnapshot());
|
|
54
55
|
if (!compare(currentSelected, selected)) {
|
|
56
|
+
currentChanged = true;
|
|
55
57
|
currentSelected = selected;
|
|
56
58
|
}
|
|
57
59
|
}
|
|
58
60
|
useIsomorphicLayoutEffect(function () {
|
|
59
61
|
latestSelectorRef.current = selector;
|
|
60
|
-
//
|
|
61
|
-
|
|
62
|
-
|
|
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
|
+
}
|
|
63
68
|
});
|
|
64
69
|
return currentSelected;
|
|
65
70
|
}
|
package/es/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/es/utils.js
CHANGED
package/lib/fsm.js
CHANGED
|
@@ -32,22 +32,22 @@ var getServiceState = function (service) {
|
|
|
32
32
|
};
|
|
33
33
|
function useMachine(stateMachine, options) {
|
|
34
34
|
if (process.env.NODE_ENV !== 'production') {
|
|
35
|
-
var _a = __read(react_1.useState(stateMachine), 1), initialMachine = _a[0];
|
|
35
|
+
var _a = __read((0, react_1.useState)(stateMachine), 1), initialMachine = _a[0];
|
|
36
36
|
if (stateMachine !== initialMachine) {
|
|
37
37
|
console.warn('Machine given to `useMachine` has changed between renders. This is not supported and might lead to unexpected results.\n' +
|
|
38
38
|
'Please make sure that you pass the same Machine as argument each time.');
|
|
39
39
|
}
|
|
40
40
|
}
|
|
41
|
-
var service = useConstant_1.default(function () {
|
|
42
|
-
return fsm_1.interpret(fsm_1.createMachine(stateMachine.config, options ? options : stateMachine._options)).start();
|
|
41
|
+
var service = (0, useConstant_1.default)(function () {
|
|
42
|
+
return (0, fsm_1.interpret)((0, fsm_1.createMachine)(stateMachine.config, options ? options : stateMachine._options)).start();
|
|
43
43
|
});
|
|
44
|
-
var _b = __read(react_1.useState(function () { return getServiceState(service); }), 2), state = _b[0], setState = _b[1];
|
|
45
|
-
react_1.useEffect(function () {
|
|
44
|
+
var _b = __read((0, react_1.useState)(function () { return getServiceState(service); }), 2), state = _b[0], setState = _b[1];
|
|
45
|
+
(0, react_1.useEffect)(function () {
|
|
46
46
|
if (options) {
|
|
47
47
|
service._machine._options = options;
|
|
48
48
|
}
|
|
49
49
|
});
|
|
50
|
-
react_1.useEffect(function () {
|
|
50
|
+
(0, react_1.useEffect)(function () {
|
|
51
51
|
service.subscribe(setState);
|
|
52
52
|
return function () {
|
|
53
53
|
service.stop();
|
|
@@ -57,7 +57,7 @@ function useMachine(stateMachine, options) {
|
|
|
57
57
|
}
|
|
58
58
|
exports.useMachine = useMachine;
|
|
59
59
|
function useService(service) {
|
|
60
|
-
var subscription = react_1.useMemo(function () {
|
|
60
|
+
var subscription = (0, react_1.useMemo)(function () {
|
|
61
61
|
var currentState = getServiceState(service);
|
|
62
62
|
return {
|
|
63
63
|
getCurrentValue: function () { return currentState; },
|
|
@@ -72,7 +72,7 @@ function useService(service) {
|
|
|
72
72
|
}
|
|
73
73
|
};
|
|
74
74
|
}, [service]);
|
|
75
|
-
var state = use_subscription_1.useSubscription(subscription);
|
|
75
|
+
var state = (0, use_subscription_1.useSubscription)(subscription);
|
|
76
76
|
return [state, service.send, service];
|
|
77
77
|
}
|
|
78
78
|
exports.useService = useService;
|
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
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.asLayoutEffect = exports.asEffect = 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
6
|
Object.defineProperty(exports, "asEffect", { enumerable: true, get: function () { return useMachine_1.asEffect; } });
|
|
7
7
|
Object.defineProperty(exports, "asLayoutEffect", { enumerable: true, get: function () { return useMachine_1.asLayoutEffect; } });
|
|
8
|
-
var useService_1 = require("./useService");
|
|
9
|
-
Object.defineProperty(exports, "useService", { enumerable: true, get: function () { return useService_1.useService; } });
|
|
10
8
|
var useActor_1 = require("./useActor");
|
|
11
9
|
Object.defineProperty(exports, "useActor", { enumerable: true, get: function () { return useActor_1.useActor; } });
|
|
12
10
|
var useInterpret_1 = require("./useInterpret");
|
package/lib/useActor.js
CHANGED
|
@@ -39,17 +39,17 @@ function defaultGetSnapshot(actorRef) {
|
|
|
39
39
|
}
|
|
40
40
|
function useActor(actorRef, getSnapshot) {
|
|
41
41
|
if (getSnapshot === void 0) { getSnapshot = defaultGetSnapshot; }
|
|
42
|
-
var actorRefRef = react_1.useRef(actorRef);
|
|
43
|
-
var deferredEventsRef = react_1.useRef([]);
|
|
44
|
-
var _a = __read(react_1.useState(function () { return getSnapshot(actorRef); }), 2), current = _a[0], setCurrent = _a[1];
|
|
45
|
-
var send = useConstant_1.default(function () { return function () {
|
|
42
|
+
var actorRefRef = (0, react_1.useRef)(actorRef);
|
|
43
|
+
var deferredEventsRef = (0, react_1.useRef)([]);
|
|
44
|
+
var _a = __read((0, react_1.useState)(function () { return getSnapshot(actorRef); }), 2), current = _a[0], setCurrent = _a[1];
|
|
45
|
+
var send = (0, useConstant_1.default)(function () { return function () {
|
|
46
46
|
var args = [];
|
|
47
47
|
for (var _i = 0; _i < arguments.length; _i++) {
|
|
48
48
|
args[_i] = arguments[_i];
|
|
49
49
|
}
|
|
50
50
|
var event = args[0];
|
|
51
51
|
if (process.env.NODE_ENV !== 'production' && args.length > 1) {
|
|
52
|
-
console.warn("Unexpected payload: "
|
|
52
|
+
console.warn("Unexpected payload: ".concat(JSON.stringify(args[1]), ". Only a single event object can be sent to actor send() functions."));
|
|
53
53
|
}
|
|
54
54
|
var currentActorRef = actorRefRef.current;
|
|
55
55
|
// If the previous actor is a deferred actor,
|
|
@@ -62,7 +62,7 @@ function useActor(actorRef, getSnapshot) {
|
|
|
62
62
|
currentActorRef.send(event);
|
|
63
63
|
}
|
|
64
64
|
}; });
|
|
65
|
-
use_isomorphic_layout_effect_1.default(function () {
|
|
65
|
+
(0, use_isomorphic_layout_effect_1.default)(function () {
|
|
66
66
|
actorRefRef.current = actorRef;
|
|
67
67
|
setCurrent(getSnapshot(actorRef));
|
|
68
68
|
var subscription = actorRef.subscribe({
|
package/lib/useInterpret.d.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { AreAllImplementationsAssumedToBeProvided, InternalMachineOptions, InterpreterFrom, InterpreterOptions, Observer, State, StateMachine } from 'xstate';
|
|
2
2
|
import { MaybeLazy } from './types';
|
|
3
3
|
import { UseMachineOptions } from './useMachine';
|
|
4
|
-
declare type RestParams<TMachine extends StateMachine<any, any, any, any, any, any>> = AreAllImplementationsAssumedToBeProvided<TMachine['__TResolvedTypesMeta']> extends false ? [
|
|
4
|
+
declare type RestParams<TMachine extends StateMachine<any, any, any, any, any, any, any>> = AreAllImplementationsAssumedToBeProvided<TMachine['__TResolvedTypesMeta']> extends false ? [
|
|
5
5
|
options: InterpreterOptions & UseMachineOptions<TMachine['__TContext'], TMachine['__TEvent']> & InternalMachineOptions<TMachine['__TContext'], TMachine['__TEvent'], TMachine['__TResolvedTypesMeta'], true>,
|
|
6
6
|
observerOrListener?: Observer<State<TMachine['__TContext'], TMachine['__TEvent'], any, TMachine['__TTypestate'], TMachine['__TResolvedTypesMeta']>> | ((value: State<TMachine['__TContext'], TMachine['__TEvent'], any, TMachine['__TTypestate'], TMachine['__TResolvedTypesMeta']>) => void)
|
|
7
7
|
] : [
|
|
8
8
|
options?: InterpreterOptions & UseMachineOptions<TMachine['__TContext'], TMachine['__TEvent']> & InternalMachineOptions<TMachine['__TContext'], TMachine['__TEvent'], TMachine['__TResolvedTypesMeta']>,
|
|
9
9
|
observerOrListener?: Observer<State<TMachine['__TContext'], TMachine['__TEvent'], any, TMachine['__TTypestate'], TMachine['__TResolvedTypesMeta']>> | ((value: State<TMachine['__TContext'], TMachine['__TEvent'], any, TMachine['__TTypestate'], TMachine['__TResolvedTypesMeta']>) => void)
|
|
10
10
|
];
|
|
11
|
-
export declare function useInterpret<TMachine extends StateMachine<any, any, any, any, any, any>>(getMachine: MaybeLazy<TMachine>, ...[options, observerOrListener]: RestParams<TMachine>): InterpreterFrom<TMachine>;
|
|
11
|
+
export declare function useInterpret<TMachine extends StateMachine<any, any, any, any, any, any, any>>(getMachine: MaybeLazy<TMachine>, ...[options, observerOrListener]: RestParams<TMachine>): InterpreterFrom<TMachine>;
|
|
12
12
|
export {};
|
|
13
13
|
//# sourceMappingURL=useInterpret.d.ts.map
|
package/lib/useInterpret.js
CHANGED
|
@@ -63,12 +63,12 @@ function useInterpret(getMachine) {
|
|
|
63
63
|
_a[_i - 1] = arguments[_i];
|
|
64
64
|
}
|
|
65
65
|
var _b = __read(_a, 2), _c = _b[0], options = _c === void 0 ? {} : _c, observerOrListener = _b[1];
|
|
66
|
-
var machine = useConstant_1.default(function () {
|
|
66
|
+
var machine = (0, useConstant_1.default)(function () {
|
|
67
67
|
return typeof getMachine === 'function' ? getMachine() : getMachine;
|
|
68
68
|
});
|
|
69
69
|
if (process.env.NODE_ENV !== 'production' &&
|
|
70
70
|
typeof getMachine !== 'function') {
|
|
71
|
-
var _d = __read(react_1.useState(machine), 1), initialMachine = _d[0];
|
|
71
|
+
var _d = __read((0, react_1.useState)(machine), 1), initialMachine = _d[0];
|
|
72
72
|
if (getMachine !== initialMachine) {
|
|
73
73
|
console.warn('Machine given to `useMachine` has changed between renders. This is not supported and might lead to unexpected results.\n' +
|
|
74
74
|
'Please make sure that you pass the same Machine as argument each time.');
|
|
@@ -77,7 +77,7 @@ function useInterpret(getMachine) {
|
|
|
77
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
78
|
// it's not defined in `TypegenMachineOptions` so we can't just unpack this property here freely
|
|
79
79
|
var activities = options.activities;
|
|
80
|
-
var service = useConstant_1.default(function () {
|
|
80
|
+
var service = (0, useConstant_1.default)(function () {
|
|
81
81
|
var machineConfig = {
|
|
82
82
|
context: context,
|
|
83
83
|
guards: guards,
|
|
@@ -87,9 +87,9 @@ function useInterpret(getMachine) {
|
|
|
87
87
|
delays: delays
|
|
88
88
|
};
|
|
89
89
|
var machineWithConfig = machine.withConfig(machineConfig, function () { return (__assign(__assign({}, machine.context), context)); });
|
|
90
|
-
return xstate_1.interpret(machineWithConfig, __assign({ deferEvents: true }, interpreterOptions));
|
|
90
|
+
return (0, xstate_1.interpret)(machineWithConfig, __assign({ deferEvents: true }, interpreterOptions));
|
|
91
91
|
});
|
|
92
|
-
use_isomorphic_layout_effect_1.default(function () {
|
|
92
|
+
(0, use_isomorphic_layout_effect_1.default)(function () {
|
|
93
93
|
var sub;
|
|
94
94
|
if (observerOrListener) {
|
|
95
95
|
sub = service.subscribe(toObserver(observerOrListener));
|
|
@@ -98,7 +98,7 @@ function useInterpret(getMachine) {
|
|
|
98
98
|
sub === null || sub === void 0 ? void 0 : sub.unsubscribe();
|
|
99
99
|
};
|
|
100
100
|
}, [observerOrListener]);
|
|
101
|
-
use_isomorphic_layout_effect_1.default(function () {
|
|
101
|
+
(0, use_isomorphic_layout_effect_1.default)(function () {
|
|
102
102
|
service.start(rehydratedState ? xstate_1.State.create(rehydratedState) : undefined);
|
|
103
103
|
return function () {
|
|
104
104
|
service.stop();
|
|
@@ -107,14 +107,14 @@ function useInterpret(getMachine) {
|
|
|
107
107
|
// Make sure options are kept updated when they change.
|
|
108
108
|
// This mutation assignment is safe because the service instance is only used
|
|
109
109
|
// in one place -- this hook's caller.
|
|
110
|
-
use_isomorphic_layout_effect_1.default(function () {
|
|
110
|
+
(0, use_isomorphic_layout_effect_1.default)(function () {
|
|
111
111
|
Object.assign(service.machine.options.actions, actions);
|
|
112
112
|
Object.assign(service.machine.options.guards, guards);
|
|
113
113
|
Object.assign(service.machine.options.activities, activities);
|
|
114
114
|
Object.assign(service.machine.options.services, services);
|
|
115
115
|
Object.assign(service.machine.options.delays, delays);
|
|
116
116
|
}, [actions, guards, activities, services, delays]);
|
|
117
|
-
useReactEffectActions_1.useReactEffectActions(service);
|
|
117
|
+
(0, useReactEffectActions_1.useReactEffectActions)(service);
|
|
118
118
|
return service;
|
|
119
119
|
}
|
|
120
120
|
exports.useInterpret = useInterpret;
|
package/lib/useMachine.d.ts
CHANGED
|
@@ -13,12 +13,12 @@ export interface UseMachineOptions<TContext, TEvent extends EventObject> {
|
|
|
13
13
|
*/
|
|
14
14
|
state?: StateConfig<TContext, TEvent>;
|
|
15
15
|
}
|
|
16
|
-
declare type RestParams<TMachine extends StateMachine<any, any, any, any, any, any>> = AreAllImplementationsAssumedToBeProvided<TMachine['__TResolvedTypesMeta']> extends false ? [
|
|
16
|
+
declare type RestParams<TMachine extends StateMachine<any, any, any, any, any, any, any>> = AreAllImplementationsAssumedToBeProvided<TMachine['__TResolvedTypesMeta']> extends false ? [
|
|
17
17
|
options: InterpreterOptions & UseMachineOptions<TMachine['__TContext'], TMachine['__TEvent']> & InternalMachineOptions<TMachine['__TContext'], TMachine['__TEvent'], TMachine['__TResolvedTypesMeta'], true>
|
|
18
18
|
] : [
|
|
19
19
|
options?: InterpreterOptions & UseMachineOptions<TMachine['__TContext'], TMachine['__TEvent']> & InternalMachineOptions<TMachine['__TContext'], TMachine['__TEvent'], TMachine['__TResolvedTypesMeta']>
|
|
20
20
|
];
|
|
21
|
-
declare type UseMachineReturn<TMachine extends StateMachine<any, any, any, any, any, any>, TInterpreter = InterpreterFrom<TMachine>> = [StateFrom<TMachine>, Prop<TInterpreter, 'send'>, TInterpreter];
|
|
22
|
-
export declare function useMachine<TMachine extends StateMachine<any, any, any, any, any, any>>(getMachine: MaybeLazy<TMachine>, ...[options]: RestParams<TMachine>): UseMachineReturn<TMachine>;
|
|
21
|
+
declare type UseMachineReturn<TMachine extends StateMachine<any, any, any, any, any, any, any>, TInterpreter = InterpreterFrom<TMachine>> = [StateFrom<TMachine>, Prop<TInterpreter, 'send'>, TInterpreter];
|
|
22
|
+
export declare function useMachine<TMachine extends StateMachine<any, any, any, any, any, any, any>>(getMachine: MaybeLazy<TMachine>, ...[options]: RestParams<TMachine>): UseMachineReturn<TMachine>;
|
|
23
23
|
export {};
|
|
24
24
|
//# sourceMappingURL=useMachine.d.ts.map
|
package/lib/useMachine.js
CHANGED
|
@@ -15,10 +15,14 @@ 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,
|
|
20
|
-
|
|
21
|
-
|
|
18
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
19
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
20
|
+
if (ar || !(i in from)) {
|
|
21
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
22
|
+
ar[i] = from[i];
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
22
26
|
};
|
|
23
27
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
24
28
|
exports.useMachine = exports.asLayoutEffect = exports.asEffect = void 0;
|
|
@@ -34,11 +38,11 @@ function createReactActionFunction(exec, tag) {
|
|
|
34
38
|
}
|
|
35
39
|
// don't execute; just return
|
|
36
40
|
return function () {
|
|
37
|
-
return exec.apply(void 0, __spreadArray([], __read(args)));
|
|
41
|
+
return exec.apply(void 0, __spreadArray([], __read(args), false));
|
|
38
42
|
};
|
|
39
43
|
};
|
|
40
44
|
Object.defineProperties(effectExec, {
|
|
41
|
-
name: { value: "effect:"
|
|
45
|
+
name: { value: "effect:".concat(exec.name) },
|
|
42
46
|
__effect: { value: tag }
|
|
43
47
|
});
|
|
44
48
|
return effectExec;
|
|
@@ -57,7 +61,7 @@ function useMachine(getMachine) {
|
|
|
57
61
|
_a[_i - 1] = arguments[_i];
|
|
58
62
|
}
|
|
59
63
|
var _b = __read(_a, 1), _c = _b[0], options = _c === void 0 ? {} : _c;
|
|
60
|
-
var listener = react_1.useCallback(function (nextState) {
|
|
64
|
+
var listener = (0, react_1.useCallback)(function (nextState) {
|
|
61
65
|
// Only change the current state if:
|
|
62
66
|
// - the incoming state is the "live" initial state (since it might have new actors)
|
|
63
67
|
// - OR the incoming state actually changed.
|
|
@@ -68,8 +72,8 @@ function useMachine(getMachine) {
|
|
|
68
72
|
setState(nextState);
|
|
69
73
|
}
|
|
70
74
|
}, []);
|
|
71
|
-
var service = useInterpret_1.useInterpret(getMachine, options, listener);
|
|
72
|
-
var _d = __read(react_1.useState(function () {
|
|
75
|
+
var service = (0, useInterpret_1.useInterpret)(getMachine, options, listener);
|
|
76
|
+
var _d = __read((0, react_1.useState)(function () {
|
|
73
77
|
var initialState = service.machine.initialState;
|
|
74
78
|
return (options.state
|
|
75
79
|
? xstate_1.State.create(options.state)
|
|
@@ -15,10 +15,14 @@ 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,
|
|
20
|
-
|
|
21
|
-
|
|
18
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
19
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
20
|
+
if (ar || !(i in from)) {
|
|
21
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
22
|
+
ar[i] = from[i];
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
22
26
|
};
|
|
23
27
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
24
28
|
exports.useReactEffectActions = void 0;
|
|
@@ -36,9 +40,9 @@ function executeEffect(action, state) {
|
|
|
36
40
|
originalExec();
|
|
37
41
|
}
|
|
38
42
|
function useReactEffectActions(service) {
|
|
39
|
-
var effectActionsRef = react_1.useRef([]);
|
|
40
|
-
var layoutEffectActionsRef = react_1.useRef([]);
|
|
41
|
-
use_isomorphic_layout_effect_1.default(function () {
|
|
43
|
+
var effectActionsRef = (0, react_1.useRef)([]);
|
|
44
|
+
var layoutEffectActionsRef = (0, react_1.useRef)([]);
|
|
45
|
+
(0, use_isomorphic_layout_effect_1.default)(function () {
|
|
42
46
|
var sub = service.subscribe(function (currentState) {
|
|
43
47
|
var _a, _b;
|
|
44
48
|
if (currentState.actions.length) {
|
|
@@ -46,11 +50,11 @@ function useReactEffectActions(service) {
|
|
|
46
50
|
return (typeof action.exec === 'function' &&
|
|
47
51
|
'__effect' in action.exec);
|
|
48
52
|
});
|
|
49
|
-
var _c = __read(utils_1.partition(reactEffectActions, function (action) {
|
|
53
|
+
var _c = __read((0, utils_1.partition)(reactEffectActions, function (action) {
|
|
50
54
|
return action.exec.__effect === types_1.ReactEffectType.Effect;
|
|
51
55
|
}), 2), effectActions = _c[0], layoutEffectActions = _c[1];
|
|
52
|
-
(_a = effectActionsRef.current).push.apply(_a, __spreadArray([], __read(effectActions.map(function (effectAction) { return [effectAction, currentState]; }))));
|
|
53
|
-
(_b = layoutEffectActionsRef.current).push.apply(_b, __spreadArray([], __read(layoutEffectActions.map(function (layoutEffectAction) { return [layoutEffectAction, currentState]; }))));
|
|
56
|
+
(_a = effectActionsRef.current).push.apply(_a, __spreadArray([], __read(effectActions.map(function (effectAction) { return [effectAction, currentState]; })), false));
|
|
57
|
+
(_b = layoutEffectActionsRef.current).push.apply(_b, __spreadArray([], __read(layoutEffectActions.map(function (layoutEffectAction) { return [layoutEffectAction, currentState]; })), false));
|
|
54
58
|
}
|
|
55
59
|
});
|
|
56
60
|
return function () {
|
|
@@ -60,13 +64,13 @@ function useReactEffectActions(service) {
|
|
|
60
64
|
// this is somewhat weird - this should always be flushed within useLayoutEffect
|
|
61
65
|
// but we don't want to receive warnings about useLayoutEffect being used on the server
|
|
62
66
|
// so we have to use `useIsomorphicLayoutEffect` to silence those warnings
|
|
63
|
-
use_isomorphic_layout_effect_1.default(function () {
|
|
67
|
+
(0, use_isomorphic_layout_effect_1.default)(function () {
|
|
64
68
|
while (layoutEffectActionsRef.current.length) {
|
|
65
69
|
var _a = __read(layoutEffectActionsRef.current.shift(), 2), layoutEffectAction = _a[0], effectState = _a[1];
|
|
66
70
|
executeEffect(layoutEffectAction, effectState);
|
|
67
71
|
}
|
|
68
|
-
}); // https://github.com/
|
|
69
|
-
react_1.useEffect(function () {
|
|
72
|
+
}); // https://github.com/statelyai/xstate/pull/1202#discussion_r429677773
|
|
73
|
+
(0, react_1.useEffect)(function () {
|
|
70
74
|
while (effectActionsRef.current.length) {
|
|
71
75
|
var _a = __read(effectActionsRef.current.shift(), 2), effectAction = _a[0], effectState = _a[1];
|
|
72
76
|
executeEffect(effectAction, effectState);
|
package/lib/useSelector.js
CHANGED
|
@@ -5,23 +5,23 @@ var react_1 = require("react");
|
|
|
5
5
|
var use_subscription_1 = require("use-subscription");
|
|
6
6
|
var use_isomorphic_layout_effect_1 = require("use-isomorphic-layout-effect");
|
|
7
7
|
var useActor_1 = require("./useActor");
|
|
8
|
-
var
|
|
8
|
+
var utils_1 = require("./utils");
|
|
9
9
|
function isService(actor) {
|
|
10
10
|
return 'state' in actor && 'machine' in actor;
|
|
11
11
|
}
|
|
12
12
|
var defaultCompare = function (a, b) { return a === b; };
|
|
13
13
|
var defaultGetSnapshot = function (a) {
|
|
14
14
|
return isService(a)
|
|
15
|
-
?
|
|
16
|
-
: useActor_1.isActorWithState(a)
|
|
15
|
+
? (0, utils_1.getServiceSnapshot)(a)
|
|
16
|
+
: (0, useActor_1.isActorWithState)(a)
|
|
17
17
|
? a.state
|
|
18
18
|
: undefined;
|
|
19
19
|
};
|
|
20
20
|
function useSelector(actor, selector, compare, getSnapshot) {
|
|
21
21
|
if (compare === void 0) { compare = defaultCompare; }
|
|
22
22
|
if (getSnapshot === void 0) { getSnapshot = defaultGetSnapshot; }
|
|
23
|
-
var latestSelectorRef = react_1.useRef(selector);
|
|
24
|
-
var subscription = react_1.useMemo(function () {
|
|
23
|
+
var latestSelectorRef = (0, react_1.useRef)(selector);
|
|
24
|
+
var subscription = (0, react_1.useMemo)(function () {
|
|
25
25
|
var snapshot = getSnapshot(actor);
|
|
26
26
|
var current = selector(snapshot);
|
|
27
27
|
var notifySubscriber;
|
|
@@ -51,18 +51,23 @@ function useSelector(actor, selector, compare, getSnapshot) {
|
|
|
51
51
|
// - `getSnapshot`: it is only supposed to read the "initial" snapshot of an actor
|
|
52
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)
|
|
53
53
|
}, [actor]);
|
|
54
|
-
var currentSelected = use_subscription_1.useSubscription(subscription);
|
|
54
|
+
var currentSelected = (0, use_subscription_1.useSubscription)(subscription);
|
|
55
|
+
var currentChanged = false;
|
|
55
56
|
if (latestSelectorRef.current !== selector) {
|
|
56
57
|
var selected = selector(subscription.getSnapshot());
|
|
57
58
|
if (!compare(currentSelected, selected)) {
|
|
59
|
+
currentChanged = true;
|
|
58
60
|
currentSelected = selected;
|
|
59
61
|
}
|
|
60
62
|
}
|
|
61
|
-
use_isomorphic_layout_effect_1.default(function () {
|
|
63
|
+
(0, use_isomorphic_layout_effect_1.default)(function () {
|
|
62
64
|
latestSelectorRef.current = selector;
|
|
63
|
-
//
|
|
64
|
-
|
|
65
|
-
|
|
65
|
+
// this condition should not be required, but setState bailouts are currently buggy: https://github.com/facebook/react/issues/22654
|
|
66
|
+
if (currentChanged) {
|
|
67
|
+
// required so we don't cause a rerender by setting state (this could create infinite rerendering loop with inline selectors)
|
|
68
|
+
// 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
|
|
69
|
+
subscription.setCurrentValue(currentSelected);
|
|
70
|
+
}
|
|
66
71
|
});
|
|
67
72
|
return currentSelected;
|
|
68
73
|
}
|
package/lib/useSpawn.js
CHANGED
|
@@ -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 behaviors_1.spawnBehavior(behavior);
|
|
14
|
+
var actorRef = (0, useConstant_1.default)(function () {
|
|
15
|
+
return (0, behaviors_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": "2.0.0
|
|
3
|
+
"version": "2.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": "^1.
|
|
55
|
+
"@xstate/fsm": "^1.6.4",
|
|
56
56
|
"react": "^16.8.0 || ^17.0.0",
|
|
57
|
-
"xstate": "^4.
|
|
57
|
+
"xstate": "^4.29.0"
|
|
58
58
|
},
|
|
59
59
|
"peerDependenciesMeta": {
|
|
60
60
|
"@xstate/fsm": {
|
|
@@ -87,7 +87,7 @@
|
|
|
87
87
|
"rollup-plugin-terser": "^5.1.2",
|
|
88
88
|
"rollup-plugin-typescript2": "^0.30.0",
|
|
89
89
|
"ts-jest": "^26.5.6",
|
|
90
|
-
"typescript": "^4.
|
|
90
|
+
"typescript": "^4.5.2",
|
|
91
91
|
"xstate": "*"
|
|
92
92
|
}
|
|
93
93
|
}
|
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();
|
package/es/useService.d.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { EventObject, Interpreter, PayloadSender, State, TypegenConstraint, TypegenDisabled, Typestate } from 'xstate';
|
|
2
|
-
export declare function getServiceSnapshot<TService extends Interpreter<any, any, any, any>>(service: TService): TService['state'];
|
|
3
|
-
/**
|
|
4
|
-
* @deprecated Use `useActor` instead.
|
|
5
|
-
*
|
|
6
|
-
* @param service The interpreted machine
|
|
7
|
-
* @returns A tuple of the current `state` of the service and the service's `send(event)` method
|
|
8
|
-
*/
|
|
9
|
-
export declare function useService<TContext, TEvent extends EventObject, TTypestate extends Typestate<TContext> = {
|
|
10
|
-
value: any;
|
|
11
|
-
context: TContext;
|
|
12
|
-
}, TTypesMeta extends TypegenConstraint = TypegenDisabled>(service: Interpreter<TContext, any, TEvent, TTypestate, TTypesMeta>): [
|
|
13
|
-
State<TContext, TEvent, any, TTypestate, TTypesMeta>,
|
|
14
|
-
PayloadSender<TEvent>
|
|
15
|
-
];
|
|
16
|
-
//# sourceMappingURL=useService.d.ts.map
|
package/es/useService.js
DELETED
|
@@ -1,36 +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 { useActor } from './useActor';
|
|
18
|
-
export function getServiceSnapshot(service) {
|
|
19
|
-
// TODO: remove compat lines in a new major, replace literal number with InterpreterStatus then as well
|
|
20
|
-
return ('status' in service ? service.status : service._status) !== 0
|
|
21
|
-
? service.state
|
|
22
|
-
: service.machine.initialState;
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* @deprecated Use `useActor` instead.
|
|
26
|
-
*
|
|
27
|
-
* @param service The interpreted machine
|
|
28
|
-
* @returns A tuple of the current `state` of the service and the service's `send(event)` method
|
|
29
|
-
*/
|
|
30
|
-
export function useService(service) {
|
|
31
|
-
if (process.env.NODE_ENV !== 'production' && !('machine' in service)) {
|
|
32
|
-
throw new Error("Attempted to use an actor-like object instead of a service in the useService() hook. Please use the useActor() hook instead.");
|
|
33
|
-
}
|
|
34
|
-
var _a = __read(useActor(service), 1), state = _a[0];
|
|
35
|
-
return [state, service.send];
|
|
36
|
-
}
|
package/lib/useService.d.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { EventObject, Interpreter, PayloadSender, State, TypegenConstraint, TypegenDisabled, Typestate } from 'xstate';
|
|
2
|
-
export declare function getServiceSnapshot<TService extends Interpreter<any, any, any, any>>(service: TService): TService['state'];
|
|
3
|
-
/**
|
|
4
|
-
* @deprecated Use `useActor` instead.
|
|
5
|
-
*
|
|
6
|
-
* @param service The interpreted machine
|
|
7
|
-
* @returns A tuple of the current `state` of the service and the service's `send(event)` method
|
|
8
|
-
*/
|
|
9
|
-
export declare function useService<TContext, TEvent extends EventObject, TTypestate extends Typestate<TContext> = {
|
|
10
|
-
value: any;
|
|
11
|
-
context: TContext;
|
|
12
|
-
}, TTypesMeta extends TypegenConstraint = TypegenDisabled>(service: Interpreter<TContext, any, TEvent, TTypestate, TTypesMeta>): [
|
|
13
|
-
State<TContext, TEvent, any, TTypestate, TTypesMeta>,
|
|
14
|
-
PayloadSender<TEvent>
|
|
15
|
-
];
|
|
16
|
-
//# sourceMappingURL=useService.d.ts.map
|
package/lib/useService.js
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
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
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
19
|
-
exports.useService = exports.getServiceSnapshot = void 0;
|
|
20
|
-
var useActor_1 = require("./useActor");
|
|
21
|
-
function getServiceSnapshot(service) {
|
|
22
|
-
// TODO: remove compat lines in a new major, replace literal number with InterpreterStatus then as well
|
|
23
|
-
return ('status' in service ? service.status : service._status) !== 0
|
|
24
|
-
? service.state
|
|
25
|
-
: service.machine.initialState;
|
|
26
|
-
}
|
|
27
|
-
exports.getServiceSnapshot = getServiceSnapshot;
|
|
28
|
-
/**
|
|
29
|
-
* @deprecated Use `useActor` instead.
|
|
30
|
-
*
|
|
31
|
-
* @param service The interpreted machine
|
|
32
|
-
* @returns A tuple of the current `state` of the service and the service's `send(event)` method
|
|
33
|
-
*/
|
|
34
|
-
function useService(service) {
|
|
35
|
-
if (process.env.NODE_ENV !== 'production' && !('machine' in service)) {
|
|
36
|
-
throw new Error("Attempted to use an actor-like object instead of a service in the useService() hook. Please use the useActor() hook instead.");
|
|
37
|
-
}
|
|
38
|
-
var _a = __read(useActor_1.useActor(service), 1), state = _a[0];
|
|
39
|
-
return [state, service.send];
|
|
40
|
-
}
|
|
41
|
-
exports.useService = useService;
|