@xstate/react 2.0.1 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +25 -0
- package/dist/xstate-react-fsm.umd.min.js +41 -15
- package/dist/xstate-react.umd.min.js +34 -23
- package/es/fsm.d.ts +3 -19
- package/es/fsm.js +35 -26
- package/es/index.d.ts +1 -1
- package/es/index.js +1 -1
- package/es/types.d.ts +1 -16
- package/es/types.js +1 -5
- package/es/useActor.js +12 -31
- package/es/useInterpret.d.ts +2 -1
- package/es/useInterpret.js +32 -43
- package/es/useMachine.d.ts +2 -4
- package/es/useMachine.js +39 -48
- package/es/useSelector.js +11 -52
- package/lib/fsm.d.ts +3 -19
- package/lib/fsm.js +34 -25
- package/lib/index.d.ts +1 -1
- package/lib/index.js +1 -3
- package/lib/types.d.ts +1 -16
- package/lib/types.js +0 -6
- package/lib/useActor.js +11 -30
- package/lib/useInterpret.d.ts +2 -1
- package/lib/useInterpret.js +32 -42
- package/lib/useMachine.d.ts +2 -4
- package/lib/useMachine.js +37 -48
- package/lib/useSelector.js +10 -51
- package/package.json +12 -11
- package/es/useReactEffectActions.d.ts +0 -3
- package/es/useReactEffectActions.js +0 -76
- package/lib/useReactEffectActions.d.ts +0 -3
- package/lib/useReactEffectActions.js +0 -80
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,30 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 3.0.0
|
|
4
|
+
|
|
5
|
+
### Major Changes
|
|
6
|
+
|
|
7
|
+
- [#2939](https://github.com/statelyai/xstate/pull/2939) [`360e85462`](https://github.com/statelyai/xstate/commit/360e8546298c4a06b6d51d8f12c0563672dd7acf) Thanks [@Andarist](https://github.com/Andarist)! - This package now accepts React 18 as a peer dep and the implementation has been rewritten to use [`use-sync-external-store`](https://www.npmjs.com/package/use-sync-external-store) package. This doesn't break compatibility with older versions of React since we are using the shim to keep compatibility with those older versions.
|
|
8
|
+
|
|
9
|
+
* [#2939](https://github.com/statelyai/xstate/pull/2939) [`360e85462`](https://github.com/statelyai/xstate/commit/360e8546298c4a06b6d51d8f12c0563672dd7acf) Thanks [@Andarist](https://github.com/Andarist)! - `asEffect` and `asLayoutEffect` action creators were removed. They were not fitting the React model that well and could lead to issues as their existence suggested that they are easy to use.
|
|
10
|
+
|
|
11
|
+
To execute actions at those exact times you can always either just call your stuff directly from those effects or send events to the machine from those effects and execute explicit actions in response to said events.
|
|
12
|
+
|
|
13
|
+
- [#2939](https://github.com/statelyai/xstate/pull/2939) [`360e85462`](https://github.com/statelyai/xstate/commit/360e8546298c4a06b6d51d8f12c0563672dd7acf) Thanks [@Andarist](https://github.com/Andarist)! - The signatures of `useMachine` and `useService` integrating with `@xstate/fsm` were changed. They now only accept a single generic each (`TMachine` and `TService` respectively). This has been done to match their signatures with the related hooks that integrate with `xstate` itself.
|
|
14
|
+
|
|
15
|
+
### Patch Changes
|
|
16
|
+
|
|
17
|
+
- [#2939](https://github.com/statelyai/xstate/pull/2939) [`360e85462`](https://github.com/statelyai/xstate/commit/360e8546298c4a06b6d51d8f12c0563672dd7acf) Thanks [@Andarist](https://github.com/Andarist)! - In v2 we have changed signatures of `useMachine` and `useInterpret`. Instead of accepting a list of generics they now only support a single generic: `TMachine`. This change, erroneously, was only introduced to types targeting TS@4.x but the types targeting previous TS releases were still using the older signatures. This has now been fixed and users of older TS versions should now be able to leverage typegen with `@xstate/react`.
|
|
18
|
+
|
|
19
|
+
* [#2939](https://github.com/statelyai/xstate/pull/2939) [`360e85462`](https://github.com/statelyai/xstate/commit/360e8546298c4a06b6d51d8f12c0563672dd7acf) Thanks [@Andarist](https://github.com/Andarist)! - `useMachine` for `xstate` now correctly rerenders with the initial state when the internal service is being restarted. This might happen during Fast Refresh and now you shouldn't be able to observe this stale state that didn't match the actual state of the service.
|
|
20
|
+
|
|
21
|
+
- [#2939](https://github.com/statelyai/xstate/pull/2939) [`360e85462`](https://github.com/statelyai/xstate/commit/360e8546298c4a06b6d51d8f12c0563672dd7acf) Thanks [@Andarist](https://github.com/Andarist)! - `useMachine` for `@xstate/fsm` now starts the service in an effect. This avoids side-effects in render and improves the compatibility with `StrictMode`.
|
|
22
|
+
|
|
23
|
+
* [#2939](https://github.com/statelyai/xstate/pull/2939) [`360e85462`](https://github.com/statelyai/xstate/commit/360e8546298c4a06b6d51d8f12c0563672dd7acf) Thanks [@Andarist](https://github.com/Andarist)! - Implementations given to `useMachine` targeting `@xstate/fsm` are now updated in a layout effect. This avoid some stale closure problems for actions that are executed in response to events sent from layout effects.
|
|
24
|
+
|
|
25
|
+
* Updated dependencies [[`360e85462`](https://github.com/statelyai/xstate/commit/360e8546298c4a06b6d51d8f12c0563672dd7acf), [`360e85462`](https://github.com/statelyai/xstate/commit/360e8546298c4a06b6d51d8f12c0563672dd7acf)]:
|
|
26
|
+
- @xstate/fsm@2.0.0
|
|
27
|
+
|
|
3
28
|
## 2.0.1
|
|
4
29
|
|
|
5
30
|
### Patch Changes
|
|
@@ -1,4 +1,42 @@
|
|
|
1
|
-
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@xstate/fsm"),require("react")):"function"==typeof define&&define.amd?define(["exports","@xstate/fsm","react"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).XStateReactFSM={},e.XStateFSM,e.React)}(this,(function(e,t,n){"use strict";function r(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}function u(e){if(e&&e.__esModule)return e;var t=Object.create(null);return e&&Object.keys(e).forEach((function(n){if("default"!==n){var r=Object.getOwnPropertyDescriptor(e,n);Object.defineProperty(t,n,r.get?r:{enumerable:!0,get:function(){return e[n]}})}})),t.default=e,Object.freeze(t)}var o=r(n),c=u(n);var a=n.useLayoutEffect;function f(e){var t={exports:{}};return e(t,t.exports),t.exports
|
|
2
|
+
/**
|
|
3
|
+
* @license React
|
|
4
|
+
* use-sync-external-store-shim.production.min.js
|
|
5
|
+
*
|
|
6
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
7
|
+
*
|
|
8
|
+
* This source code is licensed under the MIT license found in the
|
|
9
|
+
* LICENSE file in the root directory of this source tree.
|
|
10
|
+
*/}var i="function"==typeof Object.is?Object.is:function(e,t){return e===t&&(0!==e||1/e==1/t)||e!=e&&t!=t},s=o.default.useState,l=o.default.useEffect,d=o.default.useLayoutEffect,v=o.default.useDebugValue;function p(e){var t=e.getSnapshot;e=e.value;try{var n=t();return!i(e,n)}catch(e){return!0}}var b="undefined"==typeof window||void 0===window.document||void 0===window.document.createElement?function(e,t){return t()}:function(e,t){var n=t(),r=s({inst:{value:n,getSnapshot:t}}),u=r[0].inst,o=r[1];return d((function(){u.value=n,u.getSnapshot=t,p(u)&&o({inst:u})}),[e,n,t]),l((function(){return p(u)&&o({inst:u}),e((function(){p(u)&&o({inst:u})}))}),[e]),v(n),n},y={useSyncExternalStore:void 0!==o.default.useSyncExternalStore?o.default.useSyncExternalStore:b};
|
|
11
|
+
/**
|
|
12
|
+
* @license React
|
|
13
|
+
* use-sync-external-store-shim.development.js
|
|
14
|
+
*
|
|
15
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
16
|
+
*
|
|
17
|
+
* This source code is licensed under the MIT license found in the
|
|
18
|
+
* LICENSE file in the root directory of this source tree.
|
|
19
|
+
*/
|
|
20
|
+
f((function(e,t){}));var S=f((function(e){e.exports=y}));
|
|
21
|
+
/**
|
|
22
|
+
* @license React
|
|
23
|
+
* use-sync-external-store-shim/with-selector.production.min.js
|
|
24
|
+
*
|
|
25
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
26
|
+
*
|
|
27
|
+
* This source code is licensed under the MIT license found in the
|
|
28
|
+
* LICENSE file in the root directory of this source tree.
|
|
29
|
+
*/var h="function"==typeof Object.is?Object.is:function(e,t){return e===t&&(0!==e||1/e==1/t)||e!=e&&t!=t},x=S.useSyncExternalStore,E=o.default.useRef,g=o.default.useEffect,j=o.default.useMemo,m=o.default.useDebugValue,O={useSyncExternalStoreWithSelector:function(e,t,n,r,u){var o=E(null);if(null===o.current){var c={hasValue:!1,value:null};o.current=c}else c=o.current;o=j((function(){function e(e){if(!f){if(f=!0,o=e,e=r(e),void 0!==u&&c.hasValue){var t=c.value;if(u(t,e))return a=t}return a=e}if(t=a,h(o,e))return t;var n=r(e);return void 0!==u&&u(t,n)?t:(o=e,a=n)}var o,a,f=!1,i=void 0===n?null:n;return[function(){return e(t())},null===i?void 0:function(){return e(i())}]}),[t,n,r,u]);var a=x(e,o[0],o[1]);return g((function(){c.hasValue=!0,c.value=a}),[a]),m(a),a}};
|
|
30
|
+
/**
|
|
31
|
+
* @license React
|
|
32
|
+
* use-sync-external-store-shim/with-selector.development.js
|
|
33
|
+
*
|
|
34
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
35
|
+
*
|
|
36
|
+
* This source code is licensed under the MIT license found in the
|
|
37
|
+
* LICENSE file in the root directory of this source tree.
|
|
38
|
+
*/
|
|
39
|
+
f((function(e,t){}));var w=f((function(e){e.exports=O}));function M(e){return e}var _=function(e,t){return!1===t.changed};function R(e){var t=n.useCallback((function(){return function(e){var t;return e.subscribe((function(e){t=e})).unsubscribe(),t}(e)}),[e]),r=n.useCallback((function(t){return e.subscribe(t).unsubscribe}),[e]);return[w.useSyncExternalStoreWithSelector(r,t,t,M,_),e.send,e]}e.useMachine=function(e,r){var u,o,f=n.useRef(),i=
|
|
2
40
|
/*! *****************************************************************************
|
|
3
41
|
Copyright (c) Microsoft Corporation.
|
|
4
42
|
|
|
@@ -12,17 +50,5 @@
|
|
|
12
50
|
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
13
51
|
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
14
52
|
PERFORMANCE OF THIS SOFTWARE.
|
|
15
|
-
***************************************************************************** */
|
|
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})}));
|
|
53
|
+
***************************************************************************** */
|
|
54
|
+
function(e,t){var n="function"==typeof Symbol&&e[Symbol.iterator];if(!n)return e;var r,u,o=n.call(e),c=[];try{for(;(void 0===t||t-- >0)&&!(r=o.next()).done;)c.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 c}((u=function(){var n=[],u=t.interpret(t.createMachine(e.config,r||e._options)),o=u.send;return u.send=function(e){u.status!==t.InterpreterStatus.NotStarted?(o(e),f.current=u.state):n.push(e)},[u,n]},(o=c.useRef()).current||(o.current={v:u()}),o.current.v),2),s=i[0],l=i[1];a((function(){r&&(s._machine._options=r)}));var d=R(s);return n.useEffect((function(){return s.start(f.current),l.forEach(s.send),f.current=s.state,function(){s.stop()}}),[]),d},e.useService=R,Object.defineProperty(e,"__esModule",{value:!0})}));
|
|
@@ -1,28 +1,39 @@
|
|
|
1
|
-
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("react"),require("xstate")):"function"==typeof define&&define.amd?define(["exports","react","xstate"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).XStateReact={},t.React,t.XState)}(this,(function(t,e,n){"use strict";function r(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var
|
|
2
|
-
|
|
3
|
-
|
|
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 i(t,e){var n={};for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&e.indexOf(r)<0&&(n[r]=t[r]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var u=0;for(r=Object.getOwnPropertySymbols(t);u<r.length;u++)e.indexOf(r[u])<0&&Object.prototype.propertyIsEnumerable.call(t,r[u])&&(n[r[u]]=t[r[u]])}return n}function a(t,e){var n="function"==typeof Symbol&&t[Symbol.iterator];if(!n)return t;var r,u,o=n.call(t),c=[];try{for(;(void 0===e||e-- >0)&&!(r=o.next()).done;)c.push(r.value)}catch(t){u={error:t}}finally{try{r&&!r.done&&(n=o.return)&&n.call(o)}finally{if(u)throw u.error}}return c}function f(t,e,n){if(n||2===arguments.length)for(var r,u=0,o=e.length;u<o;u++)!r&&u in e||(r||(r=Array.prototype.slice.call(e,0,u)),r[u]=e[u]);return t.concat(r||Array.prototype.slice.call(e))}!function(t){t[t.Effect=1]="Effect",t[t.LayoutEffect=2]="LayoutEffect"}(u||(u={}));var s=e.useLayoutEffect;function l(t){var n=e.useRef();return n.current||(n.current={v:t()}),n.current.v}function v(t,e){var n,r,u=a([[],[]],2),o=u[0],c=u[1];try{for(var i=function(t){var e="function"==typeof Symbol&&Symbol.iterator,n=e&&t[e],r=0;if(n)return n.call(t);if(t&&"number"==typeof t.length)return{next:function(){return t&&r>=t.length&&(t=void 0),{value:t&&t[r++],done:!t}}};throw new TypeError(e?"Object is not iterable.":"Symbol.iterator is not defined.")}(t),f=i.next();!f.done;f=i.next()){var s=f.value;e(s)?o.push(s):c.push(s)}}catch(t){n={error:t}}finally{try{f&&!f.done&&(r=i.return)&&r.call(i)}finally{if(n)throw n.error}}return[o,c]}function p(t,e){(0,t.exec)(e.context,e._event.data,{action:t,state:e,_event:e._event})()}function b(t){var n=e.useRef([]),r=e.useRef([]);s((function(){var e=t.subscribe((function(t){var e,o;if(t.actions.length){var c=a(v(t.actions.filter((function(t){return"function"==typeof t.exec&&"__effect"in t.exec})),(function(t){return t.exec.__effect===u.Effect})),2),i=c[0],s=c[1];(e=n.current).push.apply(e,f([],a(i.map((function(e){return[e,t]}))),!1)),(o=r.current).push.apply(o,f([],a(s.map((function(e){return[e,t]}))),!1))}}));return function(){e.unsubscribe()}}),[]),s((function(){for(;r.current.length;){var t=a(r.current.shift(),2);p(t[0],t[1])}})),e.useEffect((function(){for(;n.current.length;){var t=a(n.current.shift(),2);p(t[0],t[1])}}))}function y(t,e,n){if("object"==typeof t)return t;var r=function(){};return{next:t,error:e||r,complete:n||r}}function d(t){for(var e=[],r=1;r<arguments.length;r++)e[r-1]=arguments[r];var u=a(e,2),o=u[0],f=void 0===o?{}:o,v=u[1],p=l((function(){return"function"==typeof t?t():t})),d=f.context,h=f.guards,g=f.actions,O=f.services,j=f.delays,m=f.state,S=i(f,["context","guards","actions","services","delays","state"]),x=f.activities,w=l((function(){var t={context:d,guards:h,actions:g,activities:x,services:O,delays:j},e=p.withConfig(t,(function(){return c(c({},p.context),d)}));return n.interpret(e,c({deferEvents:!0},S))}));return s((function(){var t;return v&&(t=w.subscribe(y(v))),function(){null==t||t.unsubscribe()}}),[v]),s((function(){return w.start(m?n.State.create(m):void 0),function(){w.stop()}}),[]),s((function(){Object.assign(w.machine.options.actions,g),Object.assign(w.machine.options.guards,h),Object.assign(w.machine.options.activities,x),Object.assign(w.machine.options.services,O),Object.assign(w.machine.options.delays,j)}),[g,h,x,O,j]),b(w),w}function h(t,e){var n=function(){for(var e=[],n=0;n<arguments.length;n++)e[n]=arguments[n];return function(){return t.apply(void 0,f([],a(e),!1))}};return Object.defineProperties(n,{name:{value:"effect:".concat(t.name)},__effect:{value:e}}),n}function g(t){return"state"in t}function O(t){return"deferred"in t}var j=function(){};function m(t){return"getSnapshot"in t?t.getSnapshot():g(t)?t.state:void 0}function S(t){var e={exports:{}};return t(e,e.exports),e.exports
|
|
16
|
-
/*
|
|
17
|
-
object-assign
|
|
18
|
-
(c) Sindre Sorhus
|
|
19
|
-
@license MIT
|
|
20
|
-
*/}var x=Object.getOwnPropertySymbols,w=Object.prototype.hasOwnProperty,E=Object.prototype.propertyIsEnumerable;function C(t){if(null==t)throw new TypeError("Object.assign cannot be called with null or undefined");return Object(t)}var _=function(){try{if(!Object.assign)return!1;var t=new String("abc");if(t[5]="de","5"===Object.getOwnPropertyNames(t)[0])return!1;for(var e={},n=0;n<10;n++)e["_"+String.fromCharCode(n)]=n;if("0123456789"!==Object.getOwnPropertyNames(e).map((function(t){return e[t]})).join(""))return!1;var r={};return"abcdefghijklmnopqrst".split("").forEach((function(t){r[t]=t})),"abcdefghijklmnopqrst"===Object.keys(Object.assign({},r)).join("")}catch(t){return!1}}()?Object.assign:function(t,e){for(var n,r,u=C(t),o=1;o<arguments.length;o++){for(var c in n=Object(arguments[o]))w.call(n,c)&&(u[c]=n[c]);if(x){r=x(n);for(var i=0;i<r.length;i++)E.call(n,r[i])&&(u[r[i]]=n[r[i]])}}return u},P={useSubscription:function(t){var e=t.getCurrentValue,n=t.subscribe,r=o.default.useState((function(){return{getCurrentValue:e,subscribe:n,value:e()}}));t=r[0];var u=r[1];return r=t.value,t.getCurrentValue===e&&t.subscribe===n||(r=e(),u({getCurrentValue:e,subscribe:n,value:r})),o.default.useDebugValue(r),o.default.useEffect((function(){function t(){if(!r){var t=e();u((function(r){return r.getCurrentValue!==e||r.subscribe!==n||r.value===t?r:_({},r,{value:t})}))}}var r=!1,o=n(t);return t(),function(){r=!0,o()}}),[e,n]),r}},V=(S((function(t,e){})),S((function(t){t.exports=P})));
|
|
21
|
-
/** @license React vundefined
|
|
22
|
-
* use-subscription.production.min.js
|
|
1
|
+
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("react"),require("xstate")):"function"==typeof define&&define.amd?define(["exports","react","xstate"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).XStateReact={},t.React,t.XState)}(this,(function(t,e,n){"use strict";function r(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}function u(t){if(t&&t.__esModule)return t;var e=Object.create(null);return t&&Object.keys(t).forEach((function(n){if("default"!==n){var r=Object.getOwnPropertyDescriptor(t,n);Object.defineProperty(e,n,r.get?r:{enumerable:!0,get:function(){return t[n]}})}})),e.default=t,Object.freeze(e)}var a=r(e),o=u(e),i=function(){return(i=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var u in e=arguments[n])Object.prototype.hasOwnProperty.call(e,u)&&(t[u]=e[u]);return t}).apply(this,arguments)};function c(t,e){var n="function"==typeof Symbol&&t[Symbol.iterator];if(!n)return t;var r,u,a=n.call(t),o=[];try{for(;(void 0===e||e-- >0)&&!(r=a.next()).done;)o.push(r.value)}catch(t){u={error:t}}finally{try{r&&!r.done&&(n=a.return)&&n.call(a)}finally{if(u)throw u.error}}return o}function s(t){var e={exports:{}};return t(e,e.exports),e.exports
|
|
2
|
+
/**
|
|
3
|
+
* @license React
|
|
4
|
+
* use-sync-external-store-shim.production.min.js
|
|
23
5
|
*
|
|
24
6
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
25
7
|
*
|
|
26
8
|
* This source code is licensed under the MIT license found in the
|
|
27
9
|
* LICENSE file in the root directory of this source tree.
|
|
28
|
-
*/var
|
|
10
|
+
*/}var f="function"==typeof Object.is?Object.is:function(t,e){return t===e&&(0!==t||1/t==1/e)||t!=t&&e!=e},l=a.default.useState,v=a.default.useEffect,d=a.default.useLayoutEffect,p=a.default.useDebugValue;function b(t){var e=t.getSnapshot;t=t.value;try{var n=e();return!f(t,n)}catch(t){return!0}}var y="undefined"==typeof window||void 0===window.document||void 0===window.document.createElement?function(t,e){return e()}:function(t,e){var n=e(),r=l({inst:{value:n,getSnapshot:e}}),u=r[0].inst,a=r[1];return d((function(){u.value=n,u.getSnapshot=e,b(u)&&a({inst:u})}),[t,n,e]),v((function(){return b(u)&&a({inst:u}),t((function(){b(u)&&a({inst:u})}))}),[t]),p(n),n},h={useSyncExternalStore:void 0!==a.default.useSyncExternalStore?a.default.useSyncExternalStore:y};
|
|
11
|
+
/**
|
|
12
|
+
* @license React
|
|
13
|
+
* use-sync-external-store-shim.development.js
|
|
14
|
+
*
|
|
15
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
16
|
+
*
|
|
17
|
+
* This source code is licensed under the MIT license found in the
|
|
18
|
+
* LICENSE file in the root directory of this source tree.
|
|
19
|
+
*/
|
|
20
|
+
s((function(t,e){}));var S=s((function(t){t.exports=h}));
|
|
21
|
+
/**
|
|
22
|
+
* @license React
|
|
23
|
+
* use-sync-external-store-shim/with-selector.production.min.js
|
|
24
|
+
*
|
|
25
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
26
|
+
*
|
|
27
|
+
* This source code is licensed under the MIT license found in the
|
|
28
|
+
* LICENSE file in the root directory of this source tree.
|
|
29
|
+
*/var g="function"==typeof Object.is?Object.is:function(t,e){return t===e&&(0!==t||1/t==1/e)||t!=t&&e!=e},O=S.useSyncExternalStore,x=a.default.useRef,j=a.default.useEffect,m=a.default.useMemo,E=a.default.useDebugValue,w={useSyncExternalStoreWithSelector:function(t,e,n,r,u){var a=x(null);if(null===a.current){var o={hasValue:!1,value:null};a.current=o}else o=a.current;a=m((function(){function t(t){if(!c){if(c=!0,a=t,t=r(t),void 0!==u&&o.hasValue){var e=o.value;if(u(e,t))return i=e}return i=t}if(e=i,g(a,t))return e;var n=r(t);return void 0!==u&&u(e,n)?e:(a=t,i=n)}var a,i,c=!1,s=void 0===n?null:n;return[function(){return t(e())},null===s?void 0:function(){return t(s())}]}),[e,n,r,u]);var i=O(t,a[0],a[1]);return j((function(){o.hasValue=!0,o.value=i}),[i]),E(i),i}};
|
|
30
|
+
/**
|
|
31
|
+
* @license React
|
|
32
|
+
* use-sync-external-store-shim/with-selector.development.js
|
|
33
|
+
*
|
|
34
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
35
|
+
*
|
|
36
|
+
* This source code is licensed under the MIT license found in the
|
|
37
|
+
* LICENSE file in the root directory of this source tree.
|
|
38
|
+
*/
|
|
39
|
+
s((function(t,e){}));var k=s((function(t){t.exports=w})),C=e.useLayoutEffect;function P(t){var e=o.useRef();return e.current||(e.current={v:t()}),e.current.v}function I(t,e){var r=P((function(){return"function"==typeof t?t():t})),u=e.context,a=e.guards,o=e.actions,c=e.activities,s=e.services,f=e.delays;e.state;var l=function(t,e){var n={};for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&e.indexOf(r)<0&&(n[r]=t[r]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var u=0;for(r=Object.getOwnPropertySymbols(t);u<r.length;u++)e.indexOf(r[u])<0&&Object.prototype.propertyIsEnumerable.call(t,r[u])&&(n[r[u]]=t[r[u]])}return n}(e,["context","guards","actions","activities","services","delays","state"]),v=P((function(){var t={context:u,guards:a,actions:o,activities:c,services:s,delays:f},e=r.withConfig(t,(function(){return i(i({},r.context),u)}));return n.interpret(e,l)}));return C((function(){Object.assign(v.machine.options.actions,o),Object.assign(v.machine.options.guards,a),Object.assign(v.machine.options.activities,c),Object.assign(v.machine.options.services,s),Object.assign(v.machine.options.delays,f)}),[o,a,c,s,f]),v}function R(t){return t}function V(t){return"state"in t}function M(t){return"deferred"in t}function N(t){return"getSnapshot"in t?t.getSnapshot():V(t)?t.state:void 0}var _=function(t,e){return t===e},D=function(t){return"state"in(n=t)&&"machine"in n?0!==(e=t).status?e.state:e.machine.initialState:V(t)?t.state:void 0;var e,n};t.useActor=function(t,n){void 0===n&&(n=N);var r=e.useRef(t),u=e.useRef([]),a=e.useCallback((function(e){return t.subscribe(e).unsubscribe}),[t]),o=e.useCallback((function(){return n(t)}),[t,n]),i=S.useSyncExternalStore(a,o,o),c=P((function(){return function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];var n=t[0],a=r.current;M(a)&&a.deferred?u.current.push(n):a.send(n)}}));return C((function(){for(r.current=t;u.current.length>0;){var e=u.current.shift();t.send(e)}}),[t]),[i,c]},t.useInterpret=function(t){for(var r=[],u=1;u<arguments.length;u++)r[u-1]=arguments[u];var a=c(r,2),o=a[0],i=void 0===o?{}:o,s=a[1],f=I(t,i);return e.useEffect((function(){if(s){var t=f.subscribe(n.toObserver(s));return function(){t.unsubscribe()}}}),[s]),e.useEffect((function(){var t=i.state;return f.start(t?n.State.create(t):void 0),function(){f.stop(),f.status=n.InterpreterStatus.NotStarted}}),[]),f},t.useMachine=function(t){for(var r=[],u=1;u<arguments.length;u++)r[u-1]=arguments[u];var a=c(r,1),o=a[0],i=void 0===o?{}:o,s=I(t,i),f=e.useCallback((function(){return s.status===n.InterpreterStatus.NotStarted?i.state?n.State.create(i.state):s.machine.initialState:s.state}),[s]),l=e.useCallback((function(t,e){if(s.status===n.InterpreterStatus.NotStarted)return!0;var r=void 0===e.changed&&(Object.keys(e.children).length>0||"boolean"==typeof t.changed);return!(e.changed||r)}),[s]),v=e.useCallback((function(t){return s.subscribe(t).unsubscribe}),[s]),d=k.useSyncExternalStoreWithSelector(v,f,f,R,l);return e.useEffect((function(){var t=i.state;return s.start(t?n.State.create(t):void 0),function(){s.stop(),s.status=n.InterpreterStatus.NotStarted}}),[]),[d,s.send,s]},t.useSelector=function(t,n,r,u){void 0===r&&(r=_),void 0===u&&(u=D);var a=e.useCallback((function(e){return t.subscribe(e).unsubscribe}),[t]),o=e.useCallback((function(){return u(t)}),[t,u]);return k.useSyncExternalStoreWithSelector(a,o,o,n,r)},t.useSpawn=function(t){return P((function(){return n.spawnBehavior(t)}))},Object.defineProperty(t,"__esModule",{value:!0})}));
|
package/es/fsm.d.ts
CHANGED
|
@@ -1,20 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare function useMachine<
|
|
3
|
-
|
|
4
|
-
context: TContext;
|
|
5
|
-
}>(stateMachine: StateMachine.Machine<TContext, TEvent, TState>, options?: {
|
|
6
|
-
actions?: StateMachine.ActionMap<TContext, TEvent>;
|
|
7
|
-
}): [
|
|
8
|
-
StateMachine.State<TContext, TEvent, TState>,
|
|
9
|
-
StateMachine.Service<TContext, TEvent, TState>['send'],
|
|
10
|
-
StateMachine.Service<TContext, TEvent, TState>
|
|
11
|
-
];
|
|
12
|
-
export declare function useService<TContext extends object, TEvent extends EventObject = EventObject, TState extends Typestate<TContext> = {
|
|
13
|
-
value: any;
|
|
14
|
-
context: TContext;
|
|
15
|
-
}>(service: StateMachine.Service<TContext, TEvent, TState>): [
|
|
16
|
-
StateMachine.State<TContext, TEvent, TState>,
|
|
17
|
-
StateMachine.Service<TContext, TEvent, TState>['send'],
|
|
18
|
-
StateMachine.Service<TContext, TEvent, TState>
|
|
19
|
-
];
|
|
1
|
+
import { MachineImplementationsFrom, ServiceFrom, StateFrom, StateMachine } from '@xstate/fsm';
|
|
2
|
+
export declare function useMachine<TMachine extends StateMachine.AnyMachine>(stateMachine: TMachine, options?: MachineImplementationsFrom<TMachine>): [StateFrom<TMachine>, ServiceFrom<TMachine>['send'], ServiceFrom<TMachine>];
|
|
3
|
+
export declare function useService<TService extends StateMachine.AnyService>(service: TService): [StateFrom<TService>, TService['send'], TService];
|
|
20
4
|
//# sourceMappingURL=fsm.d.ts.map
|
package/es/fsm.js
CHANGED
|
@@ -14,10 +14,14 @@ var __read = (this && this.__read) || function (o, n) {
|
|
|
14
14
|
}
|
|
15
15
|
return ar;
|
|
16
16
|
};
|
|
17
|
-
import {
|
|
18
|
-
import {
|
|
19
|
-
import
|
|
17
|
+
import { createMachine, interpret, InterpreterStatus } from '@xstate/fsm';
|
|
18
|
+
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
19
|
+
import useIsomorphicLayoutEffect from 'use-isomorphic-layout-effect';
|
|
20
|
+
import { useSyncExternalStoreWithSelector } from 'use-sync-external-store/shim/with-selector';
|
|
20
21
|
import useConstant from './useConstant';
|
|
22
|
+
function identity(a) {
|
|
23
|
+
return a;
|
|
24
|
+
}
|
|
21
25
|
var getServiceState = function (service) {
|
|
22
26
|
var currentValue;
|
|
23
27
|
service
|
|
@@ -28,6 +32,7 @@ var getServiceState = function (service) {
|
|
|
28
32
|
return currentValue;
|
|
29
33
|
};
|
|
30
34
|
export function useMachine(stateMachine, options) {
|
|
35
|
+
var persistedStateRef = useRef();
|
|
31
36
|
if (process.env.NODE_ENV !== 'production') {
|
|
32
37
|
var _a = __read(useState(stateMachine), 1), initialMachine = _a[0];
|
|
33
38
|
if (stateMachine !== initialMachine) {
|
|
@@ -35,39 +40,43 @@ export function useMachine(stateMachine, options) {
|
|
|
35
40
|
'Please make sure that you pass the same Machine as argument each time.');
|
|
36
41
|
}
|
|
37
42
|
}
|
|
38
|
-
var
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
+
var _b = __read(useConstant(function () {
|
|
44
|
+
var queue = [];
|
|
45
|
+
var service = interpret(createMachine(stateMachine.config, options ? options : stateMachine._options));
|
|
46
|
+
var send = service.send;
|
|
47
|
+
service.send = function (event) {
|
|
48
|
+
if (service.status === InterpreterStatus.NotStarted) {
|
|
49
|
+
queue.push(event);
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
send(event);
|
|
53
|
+
persistedStateRef.current = service.state;
|
|
54
|
+
};
|
|
55
|
+
return [service, queue];
|
|
56
|
+
}), 2), service = _b[0], queue = _b[1];
|
|
57
|
+
useIsomorphicLayoutEffect(function () {
|
|
43
58
|
if (options) {
|
|
44
59
|
service._machine._options = options;
|
|
45
60
|
}
|
|
46
61
|
});
|
|
62
|
+
var useServiceResult = useService(service);
|
|
47
63
|
useEffect(function () {
|
|
48
|
-
service.
|
|
64
|
+
service.start(persistedStateRef.current);
|
|
65
|
+
queue.forEach(service.send);
|
|
66
|
+
persistedStateRef.current = service.state;
|
|
49
67
|
return function () {
|
|
50
68
|
service.stop();
|
|
51
69
|
};
|
|
52
70
|
}, []);
|
|
53
|
-
return
|
|
71
|
+
return useServiceResult;
|
|
54
72
|
}
|
|
73
|
+
var isEqual = function (_prevState, nextState) { return nextState.changed === false; };
|
|
55
74
|
export function useService(service) {
|
|
56
|
-
var
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
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
|
-
};
|
|
75
|
+
var getSnapshot = useCallback(function () { return getServiceState(service); }, [service]);
|
|
76
|
+
var subscribe = useCallback(function (handleStoreChange) {
|
|
77
|
+
var unsubscribe = service.subscribe(handleStoreChange).unsubscribe;
|
|
78
|
+
return unsubscribe;
|
|
70
79
|
}, [service]);
|
|
71
|
-
var
|
|
72
|
-
return [
|
|
80
|
+
var storeSnapshot = useSyncExternalStoreWithSelector(subscribe, getSnapshot, getSnapshot, identity, isEqual);
|
|
81
|
+
return [storeSnapshot, service.send, service];
|
|
73
82
|
}
|
package/es/index.d.ts
CHANGED
package/es/index.js
CHANGED
package/es/types.d.ts
CHANGED
|
@@ -1,18 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { EventObject, StateConfig } from 'xstate';
|
|
2
2
|
export declare type MaybeLazy<T> = T | (() => T);
|
|
3
3
|
export declare type NoInfer<T> = [T][T extends any ? 0 : any];
|
|
4
4
|
export declare type Prop<T, K> = K extends keyof T ? T[K] : never;
|
|
5
|
-
export declare enum ReactEffectType {
|
|
6
|
-
Effect = 1,
|
|
7
|
-
LayoutEffect = 2
|
|
8
|
-
}
|
|
9
|
-
export interface ReactActionFunction<TContext, TEvent extends EventObject> {
|
|
10
|
-
(context: TContext, event: TEvent, meta: ActionMeta<TContext, TEvent>): () => void;
|
|
11
|
-
__effect: ReactEffectType;
|
|
12
|
-
}
|
|
13
|
-
export interface ReactActionObject<TContext, TEvent extends EventObject> extends ActionObject<TContext, TEvent> {
|
|
14
|
-
exec: ReactActionFunction<TContext, TEvent>;
|
|
15
|
-
}
|
|
16
5
|
export interface UseMachineOptions<TContext, TEvent extends EventObject> {
|
|
17
6
|
/**
|
|
18
7
|
* If provided, will be merged with machine's `context`.
|
|
@@ -24,8 +13,4 @@ export interface UseMachineOptions<TContext, TEvent extends EventObject> {
|
|
|
24
13
|
*/
|
|
25
14
|
state?: StateConfig<TContext, TEvent>;
|
|
26
15
|
}
|
|
27
|
-
export declare type ActionStateTuple<TContext, TEvent extends EventObject> = [
|
|
28
|
-
ReactActionObject<TContext, TEvent>,
|
|
29
|
-
State<TContext, TEvent>
|
|
30
|
-
];
|
|
31
16
|
//# sourceMappingURL=types.d.ts.map
|
package/es/types.js
CHANGED
package/es/useActor.js
CHANGED
|
@@ -1,31 +1,13 @@
|
|
|
1
|
-
|
|
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';
|
|
1
|
+
import { useRef, useCallback } from 'react';
|
|
18
2
|
import useIsomorphicLayoutEffect from 'use-isomorphic-layout-effect';
|
|
19
3
|
import useConstant from './useConstant';
|
|
4
|
+
import { useSyncExternalStore } from 'use-sync-external-store/shim';
|
|
20
5
|
export function isActorWithState(actorRef) {
|
|
21
6
|
return 'state' in actorRef;
|
|
22
7
|
}
|
|
23
8
|
function isDeferredActor(actorRef) {
|
|
24
9
|
return 'deferred' in actorRef;
|
|
25
10
|
}
|
|
26
|
-
var noop = function () {
|
|
27
|
-
/* ... */
|
|
28
|
-
};
|
|
29
11
|
function defaultGetSnapshot(actorRef) {
|
|
30
12
|
return 'getSnapshot' in actorRef
|
|
31
13
|
? actorRef.getSnapshot()
|
|
@@ -37,7 +19,15 @@ export function useActor(actorRef, getSnapshot) {
|
|
|
37
19
|
if (getSnapshot === void 0) { getSnapshot = defaultGetSnapshot; }
|
|
38
20
|
var actorRefRef = useRef(actorRef);
|
|
39
21
|
var deferredEventsRef = useRef([]);
|
|
40
|
-
var
|
|
22
|
+
var subscribe = useCallback(function (handleStoreChange) {
|
|
23
|
+
var unsubscribe = actorRef.subscribe(handleStoreChange).unsubscribe;
|
|
24
|
+
return unsubscribe;
|
|
25
|
+
}, [actorRef]);
|
|
26
|
+
var boundGetSnapshot = useCallback(function () { return getSnapshot(actorRef); }, [
|
|
27
|
+
actorRef,
|
|
28
|
+
getSnapshot
|
|
29
|
+
]);
|
|
30
|
+
var storeSnapshot = useSyncExternalStore(subscribe, boundGetSnapshot, boundGetSnapshot);
|
|
41
31
|
var send = useConstant(function () { return function () {
|
|
42
32
|
var args = [];
|
|
43
33
|
for (var _i = 0; _i < arguments.length; _i++) {
|
|
@@ -60,20 +50,11 @@ export function useActor(actorRef, getSnapshot) {
|
|
|
60
50
|
}; });
|
|
61
51
|
useIsomorphicLayoutEffect(function () {
|
|
62
52
|
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
53
|
// Dequeue deferred events from the previous deferred actorRef
|
|
70
54
|
while (deferredEventsRef.current.length > 0) {
|
|
71
55
|
var deferredEvent = deferredEventsRef.current.shift();
|
|
72
56
|
actorRef.send(deferredEvent);
|
|
73
57
|
}
|
|
74
|
-
return function () {
|
|
75
|
-
subscription.unsubscribe();
|
|
76
|
-
};
|
|
77
58
|
}, [actorRef]);
|
|
78
|
-
return [
|
|
59
|
+
return [storeSnapshot, send];
|
|
79
60
|
}
|
package/es/useInterpret.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { AnyStateMachine, AreAllImplementationsAssumedToBeProvided, InternalMachineOptions, InterpreterFrom, InterpreterOptions, Observer, StateFrom } from 'xstate';
|
|
1
|
+
import { AnyInterpreter, AnyStateMachine, AreAllImplementationsAssumedToBeProvided, InternalMachineOptions, InterpreterFrom, InterpreterOptions, MachineOptions, Observer, StateFrom } from 'xstate';
|
|
2
2
|
import { MaybeLazy } from './types';
|
|
3
3
|
import { UseMachineOptions } from './useMachine';
|
|
4
|
+
export declare function useIdleInterpreter(getMachine: MaybeLazy<AnyStateMachine>, options: Partial<InterpreterOptions> & Partial<UseMachineOptions<unknown, never>> & Partial<MachineOptions<unknown, never>>): AnyInterpreter;
|
|
4
5
|
declare type RestParams<TMachine extends AnyStateMachine> = AreAllImplementationsAssumedToBeProvided<TMachine['__TResolvedTypesMeta']> extends false ? [
|
|
5
6
|
options: InterpreterOptions & UseMachineOptions<TMachine['__TContext'], TMachine['__TEvent']> & InternalMachineOptions<TMachine['__TContext'], TMachine['__TEvent'], TMachine['__TResolvedTypesMeta'], true>,
|
|
6
7
|
observerOrListener?: Observer<StateFrom<TMachine>> | ((value: StateFrom<TMachine>) => void)
|
package/es/useInterpret.js
CHANGED
|
@@ -36,44 +36,23 @@ var __read = (this && this.__read) || function (o, n) {
|
|
|
36
36
|
}
|
|
37
37
|
return ar;
|
|
38
38
|
};
|
|
39
|
-
import { useState } from 'react';
|
|
39
|
+
import { useEffect, useState } from 'react';
|
|
40
40
|
import useIsomorphicLayoutEffect from 'use-isomorphic-layout-effect';
|
|
41
|
-
import { interpret, State } from 'xstate';
|
|
41
|
+
import { interpret, InterpreterStatus, State, toObserver } from 'xstate';
|
|
42
42
|
import useConstant from './useConstant';
|
|
43
|
-
|
|
44
|
-
// copied from core/src/utils.ts
|
|
45
|
-
// it avoids a breaking change between this package and XState which is its peer dep
|
|
46
|
-
function toObserver(nextHandler, errorHandler, completionHandler) {
|
|
47
|
-
if (typeof nextHandler === 'object') {
|
|
48
|
-
return nextHandler;
|
|
49
|
-
}
|
|
50
|
-
var noop = function () { return void 0; };
|
|
51
|
-
return {
|
|
52
|
-
next: nextHandler,
|
|
53
|
-
error: errorHandler || noop,
|
|
54
|
-
complete: completionHandler || noop
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
|
-
export function useInterpret(getMachine) {
|
|
58
|
-
var _a = [];
|
|
59
|
-
for (var _i = 1; _i < arguments.length; _i++) {
|
|
60
|
-
_a[_i - 1] = arguments[_i];
|
|
61
|
-
}
|
|
62
|
-
var _b = __read(_a, 2), _c = _b[0], options = _c === void 0 ? {} : _c, observerOrListener = _b[1];
|
|
43
|
+
export function useIdleInterpreter(getMachine, options) {
|
|
63
44
|
var machine = useConstant(function () {
|
|
64
45
|
return typeof getMachine === 'function' ? getMachine() : getMachine;
|
|
65
46
|
});
|
|
66
47
|
if (process.env.NODE_ENV !== 'production' &&
|
|
67
48
|
typeof getMachine !== 'function') {
|
|
68
|
-
var
|
|
49
|
+
var _a = __read(useState(machine), 1), initialMachine = _a[0];
|
|
69
50
|
if (getMachine !== initialMachine) {
|
|
70
51
|
console.warn('Machine given to `useMachine` has changed between renders. This is not supported and might lead to unexpected results.\n' +
|
|
71
52
|
'Please make sure that you pass the same Machine as argument each time.');
|
|
72
53
|
}
|
|
73
54
|
}
|
|
74
|
-
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"]);
|
|
75
|
-
// it's not defined in `TypegenMachineOptions` so we can't just unpack this property here freely
|
|
76
|
-
var activities = options.activities;
|
|
55
|
+
var context = options.context, guards = options.guards, actions = options.actions, activities = options.activities, services = options.services, delays = options.delays, rehydratedState = options.state, interpreterOptions = __rest(options, ["context", "guards", "actions", "activities", "services", "delays", "state"]);
|
|
77
56
|
var service = useConstant(function () {
|
|
78
57
|
var machineConfig = {
|
|
79
58
|
context: context,
|
|
@@ -84,23 +63,8 @@ export function useInterpret(getMachine) {
|
|
|
84
63
|
delays: delays
|
|
85
64
|
};
|
|
86
65
|
var machineWithConfig = machine.withConfig(machineConfig, function () { return (__assign(__assign({}, machine.context), context)); });
|
|
87
|
-
return interpret(machineWithConfig,
|
|
66
|
+
return interpret(machineWithConfig, interpreterOptions);
|
|
88
67
|
});
|
|
89
|
-
useIsomorphicLayoutEffect(function () {
|
|
90
|
-
var sub;
|
|
91
|
-
if (observerOrListener) {
|
|
92
|
-
sub = service.subscribe(toObserver(observerOrListener));
|
|
93
|
-
}
|
|
94
|
-
return function () {
|
|
95
|
-
sub === null || sub === void 0 ? void 0 : sub.unsubscribe();
|
|
96
|
-
};
|
|
97
|
-
}, [observerOrListener]);
|
|
98
|
-
useIsomorphicLayoutEffect(function () {
|
|
99
|
-
service.start(rehydratedState ? State.create(rehydratedState) : undefined);
|
|
100
|
-
return function () {
|
|
101
|
-
service.stop();
|
|
102
|
-
};
|
|
103
|
-
}, []);
|
|
104
68
|
// Make sure options are kept updated when they change.
|
|
105
69
|
// This mutation assignment is safe because the service instance is only used
|
|
106
70
|
// in one place -- this hook's caller.
|
|
@@ -111,6 +75,31 @@ export function useInterpret(getMachine) {
|
|
|
111
75
|
Object.assign(service.machine.options.services, services);
|
|
112
76
|
Object.assign(service.machine.options.delays, delays);
|
|
113
77
|
}, [actions, guards, activities, services, delays]);
|
|
114
|
-
|
|
78
|
+
return service;
|
|
79
|
+
}
|
|
80
|
+
export function useInterpret(getMachine) {
|
|
81
|
+
var _a = [];
|
|
82
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
|
83
|
+
_a[_i - 1] = arguments[_i];
|
|
84
|
+
}
|
|
85
|
+
var _b = __read(_a, 2), _c = _b[0], options = _c === void 0 ? {} : _c, observerOrListener = _b[1];
|
|
86
|
+
var service = useIdleInterpreter(getMachine, options);
|
|
87
|
+
useEffect(function () {
|
|
88
|
+
if (!observerOrListener) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
var sub = service.subscribe(toObserver(observerOrListener));
|
|
92
|
+
return function () {
|
|
93
|
+
sub.unsubscribe();
|
|
94
|
+
};
|
|
95
|
+
}, [observerOrListener]);
|
|
96
|
+
useEffect(function () {
|
|
97
|
+
var rehydratedState = options.state;
|
|
98
|
+
service.start(rehydratedState ? State.create(rehydratedState) : undefined);
|
|
99
|
+
return function () {
|
|
100
|
+
service.stop();
|
|
101
|
+
service.status = InterpreterStatus.NotStarted;
|
|
102
|
+
};
|
|
103
|
+
}, []);
|
|
115
104
|
return service;
|
|
116
105
|
}
|
package/es/useMachine.d.ts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { MaybeLazy, Prop
|
|
3
|
-
export declare function asEffect<TContext, TEvent extends EventObject>(exec: ActionFunction<TContext, TEvent>): ReactActionFunction<TContext, TEvent>;
|
|
4
|
-
export declare function asLayoutEffect<TContext, TEvent extends EventObject>(exec: ActionFunction<TContext, TEvent>): ReactActionFunction<TContext, TEvent>;
|
|
1
|
+
import { AnyStateMachine, AreAllImplementationsAssumedToBeProvided, EventObject, InternalMachineOptions, InterpreterFrom, InterpreterOptions, StateConfig, StateFrom } from 'xstate';
|
|
2
|
+
import { MaybeLazy, Prop } from './types';
|
|
5
3
|
export interface UseMachineOptions<TContext, TEvent extends EventObject> {
|
|
6
4
|
/**
|
|
7
5
|
* If provided, will be merged with machine's `context`.
|