@xstate/react 1.6.2 → 1.6.3
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 +6 -0
- package/dist/xstate-react.umd.min.js +4 -4
- package/es/useActor.js +1 -1
- package/es/useMachine.js +10 -6
- package/es/useReactEffectActions.js +10 -6
- package/es/useSelector.js +8 -3
- package/lib/fsm.js +8 -8
- package/lib/useActor.js +6 -6
- package/lib/useInterpret.js +8 -8
- package/lib/useMachine.js +13 -9
- package/lib/useReactEffectActions.js +16 -12
- package/lib/useSelector.js +14 -9
- package/lib/useService.js +1 -1
- package/lib/useSpawn.js +2 -2
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 1.6.3
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#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`.
|
|
8
|
+
|
|
3
9
|
## 1.6.2
|
|
4
10
|
|
|
5
11
|
### Patch Changes
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
!function(e
|
|
1
|
+
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("react"),require("xstate"),require("xstate/lib/behaviors")):"function"==typeof define&&define.amd?define(["exports","react","xstate","xstate/lib/behaviors"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).XStateReact={},t.React,t.XState,t.behaviors)}(this,(function(t,e,n,r){"use strict";function u(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var i,o=u(e),c=function(){return(c=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)};
|
|
2
2
|
/*! *****************************************************************************
|
|
3
3
|
Copyright (c) Microsoft Corporation.
|
|
4
4
|
|
|
@@ -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
|
|
15
|
+
***************************************************************************** */function a(t,e){var n="function"==typeof Symbol&&t[Symbol.iterator];if(!n)return t;var r,u,i=n.call(t),o=[];try{for(;(void 0===e||e-- >0)&&!(r=i.next()).done;)o.push(r.value)}catch(t){u={error:t}}finally{try{r&&!r.done&&(n=i.return)&&n.call(i)}finally{if(u)throw u.error}}return o}function f(t,e,n){if(n||2===arguments.length)for(var r,u=0,i=e.length;u<i;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"}(i||(i={}));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),i=u[0],o=u[1];try{for(var c=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=c.next();!f.done;f=c.next()){var s=f.value;e(s)?i.push(s):o.push(s)}}catch(t){n={error:t}}finally{try{f&&!f.done&&(r=c.return)&&r.call(c)}finally{if(n)throw n.error}}return[i,o]}function p(t,e){(0,t.exec)(e.context,e._event.data,{action:t,state:e,_event:e._event})()}function b(t,r,u){void 0===r&&(r={});var o=l((function(){return"function"==typeof t?t():t})),b=r.context,d=r.guards,y=r.actions,h=r.activities,g=r.services,O=r.delays,j=r.state,m=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}(r,["context","guards","actions","activities","services","delays","state"]),x=l((function(){var t={context:b,guards:d,actions:y,activities:h,services:g,delays:O},e=o.withConfig(t,(function(){return c(c({},o.context),b)}));return n.interpret(e,c({deferEvents:!0},m))}));return s((function(){var t;return u&&(t=x.subscribe(function(t,e,n){if("object"==typeof t)return t;var r=function(){};return{next:t,error:e||r,complete:n||r}}(u))),function(){null==t||t.unsubscribe()}}),[u]),s((function(){return x.start(j?n.State.create(j):void 0),function(){x.stop()}}),[]),s((function(){Object.assign(x.machine.options.actions,y),Object.assign(x.machine.options.guards,d),Object.assign(x.machine.options.activities,h),Object.assign(x.machine.options.services,g),Object.assign(x.machine.options.delays,O)}),[y,d,h,g,O]),function(t){var n=e.useRef([]),r=e.useRef([]);s((function(){var e=t.subscribe((function(t){var e,u;if(t.actions.length){var o=a(v(t.actions.filter((function(t){return"function"==typeof t.exec&&"__effect"in t.exec})),(function(t){return t.exec.__effect===i.Effect})),2),c=o[0],s=o[1];(e=n.current).push.apply(e,f([],a(c.map((function(e){return[e,t]}))),!1)),(u=r.current).push.apply(u,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])}}))}(x),x}function d(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 y(t){return"state"in t}function h(t){return"deferred"in t}var g=function(){};function O(t){return"getSnapshot"in t?t.getSnapshot():y(t)?t.state:void 0}function j(t,n){void 0===n&&(n=O);var r=e.useRef(t),u=e.useRef([]),i=a(e.useState((function(){return n(t)})),2),o=i[0],c=i[1],f=l((function(){return function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];var n=t[0],i=r.current;h(i)&&i.deferred?u.current.push(n):i.send(n)}}));return s((function(){r.current=t,c(n(t));for(var e=t.subscribe({next:function(t){return c(t)},error:g,complete:g});u.current.length>0;){var i=u.current.shift();t.send(i)}return function(){e.unsubscribe()}}),[t]),[o,f]}function m(t){var e={exports:{}};return t(e,e.exports),e.exports
|
|
16
16
|
/*
|
|
17
17
|
object-assign
|
|
18
18
|
(c) Sindre Sorhus
|
|
19
19
|
@license MIT
|
|
20
|
-
*/}var x=Object.getOwnPropertySymbols,S=Object.prototype.hasOwnProperty,w=Object.prototype.propertyIsEnumerable;function E(
|
|
20
|
+
*/}var x=Object.getOwnPropertySymbols,S=Object.prototype.hasOwnProperty,w=Object.prototype.propertyIsEnumerable;function E(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=E(t),i=1;i<arguments.length;i++){for(var o in n=Object(arguments[i]))S.call(n,o)&&(u[o]=n[o]);if(x){r=x(n);for(var c=0;c<r.length;c++)w.call(n,r[c])&&(u[r[c]]=n[r[c]])}}return u},C={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,i=n(t);return t(),function(){r=!0,i()}}),[e,n]),r}},P=(m((function(t,e){})),m((function(t){t.exports=C})));
|
|
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 V=function(e
|
|
28
|
+
*/var V=function(t,e){return t===e},R=function(t){return"state"in(n=t)&&"machine"in n?0!==("status"in(e=t)?e.status:e._status)?e.state:e.machine.initialState:y(t)?t.state:void 0;var e,n};t.asEffect=function(t){return d(t,i.Effect)},t.asLayoutEffect=function(t){return d(t,i.LayoutEffect)},t.useActor=j,t.useInterpret=b,t.useMachine=function(t,r){void 0===r&&(r={});var u=e.useCallback((function(t){var e=void 0===t.changed&&Object.keys(t.children).length;(t.changed||e)&&f(t)}),[]),i=b(t,r,u),o=a(e.useState((function(){var t=i.machine.initialState;return r.state?n.State.create(r.state):t})),2),c=o[0],f=o[1];return[c,i.send,i]},t.useSelector=function(t,n,r,u){void 0===r&&(r=V),void 0===u&&(u=R);var i=e.useRef(n),o=e.useMemo((function(){var e,o=u(t),c=n(o);return{getSnapshot:function(){return o},getCurrentValue:function(){return c},setCurrentValue:function(t){c=t,null==e||e()},subscribe:function(n){e=n;var u=t.subscribe((function(t){o=t;var e=i.current(t);r(c,e)||(c=e,n())}));return function(){u.unsubscribe()}}}}),[t]),c=P.useSubscription(o),a=!1;if(i.current!==n){var f=n(o.getSnapshot());r(c,f)||(a=!0,c=f)}return s((function(){i.current=n,a&&o.setCurrentValue(c)})),c},t.useService=function(t){return[a(j(t),1)[0],t.send]},t.useSpawn=function(t){return l((function(){return r.spawnBehavior(t)}))},Object.defineProperty(t,"__esModule",{value:!0})}));
|
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/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 () {
|
package/es/useSelector.js
CHANGED
|
@@ -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/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/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.js
CHANGED
|
@@ -59,19 +59,19 @@ function toObserver(nextHandler, errorHandler, completionHandler) {
|
|
|
59
59
|
}
|
|
60
60
|
function useInterpret(getMachine, options, observerOrListener) {
|
|
61
61
|
if (options === void 0) { options = {}; }
|
|
62
|
-
var machine = useConstant_1.default(function () {
|
|
62
|
+
var machine = (0, useConstant_1.default)(function () {
|
|
63
63
|
return typeof getMachine === 'function' ? getMachine() : getMachine;
|
|
64
64
|
});
|
|
65
65
|
if (process.env.NODE_ENV !== 'production' &&
|
|
66
66
|
typeof getMachine !== 'function') {
|
|
67
|
-
var _a = __read(react_1.useState(machine), 1), initialMachine = _a[0];
|
|
67
|
+
var _a = __read((0, react_1.useState)(machine), 1), initialMachine = _a[0];
|
|
68
68
|
if (getMachine !== initialMachine) {
|
|
69
69
|
console.warn('Machine given to `useMachine` has changed between renders. This is not supported and might lead to unexpected results.\n' +
|
|
70
70
|
'Please make sure that you pass the same Machine as argument each time.');
|
|
71
71
|
}
|
|
72
72
|
}
|
|
73
73
|
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"]);
|
|
74
|
-
var service = useConstant_1.default(function () {
|
|
74
|
+
var service = (0, useConstant_1.default)(function () {
|
|
75
75
|
var machineConfig = {
|
|
76
76
|
context: context,
|
|
77
77
|
guards: guards,
|
|
@@ -81,9 +81,9 @@ function useInterpret(getMachine, options, observerOrListener) {
|
|
|
81
81
|
delays: delays
|
|
82
82
|
};
|
|
83
83
|
var machineWithConfig = machine.withConfig(machineConfig, function () { return (__assign(__assign({}, machine.context), context)); });
|
|
84
|
-
return xstate_1.interpret(machineWithConfig, __assign({ deferEvents: true }, interpreterOptions));
|
|
84
|
+
return (0, xstate_1.interpret)(machineWithConfig, __assign({ deferEvents: true }, interpreterOptions));
|
|
85
85
|
});
|
|
86
|
-
use_isomorphic_layout_effect_1.default(function () {
|
|
86
|
+
(0, use_isomorphic_layout_effect_1.default)(function () {
|
|
87
87
|
var sub;
|
|
88
88
|
if (observerOrListener) {
|
|
89
89
|
sub = service.subscribe(toObserver(observerOrListener));
|
|
@@ -92,7 +92,7 @@ function useInterpret(getMachine, options, observerOrListener) {
|
|
|
92
92
|
sub === null || sub === void 0 ? void 0 : sub.unsubscribe();
|
|
93
93
|
};
|
|
94
94
|
}, [observerOrListener]);
|
|
95
|
-
use_isomorphic_layout_effect_1.default(function () {
|
|
95
|
+
(0, use_isomorphic_layout_effect_1.default)(function () {
|
|
96
96
|
service.start(rehydratedState ? xstate_1.State.create(rehydratedState) : undefined);
|
|
97
97
|
return function () {
|
|
98
98
|
service.stop();
|
|
@@ -101,14 +101,14 @@ function useInterpret(getMachine, options, observerOrListener) {
|
|
|
101
101
|
// Make sure options are kept updated when they change.
|
|
102
102
|
// This mutation assignment is safe because the service instance is only used
|
|
103
103
|
// in one place -- this hook's caller.
|
|
104
|
-
use_isomorphic_layout_effect_1.default(function () {
|
|
104
|
+
(0, use_isomorphic_layout_effect_1.default)(function () {
|
|
105
105
|
Object.assign(service.machine.options.actions, actions);
|
|
106
106
|
Object.assign(service.machine.options.guards, guards);
|
|
107
107
|
Object.assign(service.machine.options.activities, activities);
|
|
108
108
|
Object.assign(service.machine.options.services, services);
|
|
109
109
|
Object.assign(service.machine.options.delays, delays);
|
|
110
110
|
}, [actions, guards, activities, services, delays]);
|
|
111
|
-
useReactEffectActions_1.useReactEffectActions(service);
|
|
111
|
+
(0, useReactEffectActions_1.useReactEffectActions)(service);
|
|
112
112
|
return service;
|
|
113
113
|
}
|
|
114
114
|
exports.useInterpret = useInterpret;
|
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;
|
|
@@ -53,7 +57,7 @@ function asLayoutEffect(exec) {
|
|
|
53
57
|
exports.asLayoutEffect = asLayoutEffect;
|
|
54
58
|
function useMachine(getMachine, options) {
|
|
55
59
|
if (options === void 0) { options = {}; }
|
|
56
|
-
var listener = react_1.useCallback(function (nextState) {
|
|
60
|
+
var listener = (0, react_1.useCallback)(function (nextState) {
|
|
57
61
|
// Only change the current state if:
|
|
58
62
|
// - the incoming state is the "live" initial state (since it might have new actors)
|
|
59
63
|
// - OR the incoming state actually changed.
|
|
@@ -65,8 +69,8 @@ function useMachine(getMachine, options) {
|
|
|
65
69
|
setState(nextState);
|
|
66
70
|
}
|
|
67
71
|
}, []);
|
|
68
|
-
var service = useInterpret_1.useInterpret(getMachine, options, listener);
|
|
69
|
-
var _a = __read(react_1.useState(function () {
|
|
72
|
+
var service = (0, useInterpret_1.useInterpret)(getMachine, options, listener);
|
|
73
|
+
var _a = __read((0, react_1.useState)(function () {
|
|
70
74
|
var initialState = service.machine.initialState;
|
|
71
75
|
return (options.state
|
|
72
76
|
? 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
72
|
}); // https://github.com/davidkpiano/xstate/pull/1202#discussion_r429677773
|
|
69
|
-
react_1.useEffect(function () {
|
|
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
|
@@ -12,16 +12,16 @@ function isService(actor) {
|
|
|
12
12
|
var defaultCompare = function (a, b) { return a === b; };
|
|
13
13
|
var defaultGetSnapshot = function (a) {
|
|
14
14
|
return isService(a)
|
|
15
|
-
? useService_1.getServiceSnapshot(a)
|
|
16
|
-
: useActor_1.isActorWithState(a)
|
|
15
|
+
? (0, useService_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/useService.js
CHANGED
|
@@ -35,7 +35,7 @@ function useService(service) {
|
|
|
35
35
|
if (process.env.NODE_ENV !== 'production' && !('machine' in service)) {
|
|
36
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
37
|
}
|
|
38
|
-
var _a = __read(useActor_1.useActor(service), 1), state = _a[0];
|
|
38
|
+
var _a = __read((0, useActor_1.useActor)(service), 1), state = _a[0];
|
|
39
39
|
return [state, service.send];
|
|
40
40
|
}
|
|
41
41
|
exports.useService = useService;
|
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xstate/react",
|
|
3
|
-
"version": "1.6.
|
|
3
|
+
"version": "1.6.3",
|
|
4
4
|
"description": "XState tools for React",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"state",
|
|
@@ -80,7 +80,7 @@
|
|
|
80
80
|
"rollup-plugin-terser": "^5.1.2",
|
|
81
81
|
"rollup-plugin-typescript2": "^0.30.0",
|
|
82
82
|
"ts-jest": "^26.5.6",
|
|
83
|
-
"typescript": "^4.
|
|
83
|
+
"typescript": "^4.5.2",
|
|
84
84
|
"xstate": "*"
|
|
85
85
|
}
|
|
86
86
|
}
|