source: node_modules/use-sync-external-store/cjs/use-sync-external-store-with-selector.development.js

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 5.5 KB
RevLine 
[d24f17c]1/**
2 * @license React
3 * use-sync-external-store-with-selector.development.js
4 *
5 * Copyright (c) Facebook, Inc. and its affiliates.
6 *
7 * This source code is licensed under the MIT license found in the
8 * LICENSE file in the root directory of this source tree.
9 */
10
11'use strict';
12
13if (process.env.NODE_ENV !== "production") {
14 (function() {
15
16 'use strict';
17
18/* global __REACT_DEVTOOLS_GLOBAL_HOOK__ */
19if (
20 typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ !== 'undefined' &&
21 typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart ===
22 'function'
23) {
24 __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart(new Error());
25}
26 var React = require('react');
27
28/**
29 * inlined Object.is polyfill to avoid requiring consumers ship their own
30 * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
31 */
32function is(x, y) {
33 return x === y && (x !== 0 || 1 / x === 1 / y) || x !== x && y !== y // eslint-disable-line no-self-compare
34 ;
35}
36
37var objectIs = typeof Object.is === 'function' ? Object.is : is;
38
39var useSyncExternalStore = React.useSyncExternalStore;
40
41// for CommonJS interop.
42
43var useRef = React.useRef,
44 useEffect = React.useEffect,
45 useMemo = React.useMemo,
46 useDebugValue = React.useDebugValue; // Same as useSyncExternalStore, but supports selector and isEqual arguments.
47
48function useSyncExternalStoreWithSelector(subscribe, getSnapshot, getServerSnapshot, selector, isEqual) {
49 // Use this to track the rendered snapshot.
50 var instRef = useRef(null);
51 var inst;
52
53 if (instRef.current === null) {
54 inst = {
55 hasValue: false,
56 value: null
57 };
58 instRef.current = inst;
59 } else {
60 inst = instRef.current;
61 }
62
63 var _useMemo = useMemo(function () {
64 // Track the memoized state using closure variables that are local to this
65 // memoized instance of a getSnapshot function. Intentionally not using a
66 // useRef hook, because that state would be shared across all concurrent
67 // copies of the hook/component.
68 var hasMemo = false;
69 var memoizedSnapshot;
70 var memoizedSelection;
71
72 var memoizedSelector = function (nextSnapshot) {
73 if (!hasMemo) {
74 // The first time the hook is called, there is no memoized result.
75 hasMemo = true;
76 memoizedSnapshot = nextSnapshot;
77
78 var _nextSelection = selector(nextSnapshot);
79
80 if (isEqual !== undefined) {
81 // Even if the selector has changed, the currently rendered selection
82 // may be equal to the new selection. We should attempt to reuse the
83 // current value if possible, to preserve downstream memoizations.
84 if (inst.hasValue) {
85 var currentSelection = inst.value;
86
87 if (isEqual(currentSelection, _nextSelection)) {
88 memoizedSelection = currentSelection;
89 return currentSelection;
90 }
91 }
92 }
93
94 memoizedSelection = _nextSelection;
95 return _nextSelection;
96 } // We may be able to reuse the previous invocation's result.
97
98
99 // We may be able to reuse the previous invocation's result.
100 var prevSnapshot = memoizedSnapshot;
101 var prevSelection = memoizedSelection;
102
103 if (objectIs(prevSnapshot, nextSnapshot)) {
104 // The snapshot is the same as last time. Reuse the previous selection.
105 return prevSelection;
106 } // The snapshot has changed, so we need to compute a new selection.
107
108
109 // The snapshot has changed, so we need to compute a new selection.
110 var nextSelection = selector(nextSnapshot); // If a custom isEqual function is provided, use that to check if the data
111 // has changed. If it hasn't, return the previous selection. That signals
112 // to React that the selections are conceptually equal, and we can bail
113 // out of rendering.
114
115 // If a custom isEqual function is provided, use that to check if the data
116 // has changed. If it hasn't, return the previous selection. That signals
117 // to React that the selections are conceptually equal, and we can bail
118 // out of rendering.
119 if (isEqual !== undefined && isEqual(prevSelection, nextSelection)) {
120 return prevSelection;
121 }
122
123 memoizedSnapshot = nextSnapshot;
124 memoizedSelection = nextSelection;
125 return nextSelection;
126 }; // Assigning this to a constant so that Flow knows it can't change.
127
128
129 // Assigning this to a constant so that Flow knows it can't change.
130 var maybeGetServerSnapshot = getServerSnapshot === undefined ? null : getServerSnapshot;
131
132 var getSnapshotWithSelector = function () {
133 return memoizedSelector(getSnapshot());
134 };
135
136 var getServerSnapshotWithSelector = maybeGetServerSnapshot === null ? undefined : function () {
137 return memoizedSelector(maybeGetServerSnapshot());
138 };
139 return [getSnapshotWithSelector, getServerSnapshotWithSelector];
140 }, [getSnapshot, getServerSnapshot, selector, isEqual]),
141 getSelection = _useMemo[0],
142 getServerSelection = _useMemo[1];
143
144 var value = useSyncExternalStore(subscribe, getSelection, getServerSelection);
145 useEffect(function () {
146 inst.hasValue = true;
147 inst.value = value;
148 }, [value]);
149 useDebugValue(value);
150 return value;
151}
152
153exports.useSyncExternalStoreWithSelector = useSyncExternalStoreWithSelector;
154 /* global __REACT_DEVTOOLS_GLOBAL_HOOK__ */
155if (
156 typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ !== 'undefined' &&
157 typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop ===
158 'function'
159) {
160 __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop(new Error());
161}
162
163 })();
164}
Note: See TracBrowser for help on using the repository browser.