@xstate/react 3.0.0 → 3.0.2

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.
@@ -1,4 +1,4 @@
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
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 o=r(e),a=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,o=n.call(t),a=[];try{for(;(void 0===e||e-- >0)&&!(r=o.next()).done;)a.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 a}function s(t){var e={exports:{}};return t(e,e.exports),e.exports
2
2
  /**
3
3
  * @license React
4
4
  * use-sync-external-store-shim.production.min.js
@@ -7,7 +7,7 @@
7
7
  *
8
8
  * This source code is licensed under the MIT license found in the
9
9
  * LICENSE file in the root directory of this source tree.
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};
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=o.default.useState,v=o.default.useEffect,d=o.default.useLayoutEffect,p=o.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,o=r[1];return d((function(){u.value=n,u.getSnapshot=e,b(u)&&o({inst:u})}),[t,n,e]),v((function(){return b(u)&&o({inst:u}),t((function(){b(u)&&o({inst:u})}))}),[t]),p(n),n},h={useSyncExternalStore:void 0!==o.default.useSyncExternalStore?o.default.useSyncExternalStore:y};
11
11
  /**
12
12
  * @license React
13
13
  * use-sync-external-store-shim.development.js
@@ -26,7 +26,7 @@ s((function(t,e){}));var S=s((function(t){t.exports=h}));
26
26
  *
27
27
  * This source code is licensed under the MIT license found in the
28
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}};
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,j=o.default.useRef,x=o.default.useEffect,m=o.default.useMemo,E=o.default.useDebugValue,w={useSyncExternalStoreWithSelector:function(t,e,n,r,u){var o=j(null);if(null===o.current){var a={hasValue:!1,value:null};o.current=a}else a=o.current;o=m((function(){function t(t){if(!c){if(c=!0,o=t,t=r(t),void 0!==u&&a.hasValue){var e=a.value;if(u(e,t))return i=e}return i=t}if(e=i,g(o,t))return e;var n=r(t);return void 0!==u&&u(e,n)?e:(o=t,i=n)}var o,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,o[0],o[1]);return x((function(){a.hasValue=!0,a.value=i}),[i]),E(i),i}};
30
30
  /**
31
31
  * @license React
32
32
  * use-sync-external-store-shim/with-selector.development.js
@@ -36,4 +36,4 @@ s((function(t,e){}));var S=s((function(t){t.exports=h}));
36
36
  * This source code is licensed under the MIT license found in the
37
37
  * LICENSE file in the root directory of this source tree.
38
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})}));
39
+ s((function(t,e){}));var k=s((function(t){t.exports=w})),C=e.useLayoutEffect;function P(t){var e=a.useRef();return e.current||(e.current={v:t()}),e.current.v}function R(t,e){var r=P((function(){return"function"==typeof t?t():t})),u=e.context,o=e.guards,a=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:o,actions:a,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,a),Object.assign(v.machine.options.guards,o),Object.assign(v.machine.options.activities,c),Object.assign(v.machine.options.services,s),Object.assign(v.machine.options.delays,f)}),[a,o,c,s,f]),v}function I(t){return t}function V(t){return 0!==t.status?t.getSnapshot():t.machine.initialState}function M(t,e){return t===e?0!==t||0!==e||1/t==1/e:t!=t&&e!=e}function N(t){return"state"in t&&"machine"in t}function _(t){return"state"in t}function q(t){return"deferred"in t}function D(t){return"getSnapshot"in t?N(t)?V(t):t.getSnapshot():_(t)?t.state:void 0}var W=function(t,e){return t===e};t.shallowEqual=function(t,e){if(M(t,e))return!0;if("object"!=typeof t||null===t||"object"!=typeof e||null===e)return!1;var n=Object.keys(t),r=Object.keys(e);if(n.length!==r.length)return!1;for(var u=0;u<n.length;u++)if(!Object.prototype.hasOwnProperty.call(e,n[u])||!M(t[n[u]],e[n[u]]))return!1;return!0},t.useActor=function(t,n){void 0===n&&(n=D);var r=e.useRef(t),u=e.useRef([]),o=e.useCallback((function(e){return t.subscribe(e).unsubscribe}),[t]),a=e.useCallback((function(){return n(t)}),[t,n]),i=S.useSyncExternalStore(o,a,a),c=P((function(){return function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];var n=t[0],o=r.current;q(o)&&o.deferred?u.current.push(n):o.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 o=c(r,2),a=o[0],i=void 0===a?{}:a,s=o[1],f=R(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 o=c(r,1),a=o[0],i=void 0===a?{}:a,s=R(t,i),f=e.useCallback((function(){return s.status===n.InterpreterStatus.NotStarted?i.state?n.State.create(i.state):s.machine.initialState:s.getSnapshot()}),[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,I,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=W);var o=e.useRef(null),a=e.useCallback((function(e){return t.subscribe(e).unsubscribe}),[t]),i=e.useCallback((function(){return u?u(t):function(t,e){if(N(t)){if(0===t.status&&e.current)return e.current;var n=V(t);return e.current=0===t.status?n:null,n}return _(t)?t.state:void 0}(t,o)}),[t,u]);return k.useSyncExternalStoreWithSelector(a,i,i,n,r)},t.useSpawn=function(t){return P((function(){return n.spawnBehavior(t)}))},Object.defineProperty(t,"__esModule",{value:!0})}));
package/es/index.d.ts CHANGED
@@ -3,4 +3,5 @@ export { useActor } from './useActor';
3
3
  export { useInterpret } from './useInterpret';
4
4
  export { useSelector } from './useSelector';
5
5
  export { useSpawn } from './useSpawn';
6
+ export { shallowEqual } from './utils';
6
7
  //# sourceMappingURL=index.d.ts.map
package/es/index.js CHANGED
@@ -3,3 +3,4 @@ export { useActor } from './useActor';
3
3
  export { useInterpret } from './useInterpret';
4
4
  export { useSelector } from './useSelector';
5
5
  export { useSpawn } from './useSpawn';
6
+ export { shallowEqual } from './utils';
package/es/useActor.js CHANGED
@@ -2,6 +2,7 @@ import { useRef, useCallback } from 'react';
2
2
  import useIsomorphicLayoutEffect from 'use-isomorphic-layout-effect';
3
3
  import useConstant from './useConstant';
4
4
  import { useSyncExternalStore } from 'use-sync-external-store/shim';
5
+ import { getServiceSnapshot, isService } from './utils';
5
6
  export function isActorWithState(actorRef) {
6
7
  return 'state' in actorRef;
7
8
  }
@@ -10,7 +11,9 @@ function isDeferredActor(actorRef) {
10
11
  }
11
12
  function defaultGetSnapshot(actorRef) {
12
13
  return 'getSnapshot' in actorRef
13
- ? actorRef.getSnapshot()
14
+ ? isService(actorRef)
15
+ ? getServiceSnapshot(actorRef)
16
+ : actorRef.getSnapshot()
14
17
  : isActorWithState(actorRef)
15
18
  ? actorRef.state
16
19
  : undefined;
package/es/useMachine.js CHANGED
@@ -36,7 +36,7 @@ export function useMachine(getMachine) {
36
36
  ? State.create(options.state)
37
37
  : service.machine.initialState);
38
38
  }
39
- return service.state;
39
+ return service.getSnapshot();
40
40
  }, [service]);
41
41
  var isEqual = useCallback(function (prevState, nextState) {
42
42
  if (service.status === InterpreterStatus.NotStarted) {
package/es/useSelector.js CHANGED
@@ -1,29 +1,33 @@
1
- import { useCallback } from 'react';
1
+ import { useCallback, useRef } from 'react';
2
2
  import { useSyncExternalStoreWithSelector } from 'use-sync-external-store/shim/with-selector';
3
3
  import { isActorWithState } from './useActor';
4
- import { getServiceSnapshot } from './utils';
5
- function isService(actor) {
6
- return 'state' in actor && 'machine' in actor;
7
- }
4
+ import { getServiceSnapshot, isService } from './utils';
8
5
  var defaultCompare = function (a, b) { return a === b; };
9
- var defaultGetSnapshot = function (a) {
10
- return isService(a)
11
- ? getServiceSnapshot(a)
12
- : isActorWithState(a)
13
- ? a.state
14
- : undefined;
6
+ var defaultGetSnapshot = function (a, initialStateCacheRef) {
7
+ if (isService(a)) {
8
+ // A status of 0 = interpreter not started
9
+ if (a.status === 0 && initialStateCacheRef.current) {
10
+ return initialStateCacheRef.current;
11
+ }
12
+ var snapshot = getServiceSnapshot(a);
13
+ initialStateCacheRef.current = a.status === 0 ? snapshot : null;
14
+ return snapshot;
15
+ }
16
+ return isActorWithState(a) ? a.state : undefined;
15
17
  };
16
18
  export function useSelector(actor, selector, compare, getSnapshot) {
17
19
  if (compare === void 0) { compare = defaultCompare; }
18
- if (getSnapshot === void 0) { getSnapshot = defaultGetSnapshot; }
20
+ var initialStateCacheRef = useRef(null);
19
21
  var subscribe = useCallback(function (handleStoreChange) {
20
22
  var unsubscribe = actor.subscribe(handleStoreChange).unsubscribe;
21
23
  return unsubscribe;
22
24
  }, [actor]);
23
- var boundGetSnapshot = useCallback(function () { return getSnapshot(actor); }, [
24
- actor,
25
- getSnapshot
26
- ]);
25
+ var boundGetSnapshot = useCallback(function () {
26
+ if (getSnapshot) {
27
+ return getSnapshot(actor);
28
+ }
29
+ return defaultGetSnapshot(actor, initialStateCacheRef);
30
+ }, [actor, getSnapshot]);
27
31
  var selectedSnapshot = useSyncExternalStoreWithSelector(subscribe, boundGetSnapshot, boundGetSnapshot, selector, compare);
28
32
  return selectedSnapshot;
29
33
  }
package/es/utils.d.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  import { Interpreter } from 'xstate';
2
2
  export declare function partition<T, A extends T, B extends T>(items: T[], predicate: (item: T) => item is A): [A[], B[]];
3
3
  export declare function getServiceSnapshot<TService extends Interpreter<any, any, any, any>>(service: TService): TService['state'];
4
+ export declare function shallowEqual(objA: any, objB: any): boolean;
5
+ export declare function isService(actor: any): actor is Interpreter<any, any, any, any>;
4
6
  //# sourceMappingURL=utils.d.ts.map
package/es/utils.js CHANGED
@@ -49,5 +49,40 @@ export function partition(items, predicate) {
49
49
  return [truthy, falsy];
50
50
  }
51
51
  export function getServiceSnapshot(service) {
52
- return service.status !== 0 ? service.state : service.machine.initialState;
52
+ return service.status !== 0
53
+ ? service.getSnapshot()
54
+ : service.machine.initialState;
55
+ }
56
+ // From https://github.com/reduxjs/react-redux/blob/master/src/utils/shallowEqual.ts
57
+ function is(x, y) {
58
+ if (x === y) {
59
+ return x !== 0 || y !== 0 || 1 / x === 1 / y;
60
+ }
61
+ else {
62
+ return x !== x && y !== y;
63
+ }
64
+ }
65
+ export function shallowEqual(objA, objB) {
66
+ if (is(objA, objB))
67
+ return true;
68
+ if (typeof objA !== 'object' ||
69
+ objA === null ||
70
+ typeof objB !== 'object' ||
71
+ objB === null) {
72
+ return false;
73
+ }
74
+ var keysA = Object.keys(objA);
75
+ var keysB = Object.keys(objB);
76
+ if (keysA.length !== keysB.length)
77
+ return false;
78
+ for (var i = 0; i < keysA.length; i++) {
79
+ if (!Object.prototype.hasOwnProperty.call(objB, keysA[i]) ||
80
+ !is(objA[keysA[i]], objB[keysA[i]])) {
81
+ return false;
82
+ }
83
+ }
84
+ return true;
85
+ }
86
+ export function isService(actor) {
87
+ return 'state' in actor && 'machine' in actor;
53
88
  }
package/lib/index.d.ts CHANGED
@@ -3,4 +3,5 @@ export { useActor } from './useActor';
3
3
  export { useInterpret } from './useInterpret';
4
4
  export { useSelector } from './useSelector';
5
5
  export { useSpawn } from './useSpawn';
6
+ export { shallowEqual } from './utils';
6
7
  //# sourceMappingURL=index.d.ts.map
package/lib/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.useSpawn = exports.useSelector = exports.useInterpret = exports.useActor = exports.useMachine = void 0;
3
+ exports.shallowEqual = exports.useSpawn = exports.useSelector = exports.useInterpret = exports.useActor = exports.useMachine = void 0;
4
4
  var useMachine_1 = require("./useMachine");
5
5
  Object.defineProperty(exports, "useMachine", { enumerable: true, get: function () { return useMachine_1.useMachine; } });
6
6
  var useActor_1 = require("./useActor");
@@ -11,3 +11,5 @@ var useSelector_1 = require("./useSelector");
11
11
  Object.defineProperty(exports, "useSelector", { enumerable: true, get: function () { return useSelector_1.useSelector; } });
12
12
  var useSpawn_1 = require("./useSpawn");
13
13
  Object.defineProperty(exports, "useSpawn", { enumerable: true, get: function () { return useSpawn_1.useSpawn; } });
14
+ var utils_1 = require("./utils");
15
+ Object.defineProperty(exports, "shallowEqual", { enumerable: true, get: function () { return utils_1.shallowEqual; } });
package/lib/useActor.js CHANGED
@@ -5,6 +5,7 @@ var react_1 = require("react");
5
5
  var use_isomorphic_layout_effect_1 = require("use-isomorphic-layout-effect");
6
6
  var useConstant_1 = require("./useConstant");
7
7
  var shim_1 = require("use-sync-external-store/shim");
8
+ var utils_1 = require("./utils");
8
9
  function isActorWithState(actorRef) {
9
10
  return 'state' in actorRef;
10
11
  }
@@ -14,7 +15,9 @@ function isDeferredActor(actorRef) {
14
15
  }
15
16
  function defaultGetSnapshot(actorRef) {
16
17
  return 'getSnapshot' in actorRef
17
- ? actorRef.getSnapshot()
18
+ ? (0, utils_1.isService)(actorRef)
19
+ ? (0, utils_1.getServiceSnapshot)(actorRef)
20
+ : actorRef.getSnapshot()
18
21
  : isActorWithState(actorRef)
19
22
  ? actorRef.state
20
23
  : undefined;
package/lib/useMachine.js CHANGED
@@ -39,7 +39,7 @@ function useMachine(getMachine) {
39
39
  ? xstate_1.State.create(options.state)
40
40
  : service.machine.initialState);
41
41
  }
42
- return service.state;
42
+ return service.getSnapshot();
43
43
  }, [service]);
44
44
  var isEqual = (0, react_1.useCallback)(function (prevState, nextState) {
45
45
  if (service.status === xstate_1.InterpreterStatus.NotStarted) {
@@ -5,28 +5,32 @@ var react_1 = require("react");
5
5
  var with_selector_1 = require("use-sync-external-store/shim/with-selector");
6
6
  var useActor_1 = require("./useActor");
7
7
  var utils_1 = require("./utils");
8
- function isService(actor) {
9
- return 'state' in actor && 'machine' in actor;
10
- }
11
8
  var defaultCompare = function (a, b) { return a === b; };
12
- var defaultGetSnapshot = function (a) {
13
- return isService(a)
14
- ? (0, utils_1.getServiceSnapshot)(a)
15
- : (0, useActor_1.isActorWithState)(a)
16
- ? a.state
17
- : undefined;
9
+ var defaultGetSnapshot = function (a, initialStateCacheRef) {
10
+ if ((0, utils_1.isService)(a)) {
11
+ // A status of 0 = interpreter not started
12
+ if (a.status === 0 && initialStateCacheRef.current) {
13
+ return initialStateCacheRef.current;
14
+ }
15
+ var snapshot = (0, utils_1.getServiceSnapshot)(a);
16
+ initialStateCacheRef.current = a.status === 0 ? snapshot : null;
17
+ return snapshot;
18
+ }
19
+ return (0, useActor_1.isActorWithState)(a) ? a.state : undefined;
18
20
  };
19
21
  function useSelector(actor, selector, compare, getSnapshot) {
20
22
  if (compare === void 0) { compare = defaultCompare; }
21
- if (getSnapshot === void 0) { getSnapshot = defaultGetSnapshot; }
23
+ var initialStateCacheRef = (0, react_1.useRef)(null);
22
24
  var subscribe = (0, react_1.useCallback)(function (handleStoreChange) {
23
25
  var unsubscribe = actor.subscribe(handleStoreChange).unsubscribe;
24
26
  return unsubscribe;
25
27
  }, [actor]);
26
- var boundGetSnapshot = (0, react_1.useCallback)(function () { return getSnapshot(actor); }, [
27
- actor,
28
- getSnapshot
29
- ]);
28
+ var boundGetSnapshot = (0, react_1.useCallback)(function () {
29
+ if (getSnapshot) {
30
+ return getSnapshot(actor);
31
+ }
32
+ return defaultGetSnapshot(actor, initialStateCacheRef);
33
+ }, [actor, getSnapshot]);
30
34
  var selectedSnapshot = (0, with_selector_1.useSyncExternalStoreWithSelector)(subscribe, boundGetSnapshot, boundGetSnapshot, selector, compare);
31
35
  return selectedSnapshot;
32
36
  }
package/lib/utils.d.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  import { Interpreter } from 'xstate';
2
2
  export declare function partition<T, A extends T, B extends T>(items: T[], predicate: (item: T) => item is A): [A[], B[]];
3
3
  export declare function getServiceSnapshot<TService extends Interpreter<any, any, any, any>>(service: TService): TService['state'];
4
+ export declare function shallowEqual(objA: any, objB: any): boolean;
5
+ export declare function isService(actor: any): actor is Interpreter<any, any, any, any>;
4
6
  //# 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.getServiceSnapshot = exports.partition = void 0;
30
+ exports.isService = exports.shallowEqual = 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];
@@ -53,6 +53,43 @@ function partition(items, predicate) {
53
53
  }
54
54
  exports.partition = partition;
55
55
  function getServiceSnapshot(service) {
56
- return service.status !== 0 ? service.state : service.machine.initialState;
56
+ return service.status !== 0
57
+ ? service.getSnapshot()
58
+ : service.machine.initialState;
57
59
  }
58
60
  exports.getServiceSnapshot = getServiceSnapshot;
61
+ // From https://github.com/reduxjs/react-redux/blob/master/src/utils/shallowEqual.ts
62
+ function is(x, y) {
63
+ if (x === y) {
64
+ return x !== 0 || y !== 0 || 1 / x === 1 / y;
65
+ }
66
+ else {
67
+ return x !== x && y !== y;
68
+ }
69
+ }
70
+ function shallowEqual(objA, objB) {
71
+ if (is(objA, objB))
72
+ return true;
73
+ if (typeof objA !== 'object' ||
74
+ objA === null ||
75
+ typeof objB !== 'object' ||
76
+ objB === null) {
77
+ return false;
78
+ }
79
+ var keysA = Object.keys(objA);
80
+ var keysB = Object.keys(objB);
81
+ if (keysA.length !== keysB.length)
82
+ return false;
83
+ for (var i = 0; i < keysA.length; i++) {
84
+ if (!Object.prototype.hasOwnProperty.call(objB, keysA[i]) ||
85
+ !is(objA[keysA[i]], objB[keysA[i]])) {
86
+ return false;
87
+ }
88
+ }
89
+ return true;
90
+ }
91
+ exports.shallowEqual = shallowEqual;
92
+ function isService(actor) {
93
+ return 'state' in actor && 'machine' in actor;
94
+ }
95
+ exports.isService = isService;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xstate/react",
3
- "version": "3.0.0",
3
+ "version": "3.0.2",
4
4
  "description": "XState tools for React",
5
5
  "keywords": [
6
6
  "state",
@@ -54,7 +54,7 @@
54
54
  "peerDependencies": {
55
55
  "@xstate/fsm": "^2.0.0",
56
56
  "react": "^16.8.0 || ^17.0.0 || ^18.0.0",
57
- "xstate": "^4.31.0"
57
+ "xstate": "^4.35.2"
58
58
  },
59
59
  "peerDependenciesMeta": {
60
60
  "@xstate/fsm": {
@@ -88,7 +88,7 @@
88
88
  "rollup-plugin-terser": "^5.1.2",
89
89
  "rollup-plugin-typescript2": "^0.30.0",
90
90
  "ts-jest": "^26.5.6",
91
- "typescript": "^4.5.2",
91
+ "typescript": "^4.8.4",
92
92
  "xstate": "*"
93
93
  }
94
94
  }
package/CHANGELOG.md DELETED
@@ -1,470 +0,0 @@
1
- # Changelog
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
-
28
- ## 2.0.1
29
-
30
- ### Patch Changes
31
-
32
- - [#3089](https://github.com/statelyai/xstate/pull/3089) [`862697e29`](https://github.com/statelyai/xstate/commit/862697e2990934d46050580d7e09c749d09d8426) Thanks [@Andarist](https://github.com/Andarist)! - Fixed compatibility with Skypack by exporting some shared utilities from root entry of XState and consuming them directly in other packages (this avoids accessing those things using deep imports and thus it avoids creating those compatibility problems).
33
-
34
- ## 2.0.0
35
-
36
- ### Major Changes
37
-
38
- - [#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.
39
-
40
- 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`.
41
-
42
- * [#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`.
43
-
44
- ### Patch Changes
45
-
46
- - [#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`.
47
-
48
- ## 1.6.3
49
-
50
- ### Patch Changes
51
-
52
- - [#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`.
53
-
54
- ## 1.6.2
55
-
56
- ### Patch Changes
57
-
58
- - [#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)
59
-
60
- * [#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`.
61
-
62
- ```js
63
- const machine = createMachine({
64
- initial: 'foo',
65
- context: { id: 1 },
66
- states: {
67
- foo: {
68
- on: {
69
- CHECK: {
70
- target: 'bar',
71
- cond: 'hasOverflown'
72
- }
73
- }
74
- },
75
- bar: {}
76
- }
77
- });
78
-
79
- const [id, setId] = useState(1);
80
- const [current, send] = useMachine(
81
- machine.withConfig({
82
- guards: {
83
- hasOverflown: () => id > 1 // id is a reference to an outside value
84
- }
85
- })
86
- );
87
-
88
- // later when id updates
89
- setId(2);
90
- // Now the reference passed to `useMachine` (the result of `machine.withConfig`) is updated but the interpreted machine stays the same. So the guard is still the previous one that got passed to the `useMachine` initially, and it closes over the stale `id`.
91
- ```
92
-
93
- ## 1.6.1
94
-
95
- ### Patch Changes
96
-
97
- - [#2587](https://github.com/statelyai/xstate/pull/2587) [`5aaa8445c`](https://github.com/statelyai/xstate/commit/5aaa8445c0041c6e9c47285c18e8b71cb2d805a7) Thanks [@Andarist](https://github.com/Andarist)! - Fixed an issue with implementations provided outside of React being wiped out and unusable.
98
-
99
- ## 1.6.0
100
-
101
- ### Minor Changes
102
-
103
- - [`4b4872ca`](https://github.com/statelyai/xstate/commit/4b4872cafd63f825f3918c6eb6fa84642d45e3e0) [#2241](https://github.com/statelyai/xstate/pull/2241) Thanks [@mattpocock](https://github.com/mattpocock)! - Changed the behaviour of guards, delays and activities when declared as options in `useMachine`/`useInterpret`.
104
-
105
- Previously, guards could not reference external props, because they would not be updated when the props changed. For instance:
106
-
107
- ```tsx
108
- const Modal = props => {
109
- useMachine(modalMachine, {
110
- guards: {
111
- isModalOpen: () => props.isOpen
112
- }
113
- });
114
- };
115
- ```
116
-
117
- When the component is created, `props.isOpen` would be checked and evaluated to the initial value. But if the guard is evaluated at any other time, it will not respond to the props' changed value.
118
-
119
- This is not true of actions/services. This will work as expected:
120
-
121
- ```tsx
122
- const Modal = props => {
123
- useMachine(modalMachine, {
124
- actions: {
125
- consoleLogModalOpen: () => {
126
- console.log(props.isOpen);
127
- }
128
- }
129
- });
130
- };
131
- ```
132
-
133
- This change brings guards and delays into line with actions and services.
134
-
135
- ⚠️ **NOTE:** Whenever possible, use data from within `context` rather than external data in your guards and delays.
136
-
137
- ### Patch Changes
138
-
139
- - [`fe3e859f`](https://github.com/statelyai/xstate/commit/fe3e859f5c53813307bacad915bebc8d1f3a982c) [#2522](https://github.com/statelyai/xstate/pull/2522) Thanks [@farskid](https://github.com/farskid), [@Andarist](https://github.com/Andarist)! - Fixed an issue with actors not being spawned correctly by `useMachine` and `useInterpret` when they were defined a lazily evaluated context, like for example here:
140
-
141
- ```js
142
- createMachine({
143
- // lazy context
144
- context: () => ({
145
- ref: spawn(() => {})
146
- })
147
- });
148
- ```
149
-
150
- ## 1.5.1
151
-
152
- ### Patch Changes
153
-
154
- - [`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.
155
-
156
- ## 1.5.0
157
-
158
- ### Minor Changes
159
-
160
- - [`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(...)`:
161
-
162
- ```tsx
163
- import { fromReducer } from 'xstate/lib/behaviors';
164
- import { useActor, useSpawn } from '@xstate/react';
165
-
166
- type CountEvent = { type: 'INC' } | { type: 'DEC' };
167
-
168
- const countBehavior = fromReducer(
169
- (count: number, event: CountEvent): number => {
170
- if (event.type === 'INC') {
171
- return count + 1;
172
- } else if (event.type === 'DEC') {
173
- return count - 1;
174
- }
175
-
176
- return count;
177
- },
178
- 0 // initial state
179
- );
180
-
181
- const countMachine = createMachine({
182
- invoke: {
183
- id: 'count',
184
- src: () => fromReducer(countReducer, 0)
185
- },
186
- on: {
187
- INC: {
188
- actions: forwardTo('count')
189
- },
190
- DEC: {
191
- actions: forwardTo('count')
192
- }
193
- }
194
- });
195
-
196
- const Component = () => {
197
- const countActorRef = useSpawn(countBehavior);
198
- const [count, send] = useActor(countActorRef);
199
-
200
- return (
201
- <div>
202
- Count: {count}
203
- <button onClick={() => send({ type: 'INC' })}>Increment</button>
204
- <button onClick={() => send({ type: 'DEC' })}>Decrement</button>
205
- </div>
206
- );
207
- };
208
- ```
209
-
210
- ## 1.4.0
211
-
212
- ### Minor Changes
213
-
214
- - [`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:
215
-
216
- ```diff
217
- -const [state, send] = useService(service);
218
- +const [state, send] = useActor(service);
219
- ```
220
-
221
- ### Patch Changes
222
-
223
- - [`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.
224
-
225
- ## 1.3.4
226
-
227
- ### Patch Changes
228
-
229
- - [`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.
230
-
231
- ## 1.3.3
232
-
233
- ### Patch Changes
234
-
235
- - [`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.
236
-
237
- ## 1.3.2
238
-
239
- ### Patch Changes
240
-
241
- - [`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.
242
-
243
- ```diff
244
- -import { useMachine } from '@xstate/react/lib/fsm'
245
- +import { useMachine } from '@xstate/react/fsm'
246
- ```
247
-
248
- ## 1.3.1
249
-
250
- ### Patch Changes
251
-
252
- - [`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
253
-
254
- * [`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.
255
-
256
- ## 1.3.0
257
-
258
- ### Minor Changes
259
-
260
- - [`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`:
261
-
262
- ```js
263
- import { useInterpret } from '@xstate/react';
264
- import { someMachine } from '../path/to/someMachine';
265
-
266
- const App = () => {
267
- const service = useInterpret(someMachine);
268
-
269
- // ...
270
- };
271
- ```
272
-
273
- * [`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)`:
274
-
275
- ```js
276
- import { useSelector } from '@xstate/react';
277
-
278
- const App = ({ someActor }) => {
279
- const count = useSelector(someActor, state => state.context.count);
280
-
281
- // ...
282
- };
283
- ```
284
-
285
- ## 1.2.2
286
-
287
- ### Patch Changes
288
-
289
- - [`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.
290
-
291
- ## 1.2.1
292
-
293
- ### Patch Changes
294
-
295
- - [`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.
296
-
297
- ## 1.2.0
298
-
299
- ### Minor Changes
300
-
301
- - [`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.
302
-
303
- ## 1.1.0
304
-
305
- ### Minor Changes
306
-
307
- - [`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:
308
-
309
- ```ts
310
- import { ActorRef } from 'xstate';
311
- import { useActor } from '@xstate/react';
312
-
313
- const Child: React.FC<{ actorRef: ActorRef<SomeEvent, SomeEmitted> }> = ({
314
- actorRef
315
- }) => {
316
- // `state` is typed as `SomeEmitted`
317
- // `send` can be called with `SomeEvent` values
318
- const [state, send] = useActor(actorRef);
319
-
320
- // . ..
321
- };
322
- ```
323
-
324
- It's also easier to specify the type of a spawned/invoked machine with `ActorRefFrom`:
325
-
326
- ```ts
327
- import { createMachine, ActorRefFrom } from 'xstate';
328
- import { useActor } from '@xstate/react';
329
-
330
- const someMachine = createMachine<SomeContext, SomeEvent>({
331
- // ...
332
- });
333
-
334
- const Child: React.FC<{ someRef: ActorRefFrom<typeof someMachine> }> = ({
335
- someRef
336
- }) => {
337
- // `state` is typed as `State<SomeContext, SomeEvent>`
338
- // `send` can be called with `SomeEvent` values
339
- const [state, send] = useActor(someRef);
340
-
341
- // . ..
342
- };
343
- ```
344
-
345
- ## 1.0.3
346
-
347
- ### Patch Changes
348
-
349
- - [`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.
350
-
351
- ## 1.0.2
352
-
353
- ### Patch Changes
354
-
355
- - [`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.
356
-
357
- * [`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:
358
-
359
- ```js
360
- const [state, send] = useService(someService);
361
-
362
- // ...
363
-
364
- // this is OK:
365
- send('ADD', { value: 3 });
366
-
367
- // which is equivalent to:
368
- send({ type: 'ADD', value: 3 });
369
- ```
370
-
371
- - [`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.
372
-
373
- * [`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.
374
-
375
- ## 1.0.1
376
-
377
- ### Patch Changes
378
-
379
- - [`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:
380
-
381
- > Attempted to use an actor-like object instead of a service in the useService() hook. Please use the useActor() hook instead.
382
-
383
- All notable changes to this project will be documented in this file.
384
-
385
- ## [1.0.0-rc.7]
386
-
387
- - The `machine` passed into `useMachine(machine)` can now be passed in lazily:
388
-
389
- ```js
390
- const [state, send] = useMachine(() => createMachine(/* ... */));
391
-
392
- // ...
393
- ```
394
-
395
- This has the benefit of avoiding unnecessary machine initializations whenever the component rerenders.
396
-
397
- - The `useActor` hook now takes a second argument: `getSnapshot` which is a function that should return the last emitted value:
398
-
399
- ```js
400
- const [state, send] = useActor(someActor, actor => actor.current);
401
- ```
402
-
403
- ## [1.0.0-rc.6]
404
-
405
- ## [1.0.0-rc.5]
406
-
407
- - You can now schedule actions in `useEffect` or `useLayoutEffect` via:
408
- - `asEffect` - queues the action to be executed in `useEffect`
409
- - `asLayoutEffect` - queues the action to be executed in `useLayoutEffect`
410
-
411
- ```jsx
412
- import { createMachine } from 'xstate';
413
- import { useMachine, asEffect } from '@xstate/react';
414
-
415
- const machine = createMachine({
416
- initial: 'focused',
417
- states: {
418
- focused: {
419
- entry: 'focus'
420
- }
421
- }
422
- });
423
-
424
- const Input = () => {
425
- const inputRef = useRef(null);
426
- const [state, send] = useMachine(machine, {
427
- actions: {
428
- focus: asEffect(() => {
429
- inputRef.current && inputRef.current.focus();
430
- })
431
- }
432
- });
433
-
434
- return <input ref={inputRef} />;
435
- };
436
- ```
437
-
438
- ## [0.8.1]
439
-
440
- - Services are now kept up to date
441
-
442
- ## [0.8.0]
443
-
444
- - The `useActor()` hook is now available.
445
- - Support for persisted states
446
-
447
- ## [0.7.1]
448
-
449
- - Actions passed into `useMachine(..., { actions: { ... } })` will now be kept up-to-date and no longer reference stale data.
450
-
451
- ## [0.7.0]
452
-
453
- ### Added
454
-
455
- - Machine configuration can now be merged into the options argument of `useMachine(machine, options)`. The following Machine Config options are available: `guards`, `actions`, `activities`, `services`, `delays` and `updates` (NOTE: `context` option is not implemented yet, use `withContext` or `withConfig` instead for the meantime)
456
-
457
- ```js
458
- const [current, send] = useMachine(someMachine, {
459
- actions: {
460
- doThing: doTheThing
461
- },
462
- services: {
463
- /* ... */
464
- },
465
- guards: {
466
- /* ... */
467
- }
468
- // ... etc.
469
- });
470
- ```