@xstate/react 3.1.0 → 3.1.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.
@@ -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=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};
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 h="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},y={useSyncExternalStore:void 0!==o.default.useSyncExternalStore?o.default.useSyncExternalStore:h};
11
11
  /**
12
12
  * @license React
13
13
  * use-sync-external-store-shim.development.js
@@ -17,7 +17,7 @@
17
17
  * This source code is licensed under the MIT license found in the
18
18
  * LICENSE file in the root directory of this source tree.
19
19
  */
20
- s((function(t,e){}));var S=s((function(t){t.exports=h}));
20
+ s((function(t,e){}));var S=s((function(t){t.exports=y}));
21
21
  /**
22
22
  * @license React
23
23
  * use-sync-external-store-shim/with-selector.production.min.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=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 N(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}function I(t){return t}function A(t){return 0!==t.status?t.getSnapshot():t.machine.initialState}function V(t,e){return t===e?0!==t||0!==e||1/t==1/e:t!=t&&e!=e}function M(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?M(t)?A(t):t.getSnapshot():_(t)?t.state:void 0}function W(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]}var L=function(t,e){return t===e};function T(t,n,r,u){void 0===r&&(r=L);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(M(t)){if(0===t.status&&e.current)return e.current;var n=A(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.createActorContext=function(t){var e=a.createContext(null),n=e.Provider;function r(e){var r=e.children,u=e.machine,o=N(void 0===u?t:u);return a.createElement(n,{value:o},r)}function u(){var t=a.useContext(e);if(!t)throw new Error('You used a hook from "'.concat(r.displayName,"\" but it's not inside a <").concat(r.displayName,"> component."));return t}return r.displayName="ActorProvider(".concat(t.id,")"),{Provider:r,useActorRef:u,useActor:function(){return W(u())},useSelector:function(t,e){return T(u(),t,e)}}},t.shallowEqual=function(t,e){if(V(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])||!V(t[n[u]],e[n[u]]))return!1;return!0},t.useActor=W,t.useInterpret=N,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=T,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 N(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}function I(t){return 0!==t.status?t.getSnapshot():t.machine.initialState}function A(t,e){return t===e?0!==t||0!==e||1/t==1/e:t!=t&&e!=e}function V(t){return"state"in t&&"machine"in t}function M(t,e,r){if(t.status===n.InterpreterStatus.NotStarted)return!0;var u=void 0===r.changed&&(Object.keys(r.children).length>0||"boolean"==typeof e.changed);return!(r.changed||u)}function W(t){return t}function _(t){return t}function q(t){return"state"in t}function D(t){return"deferred"in t}function L(t){return"getSnapshot"in t?V(t)?I(t):t.getSnapshot():q(t)?t.state:void 0}function T(t,n){void 0===n&&(n=L);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=e.useCallback((function(e,n){return V(t)?M(t,e,n):e===n}),[t]),c=k.useSyncExternalStoreWithSelector(o,a,a,_,i),s=P((function(){return function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];var n=t[0],o=r.current;D(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]),[c,s]}var X=function(t,e){return t===e};function z(t,n,r,u){void 0===r&&(r=X);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(V(t)){if(0===t.status&&e.current)return e.current;var n=I(t);return e.current=0===t.status?n:null,n}return q(t)?t.state:void 0}(t,o)}),[t,u]);return k.useSyncExternalStoreWithSelector(a,i,i,n,r)}t.createActorContext=function(t){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];var r=c(e,2),u=r[0],o=void 0===u?{}:u,i=r[1],s=a.createContext(null),f=s.Provider;function l(e){var n=e.children,r=e.machine,u=N(void 0===r?t:r,o,i);return a.createElement(f,{value:u},n)}function v(){var t=a.useContext(s);if(!t)throw new Error('You used a hook from "'.concat(l.displayName,"\" but it's not inside a <").concat(l.displayName,"> component."));return t}function d(){return T(v())}function p(t,e){return z(v(),t,e)}return l.displayName="ActorProvider(".concat(t.id,")"),{Provider:l,useActorRef:v,useActor:d,useSelector:p}},t.shallowEqual=function(t,e){if(A(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])||!A(t[n[u]],e[n[u]]))return!1;return!0},t.useActor=T,t.useInterpret=N,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){return M(s,t,e)}),[s]),v=e.useCallback((function(t){return s.subscribe(t).unsubscribe}),[s]),d=k.useSyncExternalStoreWithSelector(v,f,f,W,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=z,t.useSpawn=function(t){return P((function(){return n.spawnBehavior(t)}))},Object.defineProperty(t,"__esModule",{value:!0})}));
@@ -1,7 +1,8 @@
1
1
  import * as React from 'react';
2
+ import { RestParams } from './useInterpret';
2
3
  import { useActor as useActorUnbound } from './useActor';
3
4
  import { ActorRefFrom, AnyStateMachine, EmittedFrom } from 'xstate';
4
- export declare function createActorContext<TMachine extends AnyStateMachine>(machine: TMachine): {
5
+ export declare function createActorContext<TMachine extends AnyStateMachine>(machine: TMachine, ...[options, observerOrListener]: RestParams<TMachine>): {
5
6
  useActor: () => ReturnType<typeof useActorUnbound<ActorRefFrom<TMachine>>>;
6
7
  useSelector: <T>(selector: (snapshot: EmittedFrom<TMachine>) => T, compare?: (a: T, b: T) => boolean) => T;
7
8
  useActorRef: () => ActorRefFrom<TMachine>;
@@ -1,13 +1,34 @@
1
+ var __read = (this && this.__read) || function (o, n) {
2
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
3
+ if (!m) return o;
4
+ var i = m.call(o), r, ar = [], e;
5
+ try {
6
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
7
+ }
8
+ catch (error) { e = { error: error }; }
9
+ finally {
10
+ try {
11
+ if (r && !r.done && (m = i["return"])) m.call(i);
12
+ }
13
+ finally { if (e) throw e.error; }
14
+ }
15
+ return ar;
16
+ };
1
17
  import * as React from 'react';
2
18
  import { useInterpret } from './useInterpret';
3
19
  import { useActor as useActorUnbound } from './useActor';
4
20
  import { useSelector as useSelectorUnbound } from './useSelector';
5
21
  export function createActorContext(machine) {
22
+ var _a = [];
23
+ for (var _i = 1; _i < arguments.length; _i++) {
24
+ _a[_i - 1] = arguments[_i];
25
+ }
26
+ var _b = __read(_a, 2), _c = _b[0], options = _c === void 0 ? {} : _c, observerOrListener = _b[1];
6
27
  var ReactContext = React.createContext(null);
7
28
  var OriginalProvider = ReactContext.Provider;
8
29
  function Provider(_a) {
9
30
  var children = _a.children, _b = _a.machine, providedMachine = _b === void 0 ? machine : _b;
10
- var actor = useInterpret(providedMachine);
31
+ var actor = useInterpret(providedMachine, options, observerOrListener);
11
32
  return React.createElement(OriginalProvider, { value: actor }, children);
12
33
  }
13
34
  Provider.displayName = "ActorProvider(".concat(machine.id, ")");
package/es/useActor.js CHANGED
@@ -1,8 +1,11 @@
1
1
  import { useRef, useCallback } from 'react';
2
2
  import useIsomorphicLayoutEffect from 'use-isomorphic-layout-effect';
3
3
  import useConstant from './useConstant';
4
- import { useSyncExternalStore } from 'use-sync-external-store/shim';
5
- import { getServiceSnapshot, isService } from './utils';
4
+ import { useSyncExternalStoreWithSelector } from 'use-sync-external-store/shim/with-selector';
5
+ import { getServiceSnapshot, isInterpreterStateEqual, isService } from './utils';
6
+ function identity(a) {
7
+ return a;
8
+ }
6
9
  export function isActorWithState(actorRef) {
7
10
  return 'state' in actorRef;
8
11
  }
@@ -26,11 +29,14 @@ export function useActor(actorRef, getSnapshot) {
26
29
  var unsubscribe = actorRef.subscribe(handleStoreChange).unsubscribe;
27
30
  return unsubscribe;
28
31
  }, [actorRef]);
29
- var boundGetSnapshot = useCallback(function () { return getSnapshot(actorRef); }, [
30
- actorRef,
31
- getSnapshot
32
- ]);
33
- var storeSnapshot = useSyncExternalStore(subscribe, boundGetSnapshot, boundGetSnapshot);
32
+ var boundGetSnapshot = useCallback(function () { return getSnapshot(actorRef); }, [actorRef, getSnapshot]);
33
+ var isEqual = useCallback(function (prevState, nextState) {
34
+ if (isService(actorRef)) {
35
+ return isInterpreterStateEqual(actorRef, prevState, nextState);
36
+ }
37
+ return prevState === nextState;
38
+ }, [actorRef]);
39
+ var storeSnapshot = useSyncExternalStoreWithSelector(subscribe, boundGetSnapshot, boundGetSnapshot, identity, isEqual);
34
40
  var send = useConstant(function () { return function () {
35
41
  var args = [];
36
42
  for (var _i = 0; _i < arguments.length; _i++) {
@@ -2,7 +2,7 @@ import { AnyInterpreter, AnyStateMachine, AreAllImplementationsAssumedToBeProvid
2
2
  import { MaybeLazy } from './types';
3
3
  import { UseMachineOptions } from './useMachine';
4
4
  export declare function useIdleInterpreter(getMachine: MaybeLazy<AnyStateMachine>, options: Partial<InterpreterOptions> & Partial<UseMachineOptions<unknown, never>> & Partial<MachineOptions<unknown, never>>): AnyInterpreter;
5
- declare type RestParams<TMachine extends AnyStateMachine> = AreAllImplementationsAssumedToBeProvided<TMachine['__TResolvedTypesMeta']> extends false ? [
5
+ export declare type RestParams<TMachine extends AnyStateMachine> = AreAllImplementationsAssumedToBeProvided<TMachine['__TResolvedTypesMeta']> extends false ? [
6
6
  options: InterpreterOptions & UseMachineOptions<TMachine['__TContext'], TMachine['__TEvent']> & InternalMachineOptions<TMachine['__TContext'], TMachine['__TEvent'], TMachine['__TResolvedTypesMeta'], true>,
7
7
  observerOrListener?: Observer<StateFrom<TMachine>> | ((value: StateFrom<TMachine>) => void)
8
8
  ] : [
@@ -10,5 +10,4 @@ declare type RestParams<TMachine extends AnyStateMachine> = AreAllImplementation
10
10
  observerOrListener?: Observer<StateFrom<TMachine>> | ((value: StateFrom<TMachine>) => void)
11
11
  ];
12
12
  export declare function useInterpret<TMachine extends AnyStateMachine>(getMachine: MaybeLazy<TMachine>, ...[options, observerOrListener]: RestParams<TMachine>): InterpreterFrom<TMachine>;
13
- export {};
14
13
  //# sourceMappingURL=useInterpret.d.ts.map
package/es/useMachine.js CHANGED
@@ -18,6 +18,7 @@ import { useCallback, useEffect } from 'react';
18
18
  import { useSyncExternalStoreWithSelector } from 'use-sync-external-store/shim/with-selector';
19
19
  import { InterpreterStatus, State } from 'xstate';
20
20
  import { useIdleInterpreter } from './useInterpret';
21
+ import { isInterpreterStateEqual } from './utils';
21
22
  function identity(a) {
22
23
  return a;
23
24
  }
@@ -39,18 +40,7 @@ export function useMachine(getMachine) {
39
40
  return service.getSnapshot();
40
41
  }, [service]);
41
42
  var isEqual = useCallback(function (prevState, nextState) {
42
- if (service.status === InterpreterStatus.NotStarted) {
43
- return true;
44
- }
45
- // Only change the current state if:
46
- // - the incoming state is the "live" initial state (since it might have new actors)
47
- // - OR the incoming state actually changed.
48
- //
49
- // The "live" initial state will have .changed === undefined.
50
- var initialStateChanged = nextState.changed === undefined &&
51
- (Object.keys(nextState.children).length > 0 ||
52
- typeof prevState.changed === 'boolean');
53
- return !(nextState.changed || initialStateChanged);
43
+ return isInterpreterStateEqual(service, prevState, nextState);
54
44
  }, [service]);
55
45
  var subscribe = useCallback(function (handleStoreChange) {
56
46
  var unsubscribe = service.subscribe(handleStoreChange).unsubscribe;
package/es/utils.d.ts CHANGED
@@ -1,6 +1,7 @@
1
- import { Interpreter } from 'xstate';
1
+ import { AnyInterpreter, AnyState, 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
4
  export declare function shallowEqual(objA: any, objB: any): boolean;
5
5
  export declare function isService(actor: any): actor is Interpreter<any, any, any, any>;
6
+ export declare function isInterpreterStateEqual(service: AnyInterpreter, prevState: AnyState, nextState: AnyState): boolean;
6
7
  //# sourceMappingURL=utils.d.ts.map
package/es/utils.js CHANGED
@@ -25,6 +25,7 @@ var __values = (this && this.__values) || function(o) {
25
25
  };
26
26
  throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
27
27
  };
28
+ import { InterpreterStatus } from 'xstate';
28
29
  export function partition(items, predicate) {
29
30
  var e_1, _a;
30
31
  var _b = __read([[], []], 2), truthy = _b[0], falsy = _b[1];
@@ -86,3 +87,17 @@ export function shallowEqual(objA, objB) {
86
87
  export function isService(actor) {
87
88
  return 'state' in actor && 'machine' in actor;
88
89
  }
90
+ export function isInterpreterStateEqual(service, prevState, nextState) {
91
+ if (service.status === InterpreterStatus.NotStarted) {
92
+ return true;
93
+ }
94
+ // Only change the current state if:
95
+ // - the incoming state is the "live" initial state (since it might have new actors)
96
+ // - OR the incoming state actually changed.
97
+ //
98
+ // The "live" initial state will have .changed === undefined.
99
+ var initialStateChanged = nextState.changed === undefined &&
100
+ (Object.keys(nextState.children).length > 0 ||
101
+ typeof prevState.changed === 'boolean');
102
+ return !(nextState.changed || initialStateChanged);
103
+ }
@@ -1,7 +1,8 @@
1
1
  import * as React from 'react';
2
+ import { RestParams } from './useInterpret';
2
3
  import { useActor as useActorUnbound } from './useActor';
3
4
  import { ActorRefFrom, AnyStateMachine, EmittedFrom } from 'xstate';
4
- export declare function createActorContext<TMachine extends AnyStateMachine>(machine: TMachine): {
5
+ export declare function createActorContext<TMachine extends AnyStateMachine>(machine: TMachine, ...[options, observerOrListener]: RestParams<TMachine>): {
5
6
  useActor: () => ReturnType<typeof useActorUnbound<ActorRefFrom<TMachine>>>;
6
7
  useSelector: <T>(selector: (snapshot: EmittedFrom<TMachine>) => T, compare?: (a: T, b: T) => boolean) => T;
7
8
  useActorRef: () => ActorRefFrom<TMachine>;
@@ -1,4 +1,20 @@
1
1
  "use strict";
2
+ var __read = (this && this.__read) || function (o, n) {
3
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
4
+ if (!m) return o;
5
+ var i = m.call(o), r, ar = [], e;
6
+ try {
7
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
8
+ }
9
+ catch (error) { e = { error: error }; }
10
+ finally {
11
+ try {
12
+ if (r && !r.done && (m = i["return"])) m.call(i);
13
+ }
14
+ finally { if (e) throw e.error; }
15
+ }
16
+ return ar;
17
+ };
2
18
  Object.defineProperty(exports, "__esModule", { value: true });
3
19
  exports.createActorContext = void 0;
4
20
  var React = require("react");
@@ -6,11 +22,16 @@ var useInterpret_1 = require("./useInterpret");
6
22
  var useActor_1 = require("./useActor");
7
23
  var useSelector_1 = require("./useSelector");
8
24
  function createActorContext(machine) {
25
+ var _a = [];
26
+ for (var _i = 1; _i < arguments.length; _i++) {
27
+ _a[_i - 1] = arguments[_i];
28
+ }
29
+ var _b = __read(_a, 2), _c = _b[0], options = _c === void 0 ? {} : _c, observerOrListener = _b[1];
9
30
  var ReactContext = React.createContext(null);
10
31
  var OriginalProvider = ReactContext.Provider;
11
32
  function Provider(_a) {
12
33
  var children = _a.children, _b = _a.machine, providedMachine = _b === void 0 ? machine : _b;
13
- var actor = (0, useInterpret_1.useInterpret)(providedMachine);
34
+ var actor = (0, useInterpret_1.useInterpret)(providedMachine, options, observerOrListener);
14
35
  return React.createElement(OriginalProvider, { value: actor }, children);
15
36
  }
16
37
  Provider.displayName = "ActorProvider(".concat(machine.id, ")");
package/lib/useActor.js CHANGED
@@ -4,8 +4,11 @@ exports.useActor = exports.isActorWithState = void 0;
4
4
  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
- var shim_1 = require("use-sync-external-store/shim");
7
+ var with_selector_1 = require("use-sync-external-store/shim/with-selector");
8
8
  var utils_1 = require("./utils");
9
+ function identity(a) {
10
+ return a;
11
+ }
9
12
  function isActorWithState(actorRef) {
10
13
  return 'state' in actorRef;
11
14
  }
@@ -30,11 +33,14 @@ function useActor(actorRef, getSnapshot) {
30
33
  var unsubscribe = actorRef.subscribe(handleStoreChange).unsubscribe;
31
34
  return unsubscribe;
32
35
  }, [actorRef]);
33
- var boundGetSnapshot = (0, react_1.useCallback)(function () { return getSnapshot(actorRef); }, [
34
- actorRef,
35
- getSnapshot
36
- ]);
37
- var storeSnapshot = (0, shim_1.useSyncExternalStore)(subscribe, boundGetSnapshot, boundGetSnapshot);
36
+ var boundGetSnapshot = (0, react_1.useCallback)(function () { return getSnapshot(actorRef); }, [actorRef, getSnapshot]);
37
+ var isEqual = (0, react_1.useCallback)(function (prevState, nextState) {
38
+ if ((0, utils_1.isService)(actorRef)) {
39
+ return (0, utils_1.isInterpreterStateEqual)(actorRef, prevState, nextState);
40
+ }
41
+ return prevState === nextState;
42
+ }, [actorRef]);
43
+ var storeSnapshot = (0, with_selector_1.useSyncExternalStoreWithSelector)(subscribe, boundGetSnapshot, boundGetSnapshot, identity, isEqual);
38
44
  var send = (0, useConstant_1.default)(function () { return function () {
39
45
  var args = [];
40
46
  for (var _i = 0; _i < arguments.length; _i++) {
@@ -2,7 +2,7 @@ import { AnyInterpreter, AnyStateMachine, AreAllImplementationsAssumedToBeProvid
2
2
  import { MaybeLazy } from './types';
3
3
  import { UseMachineOptions } from './useMachine';
4
4
  export declare function useIdleInterpreter(getMachine: MaybeLazy<AnyStateMachine>, options: Partial<InterpreterOptions> & Partial<UseMachineOptions<unknown, never>> & Partial<MachineOptions<unknown, never>>): AnyInterpreter;
5
- declare type RestParams<TMachine extends AnyStateMachine> = AreAllImplementationsAssumedToBeProvided<TMachine['__TResolvedTypesMeta']> extends false ? [
5
+ export declare type RestParams<TMachine extends AnyStateMachine> = AreAllImplementationsAssumedToBeProvided<TMachine['__TResolvedTypesMeta']> extends false ? [
6
6
  options: InterpreterOptions & UseMachineOptions<TMachine['__TContext'], TMachine['__TEvent']> & InternalMachineOptions<TMachine['__TContext'], TMachine['__TEvent'], TMachine['__TResolvedTypesMeta'], true>,
7
7
  observerOrListener?: Observer<StateFrom<TMachine>> | ((value: StateFrom<TMachine>) => void)
8
8
  ] : [
@@ -10,5 +10,4 @@ declare type RestParams<TMachine extends AnyStateMachine> = AreAllImplementation
10
10
  observerOrListener?: Observer<StateFrom<TMachine>> | ((value: StateFrom<TMachine>) => void)
11
11
  ];
12
12
  export declare function useInterpret<TMachine extends AnyStateMachine>(getMachine: MaybeLazy<TMachine>, ...[options, observerOrListener]: RestParams<TMachine>): InterpreterFrom<TMachine>;
13
- export {};
14
13
  //# sourceMappingURL=useInterpret.d.ts.map
package/lib/useMachine.js CHANGED
@@ -21,6 +21,7 @@ var react_1 = require("react");
21
21
  var with_selector_1 = require("use-sync-external-store/shim/with-selector");
22
22
  var xstate_1 = require("xstate");
23
23
  var useInterpret_1 = require("./useInterpret");
24
+ var utils_1 = require("./utils");
24
25
  function identity(a) {
25
26
  return a;
26
27
  }
@@ -42,18 +43,7 @@ function useMachine(getMachine) {
42
43
  return service.getSnapshot();
43
44
  }, [service]);
44
45
  var isEqual = (0, react_1.useCallback)(function (prevState, nextState) {
45
- if (service.status === xstate_1.InterpreterStatus.NotStarted) {
46
- return true;
47
- }
48
- // Only change the current state if:
49
- // - the incoming state is the "live" initial state (since it might have new actors)
50
- // - OR the incoming state actually changed.
51
- //
52
- // The "live" initial state will have .changed === undefined.
53
- var initialStateChanged = nextState.changed === undefined &&
54
- (Object.keys(nextState.children).length > 0 ||
55
- typeof prevState.changed === 'boolean');
56
- return !(nextState.changed || initialStateChanged);
46
+ return (0, utils_1.isInterpreterStateEqual)(service, prevState, nextState);
57
47
  }, [service]);
58
48
  var subscribe = (0, react_1.useCallback)(function (handleStoreChange) {
59
49
  var unsubscribe = service.subscribe(handleStoreChange).unsubscribe;
package/lib/utils.d.ts CHANGED
@@ -1,6 +1,7 @@
1
- import { Interpreter } from 'xstate';
1
+ import { AnyInterpreter, AnyState, 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
4
  export declare function shallowEqual(objA: any, objB: any): boolean;
5
5
  export declare function isService(actor: any): actor is Interpreter<any, any, any, any>;
6
+ export declare function isInterpreterStateEqual(service: AnyInterpreter, prevState: AnyState, nextState: AnyState): boolean;
6
7
  //# sourceMappingURL=utils.d.ts.map
package/lib/utils.js CHANGED
@@ -27,7 +27,8 @@ 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.isService = exports.shallowEqual = exports.getServiceSnapshot = exports.partition = void 0;
30
+ exports.isInterpreterStateEqual = exports.isService = exports.shallowEqual = exports.getServiceSnapshot = exports.partition = void 0;
31
+ var xstate_1 = require("xstate");
31
32
  function partition(items, predicate) {
32
33
  var e_1, _a;
33
34
  var _b = __read([[], []], 2), truthy = _b[0], falsy = _b[1];
@@ -93,3 +94,18 @@ function isService(actor) {
93
94
  return 'state' in actor && 'machine' in actor;
94
95
  }
95
96
  exports.isService = isService;
97
+ function isInterpreterStateEqual(service, prevState, nextState) {
98
+ if (service.status === xstate_1.InterpreterStatus.NotStarted) {
99
+ return true;
100
+ }
101
+ // Only change the current state if:
102
+ // - the incoming state is the "live" initial state (since it might have new actors)
103
+ // - OR the incoming state actually changed.
104
+ //
105
+ // The "live" initial state will have .changed === undefined.
106
+ var initialStateChanged = nextState.changed === undefined &&
107
+ (Object.keys(nextState.children).length > 0 ||
108
+ typeof prevState.changed === 'boolean');
109
+ return !(nextState.changed || initialStateChanged);
110
+ }
111
+ exports.isInterpreterStateEqual = isInterpreterStateEqual;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xstate/react",
3
- "version": "3.1.0",
3
+ "version": "3.1.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.35.3"
57
+ "xstate": "^4.35.4"
58
58
  },
59
59
  "peerDependenciesMeta": {
60
60
  "@xstate/fsm": {