use-sync-external-store
Advanced tools
Comparing version 0.0.0-experimental-95d762e40-20210908 to 0.0.0-experimental-a8cabb564-20210915
@@ -32,6 +32,22 @@ /** @license React vundefined | ||
var useMemo = React.useMemo, | ||
var useRef = React.useRef, | ||
useEffect = React.useEffect, | ||
useMemo = React.useMemo, | ||
useDebugValue = React.useDebugValue; // Same as useSyncExternalStore, but supports selector and isEqual arguments. | ||
function useSyncExternalStoreExtra(subscribe, getSnapshot, selector, isEqual) { | ||
// Use this to track the rendered snapshot. | ||
var instRef = useRef(null); | ||
var inst; | ||
if (instRef.current === null) { | ||
inst = { | ||
hasValue: false, | ||
value: null | ||
}; | ||
instRef.current = inst; | ||
} else { | ||
inst = instRef.current; | ||
} | ||
var getSnapshotWithMemoizedSelector = useMemo(function () { | ||
@@ -55,2 +71,16 @@ // Track the memoized state using closure variables that are local to this | ||
if (isEqual !== undefined) { | ||
// Even if the selector has changed, the currently rendered selection | ||
// may be equal to the new selection. We should attempt to reuse the | ||
// current value if possible, to preserve downstream memoizations. | ||
if (inst.hasValue) { | ||
var currentSelection = inst.value; | ||
if (isEqual(currentSelection, _nextSelection)) { | ||
memoizedSelection = currentSelection; | ||
return currentSelection; | ||
} | ||
} | ||
} | ||
memoizedSelection = _nextSelection; | ||
@@ -85,2 +115,6 @@ return _nextSelection; | ||
var value = useSyncExternalStore.useSyncExternalStore(subscribe, getSnapshotWithMemoizedSelector); | ||
useEffect(function () { | ||
inst.hasValue = true; | ||
inst.value = value; | ||
}, [value]); | ||
useDebugValue(value); | ||
@@ -87,0 +121,0 @@ return value; |
@@ -9,2 +9,3 @@ /** @license React vundefined | ||
*/ | ||
'use strict';var k=require("react"),l=require("use-sync-external-store");function m(a,c){return a===c&&(0!==a||1/a===1/c)||a!==a&&c!==c}var n="function"===typeof Object.is?Object.is:m,p=k.useMemo,q=k.useDebugValue;exports.useSyncExternalStoreExtra=function(a,c,e,f){var r=p(function(){var a=!1,g,h;return function(){var b=c();if(!a){a=!0;g=b;var d=e(b);return h=d}d=h;if(n(g,b))return d;g=b;b=e(b);return void 0!==f&&f(d,b)?d:h=b}},[c,e,f]);a=l.useSyncExternalStore(a,r);q(a);return a}; | ||
'use strict';var k=require("react"),m=require("use-sync-external-store");function n(b,e){return b===e&&(0!==b||1/b===1/e)||b!==b&&e!==e}var p="function"===typeof Object.is?Object.is:n,q=k.useRef,r=k.useEffect,t=k.useMemo,u=k.useDebugValue; | ||
exports.useSyncExternalStoreExtra=function(b,e,l,h){var c=q(null);if(null===c.current){var f={hasValue:!1,value:null};c.current=f}else f=c.current;c=t(function(){var b=!1,c,d;return function(){var a=e();if(!b){b=!0;c=a;var g=l(a);return void 0!==h&&f.hasValue&&(a=f.value,h(a,g))?d=a:d=g}g=d;if(p(c,a))return g;c=a;a=l(a);return void 0!==h&&h(g,a)?g:d=a}},[e,l,h]);var d=m.useSyncExternalStore(b,c);r(function(){f.hasValue=!0;f.value=d},[d]);u(d);return d}; |
@@ -73,3 +73,3 @@ /** @license React vundefined | ||
useDebugValue = React.useDebugValue, | ||
builtInAPI = React.useSyncExternalStore; // Prefer the built-in API, if it exists. If it doesn't exist, then we assume | ||
builtInAPI = React.unstable_useSyncExternalStore; // Prefer the built-in API, if it exists. If it doesn't exist, then we assume | ||
// we're in version 16 or 17, so rendering is always synchronous. The shim | ||
@@ -76,0 +76,0 @@ // does not support concurrent rendering, only the built-in API. |
@@ -9,3 +9,3 @@ /** @license React vundefined | ||
*/ | ||
'use strict';var e=require("react");function h(a,b){return a===b&&(0!==a||1/a===1/b)||a!==a&&b!==b}var k="function"===typeof Object.is?Object.is:h,l=e.useState,m=e.useEffect,n=e.useLayoutEffect,p=e.useDebugValue,q=e.useSyncExternalStore;function r(a,b){var d=b(),f=l({inst:{value:d,getSnapshot:b}}),c=f[0].inst,g=f[1];n(function(){c.value=d;c.getSnapshot=b;t(c)&&g({inst:c})},[a,d,b]);m(function(){t(c)&&g({inst:c});return a(function(){t(c)&&g({inst:c})})},[a]);p(d);return d} | ||
'use strict';var e=require("react");function h(a,b){return a===b&&(0!==a||1/a===1/b)||a!==a&&b!==b}var k="function"===typeof Object.is?Object.is:h,l=e.useState,m=e.useEffect,n=e.useLayoutEffect,p=e.useDebugValue,q=e.unstable_useSyncExternalStore;function r(a,b){var d=b(),f=l({inst:{value:d,getSnapshot:b}}),c=f[0].inst,g=f[1];n(function(){c.value=d;c.getSnapshot=b;t(c)&&g({inst:c})},[a,d,b]);m(function(){t(c)&&g({inst:c});return a(function(){t(c)&&g({inst:c})})},[a]);p(d);return d} | ||
function t(a){var b=a.getSnapshot;a=a.value;try{var d=b();return!k(a,d)}catch(f){return!0}}exports.useSyncExternalStore=void 0!==q?q:r; |
{ | ||
"name": "use-sync-external-store", | ||
"description": "Backwards compatible shim for React's useSyncExternalStore. Works with any React that supports hooks.", | ||
"version": "0.0.0-experimental-95d762e40-20210908", | ||
"version": "0.0.0-experimental-a8cabb564-20210915", | ||
"repository": { | ||
@@ -20,4 +20,4 @@ "type": "git", | ||
"peerDependencies": { | ||
"react": "0.0.0-experimental-95d762e40-20210908" | ||
"react": "0.0.0-experimental-a8cabb564-20210915" | ||
} | ||
} |
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
15362
305
0