@xstate/react
Advanced tools
Comparing version 3.0.1 to 3.0.2
@@ -1,2 +0,2 @@ | ||
!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 | ||
!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 | ||
/** | ||
@@ -10,3 +10,3 @@ * @license React | ||
* LICENSE file in the root directory of this source tree. | ||
*/}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}; | ||
*/}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}; | ||
/** | ||
@@ -30,3 +30,3 @@ * @license React | ||
* LICENSE file in the root directory of this source tree. | ||
*/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=a.default.useRef,x=a.default.useEffect,m=a.default.useMemo,E=a.default.useDebugValue,w={useSyncExternalStoreWithSelector:function(t,e,n,r,u){var a=j(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 x((function(){o.hasValue=!0,o.value=i}),[i]),E(i),i}}; | ||
*/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}}; | ||
/** | ||
@@ -41,2 +41,2 @@ * @license React | ||
*/ | ||
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 R(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 I(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}function _(t,e){return t===e?0!==t||0!==e||1/t==1/e:t!=t&&e!=e}var q=function(t,e){return t===e};t.shallowEqual=function(t,e){if(_(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])||!_(t[n[u]],e[n[u]]))return!1;return!0},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=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 a=c(r,1),o=a[0],i=void 0===o?{}:o,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.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,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=q);var a=e.useRef(null),o=e.useCallback((function(e){return t.subscribe(e).unsubscribe}),[t]),i=e.useCallback((function(){return u?u(t):function(t,e){if("state"in(u=t)&&"machine"in u){if(0===t.status&&e.current)return e.current;var n=0!==(r=t).status?r.state:r.machine.initialState;return e.current=0===t.status?n:null,n}var r,u;return V(t)?t.state:void 0}(t,a)}),[t,u]);return k.useSyncExternalStoreWithSelector(o,i,i,n,r)},t.useSpawn=function(t){return P((function(){return n.spawnBehavior(t)}))},Object.defineProperty(t,"__esModule",{value:!0})})); | ||
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})})); |
@@ -5,2 +5,3 @@ import { useRef, useCallback } from 'react'; | ||
import { useSyncExternalStore } from 'use-sync-external-store/shim'; | ||
import { getServiceSnapshot, isService } from './utils'; | ||
export function isActorWithState(actorRef) { | ||
@@ -14,3 +15,5 @@ return 'state' in actorRef; | ||
return 'getSnapshot' in actorRef | ||
? actorRef.getSnapshot() | ||
? isService(actorRef) | ||
? getServiceSnapshot(actorRef) | ||
: actorRef.getSnapshot() | ||
: isActorWithState(actorRef) | ||
@@ -17,0 +20,0 @@ ? actorRef.state |
@@ -39,3 +39,3 @@ var __read = (this && this.__read) || function (o, n) { | ||
} | ||
return service.state; | ||
return service.getSnapshot(); | ||
}, [service]); | ||
@@ -42,0 +42,0 @@ var isEqual = useCallback(function (prevState, nextState) { |
import { useCallback, useRef } from 'react'; | ||
import { useSyncExternalStoreWithSelector } from 'use-sync-external-store/shim/with-selector'; | ||
import { isActorWithState } from './useActor'; | ||
import { getServiceSnapshot } from './utils'; | ||
function isService(actor) { | ||
return 'state' in actor && 'machine' in actor; | ||
} | ||
import { getServiceSnapshot, isService } from './utils'; | ||
var defaultCompare = function (a, b) { return a === b; }; | ||
@@ -9,0 +6,0 @@ var defaultGetSnapshot = function (a, initialStateCacheRef) { |
@@ -5,2 +5,3 @@ import { Interpreter } from 'xstate'; | ||
export declare function shallowEqual(objA: any, objB: any): boolean; | ||
export declare function isService(actor: any): actor is Interpreter<any, any, any, any>; | ||
//# sourceMappingURL=utils.d.ts.map |
@@ -52,3 +52,5 @@ var __read = (this && this.__read) || function (o, n) { | ||
export function getServiceSnapshot(service) { | ||
return service.status !== 0 ? service.state : service.machine.initialState; | ||
return service.status !== 0 | ||
? service.getSnapshot() | ||
: service.machine.initialState; | ||
} | ||
@@ -85,1 +87,4 @@ // From https://github.com/reduxjs/react-redux/blob/master/src/utils/shallowEqual.ts | ||
} | ||
export function isService(actor) { | ||
return 'state' in actor && 'machine' in actor; | ||
} |
@@ -8,2 +8,3 @@ "use strict"; | ||
var shim_1 = require("use-sync-external-store/shim"); | ||
var utils_1 = require("./utils"); | ||
function isActorWithState(actorRef) { | ||
@@ -18,3 +19,5 @@ return 'state' in actorRef; | ||
return 'getSnapshot' in actorRef | ||
? actorRef.getSnapshot() | ||
? (0, utils_1.isService)(actorRef) | ||
? (0, utils_1.getServiceSnapshot)(actorRef) | ||
: actorRef.getSnapshot() | ||
: isActorWithState(actorRef) | ||
@@ -21,0 +24,0 @@ ? actorRef.state |
@@ -42,3 +42,3 @@ "use strict"; | ||
} | ||
return service.state; | ||
return service.getSnapshot(); | ||
}, [service]); | ||
@@ -45,0 +45,0 @@ var isEqual = (0, react_1.useCallback)(function (prevState, nextState) { |
@@ -8,8 +8,5 @@ "use strict"; | ||
var utils_1 = require("./utils"); | ||
function isService(actor) { | ||
return 'state' in actor && 'machine' in actor; | ||
} | ||
var defaultCompare = function (a, b) { return a === b; }; | ||
var defaultGetSnapshot = function (a, initialStateCacheRef) { | ||
if (isService(a)) { | ||
if ((0, utils_1.isService)(a)) { | ||
// A status of 0 = interpreter not started | ||
@@ -16,0 +13,0 @@ if (a.status === 0 && initialStateCacheRef.current) { |
@@ -5,2 +5,3 @@ import { Interpreter } from 'xstate'; | ||
export declare function shallowEqual(objA: any, objB: any): boolean; | ||
export declare function isService(actor: any): actor is Interpreter<any, any, any, any>; | ||
//# sourceMappingURL=utils.d.ts.map |
@@ -30,3 +30,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.shallowEqual = exports.getServiceSnapshot = exports.partition = void 0; | ||
exports.isService = exports.shallowEqual = exports.getServiceSnapshot = exports.partition = void 0; | ||
function partition(items, predicate) { | ||
@@ -57,3 +57,5 @@ var e_1, _a; | ||
function getServiceSnapshot(service) { | ||
return service.status !== 0 ? service.state : service.machine.initialState; | ||
return service.status !== 0 | ||
? service.getSnapshot() | ||
: service.machine.initialState; | ||
} | ||
@@ -92,1 +94,5 @@ exports.getServiceSnapshot = getServiceSnapshot; | ||
exports.shallowEqual = shallowEqual; | ||
function isService(actor) { | ||
return 'state' in actor && 'machine' in actor; | ||
} | ||
exports.isService = isService; |
{ | ||
"name": "@xstate/react", | ||
"version": "3.0.1", | ||
"version": "3.0.2", | ||
"description": "XState tools for React", | ||
@@ -57,3 +57,3 @@ "keywords": [ | ||
"react": "^16.8.0 || ^17.0.0 || ^18.0.0", | ||
"xstate": "^4.33.0" | ||
"xstate": "^4.35.2" | ||
}, | ||
@@ -92,5 +92,5 @@ "peerDependenciesMeta": { | ||
"ts-jest": "^26.5.6", | ||
"typescript": "^4.5.2", | ||
"typescript": "^4.8.4", | ||
"xstate": "*" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
71523
1280