@wordpress/data 6.1.4 → 6.2.1
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.
- package/CHANGELOG.md +18 -0
- package/LICENSE.md +1 -1
- package/README.md +77 -71
- package/build/components/use-dispatch/use-dispatch.js +8 -8
- package/build/components/use-dispatch/use-dispatch.js.map +1 -1
- package/build/components/use-select/index.js +58 -43
- package/build/components/use-select/index.js.map +1 -1
- package/build/controls.js +16 -16
- package/build/controls.js.map +1 -1
- package/build/index.js +14 -14
- package/build/index.js.map +1 -1
- package/build/plugins/index.js +0 -8
- package/build/plugins/index.js.map +1 -1
- package/build/redux-store/index.js +15 -15
- package/build/redux-store/index.js.map +1 -1
- package/build/registry.js +82 -68
- package/build/registry.js.map +1 -1
- package/build/resolvers-cache-middleware.js +5 -3
- package/build/resolvers-cache-middleware.js.map +1 -1
- package/build/store/index.js +42 -43
- package/build/store/index.js.map +1 -1
- package/build/utils/emitter.js +1 -1
- package/build/utils/emitter.js.map +1 -1
- package/build-module/components/use-dispatch/use-dispatch.js +8 -8
- package/build-module/components/use-dispatch/use-dispatch.js.map +1 -1
- package/build-module/components/use-select/index.js +60 -44
- package/build-module/components/use-select/index.js.map +1 -1
- package/build-module/controls.js +16 -16
- package/build-module/controls.js.map +1 -1
- package/build-module/index.js +14 -14
- package/build-module/index.js.map +1 -1
- package/build-module/plugins/index.js +0 -1
- package/build-module/plugins/index.js.map +1 -1
- package/build-module/redux-store/index.js +15 -15
- package/build-module/redux-store/index.js.map +1 -1
- package/build-module/registry.js +82 -68
- package/build-module/registry.js.map +1 -1
- package/build-module/resolvers-cache-middleware.js +3 -3
- package/build-module/resolvers-cache-middleware.js.map +1 -1
- package/build-module/store/index.js +42 -42
- package/build-module/store/index.js.map +1 -1
- package/build-module/utils/emitter.js +1 -1
- package/build-module/utils/emitter.js.map +1 -1
- package/build-types/types.d.ts +29 -16
- package/build-types/types.d.ts.map +1 -1
- package/build-types/utils/emitter.d.ts +2 -2
- package/build-types/utils/emitter.d.ts.map +1 -1
- package/package.json +10 -10
- package/src/components/use-dispatch/test/use-dispatch.js +1 -1
- package/src/components/use-dispatch/use-dispatch.js +9 -9
- package/src/components/use-select/index.js +61 -46
- package/src/components/use-select/test/index.js +32 -32
- package/src/controls.js +22 -22
- package/src/index.js +14 -14
- package/src/plugins/index.js +0 -1
- package/src/redux-store/index.js +15 -15
- package/src/registry.js +88 -75
- package/src/resolvers-cache-middleware.js +3 -3
- package/src/store/index.js +49 -48
- package/src/test/registry.js +33 -47
- package/src/types.ts +44 -16
- package/src/utils/emitter.js +1 -1
- package/tsconfig.tsbuildinfo +1 -1
- package/build/plugins/controls/index.js +0 -24
- package/build/plugins/controls/index.js.map +0 -1
- package/build/store/name.js +0 -15
- package/build/store/name.js.map +0 -1
- package/build-module/plugins/controls/index.js +0 -12
- package/build-module/plugins/controls/index.js.map +0 -1
- package/build-module/store/name.js +0 -7
- package/build-module/store/name.js.map +0 -1
- package/src/plugins/controls/index.js +0 -12
- package/src/store/name.js +0 -6
|
@@ -7,7 +7,7 @@ import { useMemoOne } from 'use-memo-one';
|
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
import { createQueue } from '@wordpress/priority-queue';
|
|
10
|
-
import { useRef, useCallback,
|
|
10
|
+
import { useRef, useCallback, useMemo, useReducer } from '@wordpress/element';
|
|
11
11
|
import isShallowEqual from '@wordpress/is-shallow-equal';
|
|
12
12
|
import { useIsomorphicLayoutEffect } from '@wordpress/compose';
|
|
13
13
|
/**
|
|
@@ -16,8 +16,11 @@ import { useIsomorphicLayoutEffect } from '@wordpress/compose';
|
|
|
16
16
|
|
|
17
17
|
import useRegistry from '../registry-provider/use-registry';
|
|
18
18
|
import useAsyncMode from '../async-mode-provider/use-async-mode';
|
|
19
|
+
|
|
20
|
+
const noop = () => {};
|
|
21
|
+
|
|
19
22
|
const renderQueue = createQueue();
|
|
20
|
-
/** @typedef {import('
|
|
23
|
+
/** @typedef {import('../../types').StoreDescriptor} StoreDescriptor */
|
|
21
24
|
|
|
22
25
|
/**
|
|
23
26
|
* Custom react hook for retrieving props from registered selectors.
|
|
@@ -25,20 +28,20 @@ const renderQueue = createQueue();
|
|
|
25
28
|
* In general, this custom React hook follows the
|
|
26
29
|
* [rules of hooks](https://reactjs.org/docs/hooks-rules.html).
|
|
27
30
|
*
|
|
28
|
-
* @param {Function|
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
* @param {Array}
|
|
40
|
-
*
|
|
41
|
-
*
|
|
31
|
+
* @param {Function|StoreDescriptor|string} mapSelect Function called on every state change. The
|
|
32
|
+
* returned value is exposed to the component
|
|
33
|
+
* implementing this hook. The function receives
|
|
34
|
+
* the `registry.select` method on the first
|
|
35
|
+
* argument and the `registry` on the second
|
|
36
|
+
* argument.
|
|
37
|
+
* When a store key is passed, all selectors for
|
|
38
|
+
* the store will be returned. This is only meant
|
|
39
|
+
* for usage of these selectors in event
|
|
40
|
+
* callbacks, not for data needed to create the
|
|
41
|
+
* element tree.
|
|
42
|
+
* @param {Array} deps If provided, this memoizes the mapSelect so the
|
|
43
|
+
* same `mapSelect` is invoked on every state
|
|
44
|
+
* change unless the dependencies change.
|
|
42
45
|
*
|
|
43
46
|
* @example
|
|
44
47
|
* ```js
|
|
@@ -87,14 +90,24 @@ const renderQueue = createQueue();
|
|
|
87
90
|
* @return {Function} A custom react hook.
|
|
88
91
|
*/
|
|
89
92
|
|
|
90
|
-
export default function useSelect(
|
|
91
|
-
const
|
|
93
|
+
export default function useSelect(mapSelect, deps) {
|
|
94
|
+
const hasMappingFunction = 'function' === typeof mapSelect; // If we're recalling a store by its name or by
|
|
95
|
+
// its descriptor then we won't be caching the
|
|
96
|
+
// calls to `mapSelect` because we won't be calling it.
|
|
92
97
|
|
|
93
|
-
if (
|
|
98
|
+
if (!hasMappingFunction) {
|
|
94
99
|
deps = [];
|
|
95
|
-
}
|
|
100
|
+
} // Because of the "rule of hooks" we have to call `useCallback`
|
|
101
|
+
// on every invocation whether or not we have a real function
|
|
102
|
+
// for `mapSelect`. we'll create this intermediate variable to
|
|
103
|
+
// fulfill that need and then reference it with our "real"
|
|
104
|
+
// `_mapSelect` if we can.
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
const callbackMapper = useCallback(hasMappingFunction ? mapSelect : noop, deps);
|
|
108
|
+
|
|
109
|
+
const _mapSelect = hasMappingFunction ? callbackMapper : null;
|
|
96
110
|
|
|
97
|
-
const mapSelect = useCallback(_mapSelect, deps);
|
|
98
111
|
const registry = useRegistry();
|
|
99
112
|
const isAsync = useAsyncMode(); // React can sometimes clear the `useMemo` cache.
|
|
100
113
|
// We use the cache-stable `useMemoOne` to avoid
|
|
@@ -108,7 +121,7 @@ export default function useSelect(_mapSelect, deps) {
|
|
|
108
121
|
const latestIsAsync = useRef(isAsync);
|
|
109
122
|
const latestMapOutput = useRef();
|
|
110
123
|
const latestMapOutputError = useRef();
|
|
111
|
-
const isMountedAndNotUnsubscribing = useRef(); // Keep track of the stores being selected in the
|
|
124
|
+
const isMountedAndNotUnsubscribing = useRef(); // Keep track of the stores being selected in the _mapSelect function,
|
|
112
125
|
// and only subscribe to those stores later.
|
|
113
126
|
|
|
114
127
|
const listeningStores = useRef([]);
|
|
@@ -119,34 +132,35 @@ export default function useSelect(_mapSelect, deps) {
|
|
|
119
132
|
const depsChangedFlag = useMemo(() => ({}), deps || []);
|
|
120
133
|
let mapOutput;
|
|
121
134
|
|
|
122
|
-
if (
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
135
|
+
if (_mapSelect) {
|
|
136
|
+
mapOutput = latestMapOutput.current;
|
|
137
|
+
const hasReplacedMapSelect = latestMapSelect.current !== _mapSelect;
|
|
138
|
+
const lastMapSelectFailed = !!latestMapOutputError.current;
|
|
139
|
+
|
|
140
|
+
if (hasReplacedMapSelect || lastMapSelectFailed) {
|
|
141
|
+
try {
|
|
142
|
+
mapOutput = trapSelect(() => _mapSelect(registry.select, registry));
|
|
143
|
+
} catch (error) {
|
|
144
|
+
let errorMessage = `An error occurred while running 'mapSelect': ${error.message}`;
|
|
131
145
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
146
|
+
if (latestMapOutputError.current) {
|
|
147
|
+
errorMessage += `\nThe error may be correlated with this previous error:\n`;
|
|
148
|
+
errorMessage += `${latestMapOutputError.current.stack}\n\n`;
|
|
149
|
+
errorMessage += 'Original stack trace:';
|
|
150
|
+
} // eslint-disable-next-line no-console
|
|
137
151
|
|
|
138
152
|
|
|
139
|
-
|
|
140
|
-
|
|
153
|
+
console.error(errorMessage);
|
|
154
|
+
}
|
|
141
155
|
}
|
|
142
156
|
}
|
|
143
157
|
|
|
144
158
|
useIsomorphicLayoutEffect(() => {
|
|
145
|
-
if (
|
|
159
|
+
if (!hasMappingFunction) {
|
|
146
160
|
return;
|
|
147
161
|
}
|
|
148
162
|
|
|
149
|
-
latestMapSelect.current =
|
|
163
|
+
latestMapSelect.current = _mapSelect;
|
|
150
164
|
latestMapOutput.current = mapOutput;
|
|
151
165
|
latestMapOutputError.current = undefined;
|
|
152
166
|
isMountedAndNotUnsubscribing.current = true; // This has to run after the other ref updates
|
|
@@ -160,7 +174,7 @@ export default function useSelect(_mapSelect, deps) {
|
|
|
160
174
|
}
|
|
161
175
|
});
|
|
162
176
|
useIsomorphicLayoutEffect(() => {
|
|
163
|
-
if (
|
|
177
|
+
if (!hasMappingFunction) {
|
|
164
178
|
return;
|
|
165
179
|
}
|
|
166
180
|
|
|
@@ -204,8 +218,10 @@ export default function useSelect(_mapSelect, deps) {
|
|
|
204
218
|
|
|
205
219
|
unsubscribers.forEach(unsubscribe => unsubscribe === null || unsubscribe === void 0 ? void 0 : unsubscribe());
|
|
206
220
|
renderQueue.flush(queueContext);
|
|
207
|
-
};
|
|
208
|
-
|
|
209
|
-
|
|
221
|
+
}; // If you're tempted to eliminate the spread dependencies below don't do it!
|
|
222
|
+
// We're passing these in from the calling function and want to make sure we're
|
|
223
|
+
// examining every individual value inside the `deps` array.
|
|
224
|
+
}, [registry, trapSelect, hasMappingFunction, depsChangedFlag]);
|
|
225
|
+
return hasMappingFunction ? mapOutput : registry.select(mapSelect);
|
|
210
226
|
}
|
|
211
227
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/data/src/components/use-select/index.js"],"names":["useMemoOne","createQueue","useRef","useCallback","useReducer","useMemo","isShallowEqual","useIsomorphicLayoutEffect","useRegistry","useAsyncMode","renderQueue","useSelect","_mapSelect","deps","isWithoutMapping","mapSelect","registry","isAsync","queueContext","queue","forceRender","s","latestMapSelect","latestIsAsync","latestMapOutput","latestMapOutputError","isMountedAndNotUnsubscribing","listeningStores","trapSelect","callback","__experimentalMarkListeningStores","depsChangedFlag","mapOutput","current","select","error","errorMessage","message","stack","console","undefined","flush","onStoreChange","newMapOutput","add","onChange","unsubscribers","map","storeName","__experimentalSubscribeStore","forEach","unsubscribe"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,UAAT,QAA2B,cAA3B;AAEA;AACA;AACA;;AACA,SAASC,WAAT,QAA4B,2BAA5B;AACA,SAASC,MAAT,EAAiBC,WAAjB,EAA8BC,UAA9B,EAA0CC,OAA1C,QAAyD,oBAAzD;AACA,OAAOC,cAAP,MAA2B,6BAA3B;AACA,SAASC,yBAAT,QAA0C,oBAA1C;AAEA;AACA;AACA;;AACA,OAAOC,WAAP,MAAwB,mCAAxB;AACA,OAAOC,YAAP,MAAyB,uCAAzB;AAEA,MAAMC,WAAW,GAAGT,WAAW,EAA/B;AAEA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;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,CAAtB;AAIA,WAAO,MAAM;AACZnB,MAAAA,4BAA4B,CAACO,OAA7B,GAAuC,KAAvC,CADY,CAEZ;;AACAa,MAAAA,aAAa,CAACI,OAAd,CAAyBC,WAAF,IAAmBA,WAAnB,aAAmBA,WAAnB,uBAAmBA,WAAW,EAArD;AACAzC,MAAAA,WAAW,CAAC+B,KAAZ,CAAmBvB,YAAnB;AACA,KALD;AAMA,GAnDwB,EAmDtB,CAAEF,QAAF,EAAYY,UAAZ,EAAwBG,eAAxB,EAAyCjB,gBAAzC,CAnDsB,CAAzB;AAqDA,SAAOA,gBAAgB,GAAGE,QAAQ,CAACkB,MAAT,CAAiBtB,UAAjB,CAAH,GAAmCoB,SAA1D;AACA","sourcesContent":["/**\n * External dependencies\n */\nimport { useMemoOne } from 'use-memo-one';\n\n/**\n * WordPress dependencies\n */\nimport { createQueue } from '@wordpress/priority-queue';\nimport { useRef, useCallback, useReducer, useMemo } from '@wordpress/element';\nimport isShallowEqual from '@wordpress/is-shallow-equal';\nimport { useIsomorphicLayoutEffect } from '@wordpress/compose';\n\n/**\n * Internal dependencies\n */\nimport useRegistry from '../registry-provider/use-registry';\nimport useAsyncMode from '../async-mode-provider/use-async-mode';\n\nconst renderQueue = createQueue();\n\n/** @typedef {import('./types').WPDataStore} WPDataStore */\n\n/**\n * Custom react hook for retrieving props from registered selectors.\n *\n * In general, this custom React hook follows the\n * [rules of hooks](https://reactjs.org/docs/hooks-rules.html).\n *\n * @param {Function|WPDataStore|string} _mapSelect Function called on every state change. The\n * returned value is exposed to the component\n * implementing this hook. The function receives\n * the `registry.select` method on the first\n * argument and the `registry` on the second\n * argument.\n * When a store key is passed, all selectors for\n * the store will be returned. This is only meant\n * for usage of these selectors in event\n * callbacks, not for data needed to create the\n * element tree.\n * @param {Array} deps If provided, this memoizes the mapSelect so the\n * same `mapSelect` is invoked on every state\n * change unless the dependencies change.\n *\n * @example\n * ```js\n * import { useSelect } from '@wordpress/data';\n *\n * function HammerPriceDisplay( { currency } ) {\n * const price = useSelect( ( select ) => {\n * return select( 'my-shop' ).getPrice( 'hammer', currency )\n * }, [ currency ] );\n * return new Intl.NumberFormat( 'en-US', {\n * style: 'currency',\n * currency,\n * } ).format( price );\n * }\n *\n * // Rendered in the application:\n * // <HammerPriceDisplay currency=\"USD\" />\n * ```\n *\n * In the above example, when `HammerPriceDisplay` is rendered into an\n * application, the price will be retrieved from the store state using the\n * `mapSelect` callback on `useSelect`. If the currency prop changes then\n * any price in the state for that currency is retrieved. If the currency prop\n * doesn't change and other props are passed in that do change, the price will\n * not change because the dependency is just the currency.\n *\n * When data is only used in an event callback, the data should not be retrieved\n * on render, so it may be useful to get the selectors function instead.\n *\n * **Don't use `useSelect` this way when calling the selectors in the render\n * function because your component won't re-render on a data change.**\n *\n * ```js\n * import { useSelect } from '@wordpress/data';\n *\n * function Paste( { children } ) {\n * const { getSettings } = useSelect( 'my-shop' );\n * function onPaste() {\n * // Do something with the settings.\n * const settings = getSettings();\n * }\n * return <div onPaste={ onPaste }>{ children }</div>;\n * }\n * ```\n *\n * @return {Function} A custom react hook.\n */\nexport default function useSelect( _mapSelect, deps ) {\n\tconst isWithoutMapping = typeof _mapSelect !== 'function';\n\n\tif ( isWithoutMapping ) {\n\t\tdeps = [];\n\t}\n\n\tconst mapSelect = useCallback( _mapSelect, deps );\n\tconst registry = useRegistry();\n\tconst isAsync = useAsyncMode();\n\t// React can sometimes clear the `useMemo` cache.\n\t// We use the cache-stable `useMemoOne` to avoid\n\t// losing queues.\n\tconst queueContext = useMemoOne( () => ( { queue: true } ), [ registry ] );\n\tconst [ , forceRender ] = useReducer( ( s ) => s + 1, 0 );\n\n\tconst latestMapSelect = useRef();\n\tconst latestIsAsync = useRef( isAsync );\n\tconst latestMapOutput = useRef();\n\tconst latestMapOutputError = useRef();\n\tconst isMountedAndNotUnsubscribing = useRef();\n\n\t// Keep track of the stores being selected in the mapSelect function,\n\t// and only subscribe to those stores later.\n\tconst listeningStores = useRef( [] );\n\tconst trapSelect = useCallback(\n\t\t( callback ) =>\n\t\t\tregistry.__experimentalMarkListeningStores(\n\t\t\t\tcallback,\n\t\t\t\tlisteningStores\n\t\t\t),\n\t\t[ registry ]\n\t);\n\n\t// Generate a \"flag\" for used in the effect dependency array.\n\t// It's different than just using `mapSelect` since deps could be undefined,\n\t// in that case, we would still want to memoize it.\n\tconst depsChangedFlag = useMemo( () => ( {} ), deps || [] );\n\n\tlet mapOutput;\n\n\tif ( ! isWithoutMapping ) {\n\t\ttry {\n\t\t\tif (\n\t\t\t\tlatestMapSelect.current !== mapSelect ||\n\t\t\t\tlatestMapOutputError.current\n\t\t\t) {\n\t\t\t\tmapOutput = trapSelect( () =>\n\t\t\t\t\tmapSelect( registry.select, registry )\n\t\t\t\t);\n\t\t\t} else {\n\t\t\t\tmapOutput = latestMapOutput.current;\n\t\t\t}\n\t\t} catch ( error ) {\n\t\t\tlet errorMessage = `An error occurred while running 'mapSelect': ${ error.message }`;\n\n\t\t\tif ( latestMapOutputError.current ) {\n\t\t\t\terrorMessage += `\\nThe error may be correlated with this previous error:\\n`;\n\t\t\t\terrorMessage += `${ latestMapOutputError.current.stack }\\n\\n`;\n\t\t\t\terrorMessage += 'Original stack trace:';\n\t\t\t}\n\n\t\t\t// eslint-disable-next-line no-console\n\t\t\tconsole.error( errorMessage );\n\t\t\tmapOutput = latestMapOutput.current;\n\t\t}\n\t}\n\n\tuseIsomorphicLayoutEffect( () => {\n\t\tif ( isWithoutMapping ) {\n\t\t\treturn;\n\t\t}\n\n\t\tlatestMapSelect.current = mapSelect;\n\t\tlatestMapOutput.current = mapOutput;\n\t\tlatestMapOutputError.current = undefined;\n\t\tisMountedAndNotUnsubscribing.current = true;\n\n\t\t// This has to run after the other ref updates\n\t\t// to avoid using stale values in the flushed\n\t\t// callbacks or potentially overwriting a\n\t\t// changed `latestMapOutput.current`.\n\t\tif ( latestIsAsync.current !== isAsync ) {\n\t\t\tlatestIsAsync.current = isAsync;\n\t\t\trenderQueue.flush( queueContext );\n\t\t}\n\t} );\n\n\tuseIsomorphicLayoutEffect( () => {\n\t\tif ( isWithoutMapping ) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst onStoreChange = () => {\n\t\t\tif ( isMountedAndNotUnsubscribing.current ) {\n\t\t\t\ttry {\n\t\t\t\t\tconst newMapOutput = trapSelect( () =>\n\t\t\t\t\t\tlatestMapSelect.current( registry.select, registry )\n\t\t\t\t\t);\n\n\t\t\t\t\tif (\n\t\t\t\t\t\tisShallowEqual( latestMapOutput.current, newMapOutput )\n\t\t\t\t\t) {\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\t\t\t\t\tlatestMapOutput.current = newMapOutput;\n\t\t\t\t} catch ( error ) {\n\t\t\t\t\tlatestMapOutputError.current = error;\n\t\t\t\t}\n\t\t\t\tforceRender();\n\t\t\t}\n\t\t};\n\n\t\t// catch any possible state changes during mount before the subscription\n\t\t// could be set.\n\t\tif ( latestIsAsync.current ) {\n\t\t\trenderQueue.add( queueContext, onStoreChange );\n\t\t} else {\n\t\t\tonStoreChange();\n\t\t}\n\n\t\tconst onChange = () => {\n\t\t\tif ( latestIsAsync.current ) {\n\t\t\t\trenderQueue.add( queueContext, onStoreChange );\n\t\t\t} else {\n\t\t\t\tonStoreChange();\n\t\t\t}\n\t\t};\n\n\t\tconst unsubscribers = listeningStores.current.map( ( storeName ) =>\n\t\t\tregistry.__experimentalSubscribeStore( storeName, onChange )\n\t\t);\n\n\t\treturn () => {\n\t\t\tisMountedAndNotUnsubscribing.current = false;\n\t\t\t// The return value of the subscribe function could be undefined if the store is a custom generic store.\n\t\t\tunsubscribers.forEach( ( unsubscribe ) => unsubscribe?.() );\n\t\t\trenderQueue.flush( queueContext );\n\t\t};\n\t}, [ registry, trapSelect, depsChangedFlag, isWithoutMapping ] );\n\n\treturn isWithoutMapping ? registry.select( _mapSelect ) : mapOutput;\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["@wordpress/data/src/components/use-select/index.js"],"names":["useMemoOne","createQueue","useRef","useCallback","useMemo","useReducer","isShallowEqual","useIsomorphicLayoutEffect","useRegistry","useAsyncMode","noop","renderQueue","useSelect","mapSelect","deps","hasMappingFunction","callbackMapper","_mapSelect","registry","isAsync","queueContext","queue","forceRender","s","latestMapSelect","latestIsAsync","latestMapOutput","latestMapOutputError","isMountedAndNotUnsubscribing","listeningStores","trapSelect","callback","__experimentalMarkListeningStores","depsChangedFlag","mapOutput","current","hasReplacedMapSelect","lastMapSelectFailed","select","error","errorMessage","message","stack","console","undefined","flush","onStoreChange","newMapOutput","add","onChange","unsubscribers","map","storeName","__experimentalSubscribeStore","forEach","unsubscribe"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,UAAT,QAA2B,cAA3B;AAEA;AACA;AACA;;AACA,SAASC,WAAT,QAA4B,2BAA5B;AACA,SAASC,MAAT,EAAiBC,WAAjB,EAA8BC,OAA9B,EAAuCC,UAAvC,QAAyD,oBAAzD;AACA,OAAOC,cAAP,MAA2B,6BAA3B;AACA,SAASC,yBAAT,QAA0C,oBAA1C;AAEA;AACA;AACA;;AACA,OAAOC,WAAP,MAAwB,mCAAxB;AACA,OAAOC,YAAP,MAAyB,uCAAzB;;AAEA,MAAMC,IAAI,GAAG,MAAM,CAAE,CAArB;;AACA,MAAMC,WAAW,GAAGV,WAAW,EAA/B;AAEA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,eAAe,SAASW,SAAT,CAAoBC,SAApB,EAA+BC,IAA/B,EAAsC;AACpD,QAAMC,kBAAkB,GAAG,e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,WAAnB,uBAAmBA,WAAW,EAArD;AACA5C,MAAAA,WAAW,CAACkC,KAAZ,CAAmBzB,YAAnB;AACA,KALD,CA7CgC,CAmDhC;AACA;AACA;AACA,GAtDwB,EAsDtB,CAAEF,QAAF,EAAYY,UAAZ,EAAwBf,kBAAxB,EAA4CkB,eAA5C,CAtDsB,CAAzB;AAwDA,SAAOlB,kBAAkB,GAAGmB,SAAH,GAAehB,QAAQ,CAACoB,MAAT,CAAiBzB,SAAjB,CAAxC;AACA","sourcesContent":["/**\n * External dependencies\n */\nimport { useMemoOne } from 'use-memo-one';\n\n/**\n * WordPress dependencies\n */\nimport { createQueue } from '@wordpress/priority-queue';\nimport { useRef, useCallback, useMemo, useReducer } from '@wordpress/element';\nimport isShallowEqual from '@wordpress/is-shallow-equal';\nimport { useIsomorphicLayoutEffect } from '@wordpress/compose';\n\n/**\n * Internal dependencies\n */\nimport useRegistry from '../registry-provider/use-registry';\nimport useAsyncMode from '../async-mode-provider/use-async-mode';\n\nconst noop = () => {};\nconst renderQueue = createQueue();\n\n/** @typedef {import('../../types').StoreDescriptor} StoreDescriptor */\n\n/**\n * Custom react hook for retrieving props from registered selectors.\n *\n * In general, this custom React hook follows the\n * [rules of hooks](https://reactjs.org/docs/hooks-rules.html).\n *\n * @param {Function|StoreDescriptor|string} mapSelect Function called on every state change. The\n * returned value is exposed to the component\n * implementing this hook. The function receives\n * the `registry.select` method on the first\n * argument and the `registry` on the second\n * argument.\n * When a store key is passed, all selectors for\n * the store will be returned. This is only meant\n * for usage of these selectors in event\n * callbacks, not for data needed to create the\n * element tree.\n * @param {Array} deps If provided, this memoizes the mapSelect so the\n * same `mapSelect` is invoked on every state\n * change unless the dependencies change.\n *\n * @example\n * ```js\n * import { useSelect } from '@wordpress/data';\n *\n * function HammerPriceDisplay( { currency } ) {\n * const price = useSelect( ( select ) => {\n * return select( 'my-shop' ).getPrice( 'hammer', currency )\n * }, [ currency ] );\n * return new Intl.NumberFormat( 'en-US', {\n * style: 'currency',\n * currency,\n * } ).format( price );\n * }\n *\n * // Rendered in the application:\n * // <HammerPriceDisplay currency=\"USD\" />\n * ```\n *\n * In the above example, when `HammerPriceDisplay` is rendered into an\n * application, the price will be retrieved from the store state using the\n * `mapSelect` callback on `useSelect`. If the currency prop changes then\n * any price in the state for that currency is retrieved. If the currency prop\n * doesn't change and other props are passed in that do change, the price will\n * not change because the dependency is just the currency.\n *\n * When data is only used in an event callback, the data should not be retrieved\n * on render, so it may be useful to get the selectors function instead.\n *\n * **Don't use `useSelect` this way when calling the selectors in the render\n * function because your component won't re-render on a data change.**\n *\n * ```js\n * import { useSelect } from '@wordpress/data';\n *\n * function Paste( { children } ) {\n * const { getSettings } = useSelect( 'my-shop' );\n * function onPaste() {\n * // Do something with the settings.\n * const settings = getSettings();\n * }\n * return <div onPaste={ onPaste }>{ children }</div>;\n * }\n * ```\n *\n * @return {Function} A custom react hook.\n */\nexport default function useSelect( mapSelect, deps ) {\n\tconst hasMappingFunction = 'function' === typeof mapSelect;\n\n\t// If we're recalling a store by its name or by\n\t// its descriptor then we won't be caching the\n\t// calls to `mapSelect` because we won't be calling it.\n\tif ( ! hasMappingFunction ) {\n\t\tdeps = [];\n\t}\n\n\t// Because of the \"rule of hooks\" we have to call `useCallback`\n\t// on every invocation whether or not we have a real function\n\t// for `mapSelect`. we'll create this intermediate variable to\n\t// fulfill that need and then reference it with our \"real\"\n\t// `_mapSelect` if we can.\n\tconst callbackMapper = useCallback(\n\t\thasMappingFunction ? mapSelect : noop,\n\t\tdeps\n\t);\n\tconst _mapSelect = hasMappingFunction ? callbackMapper : null;\n\n\tconst registry = useRegistry();\n\tconst isAsync = useAsyncMode();\n\t// React can sometimes clear the `useMemo` cache.\n\t// We use the cache-stable `useMemoOne` to avoid\n\t// losing queues.\n\tconst queueContext = useMemoOne( () => ( { queue: true } ), [ registry ] );\n\tconst [ , forceRender ] = useReducer( ( s ) => s + 1, 0 );\n\n\tconst latestMapSelect = useRef();\n\tconst latestIsAsync = useRef( isAsync );\n\tconst latestMapOutput = useRef();\n\tconst latestMapOutputError = useRef();\n\tconst isMountedAndNotUnsubscribing = useRef();\n\n\t// Keep track of the stores being selected in the _mapSelect function,\n\t// and only subscribe to those stores later.\n\tconst listeningStores = useRef( [] );\n\tconst trapSelect = useCallback(\n\t\t( callback ) =>\n\t\t\tregistry.__experimentalMarkListeningStores(\n\t\t\t\tcallback,\n\t\t\t\tlisteningStores\n\t\t\t),\n\t\t[ registry ]\n\t);\n\n\t// Generate a \"flag\" for used in the effect dependency array.\n\t// It's different than just using `mapSelect` since deps could be undefined,\n\t// in that case, we would still want to memoize it.\n\tconst depsChangedFlag = useMemo( () => ( {} ), deps || [] );\n\n\tlet mapOutput;\n\n\tif ( _mapSelect ) {\n\t\tmapOutput = latestMapOutput.current;\n\t\tconst hasReplacedMapSelect = latestMapSelect.current !== _mapSelect;\n\t\tconst lastMapSelectFailed = !! latestMapOutputError.current;\n\n\t\tif ( hasReplacedMapSelect || lastMapSelectFailed ) {\n\t\t\ttry {\n\t\t\t\tmapOutput = trapSelect( () =>\n\t\t\t\t\t_mapSelect( registry.select, registry )\n\t\t\t\t);\n\t\t\t} catch ( error ) {\n\t\t\t\tlet errorMessage = `An error occurred while running 'mapSelect': ${ error.message }`;\n\n\t\t\t\tif ( latestMapOutputError.current ) {\n\t\t\t\t\terrorMessage += `\\nThe error may be correlated with this previous error:\\n`;\n\t\t\t\t\terrorMessage += `${ latestMapOutputError.current.stack }\\n\\n`;\n\t\t\t\t\terrorMessage += 'Original stack trace:';\n\t\t\t\t}\n\n\t\t\t\t// eslint-disable-next-line no-console\n\t\t\t\tconsole.error( errorMessage );\n\t\t\t}\n\t\t}\n\t}\n\n\tuseIsomorphicLayoutEffect( () => {\n\t\tif ( ! hasMappingFunction ) {\n\t\t\treturn;\n\t\t}\n\n\t\tlatestMapSelect.current = _mapSelect;\n\t\tlatestMapOutput.current = mapOutput;\n\t\tlatestMapOutputError.current = undefined;\n\t\tisMountedAndNotUnsubscribing.current = true;\n\n\t\t// This has to run after the other ref updates\n\t\t// to avoid using stale values in the flushed\n\t\t// callbacks or potentially overwriting a\n\t\t// changed `latestMapOutput.current`.\n\t\tif ( latestIsAsync.current !== isAsync ) {\n\t\t\tlatestIsAsync.current = isAsync;\n\t\t\trenderQueue.flush( queueContext );\n\t\t}\n\t} );\n\n\tuseIsomorphicLayoutEffect( () => {\n\t\tif ( ! hasMappingFunction ) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst onStoreChange = () => {\n\t\t\tif ( isMountedAndNotUnsubscribing.current ) {\n\t\t\t\ttry {\n\t\t\t\t\tconst newMapOutput = trapSelect( () =>\n\t\t\t\t\t\tlatestMapSelect.current( registry.select, registry )\n\t\t\t\t\t);\n\n\t\t\t\t\tif (\n\t\t\t\t\t\tisShallowEqual( latestMapOutput.current, newMapOutput )\n\t\t\t\t\t) {\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\t\t\t\t\tlatestMapOutput.current = newMapOutput;\n\t\t\t\t} catch ( error ) {\n\t\t\t\t\tlatestMapOutputError.current = error;\n\t\t\t\t}\n\t\t\t\tforceRender();\n\t\t\t}\n\t\t};\n\n\t\t// catch any possible state changes during mount before the subscription\n\t\t// could be set.\n\t\tif ( latestIsAsync.current ) {\n\t\t\trenderQueue.add( queueContext, onStoreChange );\n\t\t} else {\n\t\t\tonStoreChange();\n\t\t}\n\n\t\tconst onChange = () => {\n\t\t\tif ( latestIsAsync.current ) {\n\t\t\t\trenderQueue.add( queueContext, onStoreChange );\n\t\t\t} else {\n\t\t\t\tonStoreChange();\n\t\t\t}\n\t\t};\n\n\t\tconst unsubscribers = listeningStores.current.map( ( storeName ) =>\n\t\t\tregistry.__experimentalSubscribeStore( storeName, onChange )\n\t\t);\n\n\t\treturn () => {\n\t\t\tisMountedAndNotUnsubscribing.current = false;\n\t\t\t// The return value of the subscribe function could be undefined if the store is a custom generic store.\n\t\t\tunsubscribers.forEach( ( unsubscribe ) => unsubscribe?.() );\n\t\t\trenderQueue.flush( queueContext );\n\t\t};\n\t\t// If you're tempted to eliminate the spread dependencies below don't do it!\n\t\t// We're passing these in from the calling function and want to make sure we're\n\t\t// examining every individual value inside the `deps` array.\n\t}, [ registry, trapSelect, hasMappingFunction, depsChangedFlag ] );\n\n\treturn hasMappingFunction ? mapOutput : registry.select( mapSelect );\n}\n"]}
|
package/build-module/controls.js
CHANGED
|
@@ -7,7 +7,7 @@ import { isObject } from 'lodash';
|
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
import { createRegistryControl } from './factory';
|
|
10
|
-
/** @typedef {import('./types').
|
|
10
|
+
/** @typedef {import('./types').StoreDescriptor} StoreDescriptor */
|
|
11
11
|
|
|
12
12
|
const SELECT = '@@data/SELECT';
|
|
13
13
|
const RESOLVE_SELECT = '@@data/RESOLVE_SELECT';
|
|
@@ -18,9 +18,9 @@ const DISPATCH = '@@data/DISPATCH';
|
|
|
18
18
|
* Note: This control synchronously returns the current selector value, triggering the
|
|
19
19
|
* resolution, but not waiting for it.
|
|
20
20
|
*
|
|
21
|
-
* @param {string|
|
|
22
|
-
* @param {string}
|
|
23
|
-
* @param {Array}
|
|
21
|
+
* @param {string|StoreDescriptor} storeNameOrDescriptor Unique namespace identifier for the store
|
|
22
|
+
* @param {string} selectorName The name of the selector.
|
|
23
|
+
* @param {Array} args Arguments for the selector.
|
|
24
24
|
*
|
|
25
25
|
* @example
|
|
26
26
|
* ```js
|
|
@@ -36,14 +36,14 @@ const DISPATCH = '@@data/DISPATCH';
|
|
|
36
36
|
* @return {Object} The control descriptor.
|
|
37
37
|
*/
|
|
38
38
|
|
|
39
|
-
function select(
|
|
39
|
+
function select(storeNameOrDescriptor, selectorName) {
|
|
40
40
|
for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
|
|
41
41
|
args[_key - 2] = arguments[_key];
|
|
42
42
|
}
|
|
43
43
|
|
|
44
44
|
return {
|
|
45
45
|
type: SELECT,
|
|
46
|
-
storeKey: isObject(
|
|
46
|
+
storeKey: isObject(storeNameOrDescriptor) ? storeNameOrDescriptor.name : storeNameOrDescriptor,
|
|
47
47
|
selectorName,
|
|
48
48
|
args
|
|
49
49
|
};
|
|
@@ -55,9 +55,9 @@ function select(storeNameOrDefinition, selectorName) {
|
|
|
55
55
|
* selectors that may have a resolver. In such case, it will return a `Promise` that resolves
|
|
56
56
|
* after the selector finishes resolving, with the final result value.
|
|
57
57
|
*
|
|
58
|
-
* @param {string|
|
|
59
|
-
* @param {string}
|
|
60
|
-
* @param {Array}
|
|
58
|
+
* @param {string|StoreDescriptor} storeNameOrDescriptor Unique namespace identifier for the store
|
|
59
|
+
* @param {string} selectorName The name of the selector
|
|
60
|
+
* @param {Array} args Arguments for the selector.
|
|
61
61
|
*
|
|
62
62
|
* @example
|
|
63
63
|
* ```js
|
|
@@ -74,14 +74,14 @@ function select(storeNameOrDefinition, selectorName) {
|
|
|
74
74
|
*/
|
|
75
75
|
|
|
76
76
|
|
|
77
|
-
function resolveSelect(
|
|
77
|
+
function resolveSelect(storeNameOrDescriptor, selectorName) {
|
|
78
78
|
for (var _len2 = arguments.length, args = new Array(_len2 > 2 ? _len2 - 2 : 0), _key2 = 2; _key2 < _len2; _key2++) {
|
|
79
79
|
args[_key2 - 2] = arguments[_key2];
|
|
80
80
|
}
|
|
81
81
|
|
|
82
82
|
return {
|
|
83
83
|
type: RESOLVE_SELECT,
|
|
84
|
-
storeKey: isObject(
|
|
84
|
+
storeKey: isObject(storeNameOrDescriptor) ? storeNameOrDescriptor.name : storeNameOrDescriptor,
|
|
85
85
|
selectorName,
|
|
86
86
|
args
|
|
87
87
|
};
|
|
@@ -89,9 +89,9 @@ function resolveSelect(storeNameOrDefinition, selectorName) {
|
|
|
89
89
|
/**
|
|
90
90
|
* Dispatches a control action for triggering a registry dispatch.
|
|
91
91
|
*
|
|
92
|
-
* @param {string|
|
|
93
|
-
* @param {string}
|
|
94
|
-
* @param {Array}
|
|
92
|
+
* @param {string|StoreDescriptor} storeNameOrDescriptor Unique namespace identifier for the store
|
|
93
|
+
* @param {string} actionName The name of the action to dispatch
|
|
94
|
+
* @param {Array} args Arguments for the dispatch action.
|
|
95
95
|
*
|
|
96
96
|
* @example
|
|
97
97
|
* ```js
|
|
@@ -108,14 +108,14 @@ function resolveSelect(storeNameOrDefinition, selectorName) {
|
|
|
108
108
|
*/
|
|
109
109
|
|
|
110
110
|
|
|
111
|
-
function dispatch(
|
|
111
|
+
function dispatch(storeNameOrDescriptor, actionName) {
|
|
112
112
|
for (var _len3 = arguments.length, args = new Array(_len3 > 2 ? _len3 - 2 : 0), _key3 = 2; _key3 < _len3; _key3++) {
|
|
113
113
|
args[_key3 - 2] = arguments[_key3];
|
|
114
114
|
}
|
|
115
115
|
|
|
116
116
|
return {
|
|
117
117
|
type: DISPATCH,
|
|
118
|
-
storeKey: isObject(
|
|
118
|
+
storeKey: isObject(storeNameOrDescriptor) ? storeNameOrDescriptor.name : storeNameOrDescriptor,
|
|
119
119
|
actionName,
|
|
120
120
|
args
|
|
121
121
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/data/src/controls.js"],"names":["isObject","createRegistryControl","SELECT","RESOLVE_SELECT","DISPATCH","select","
|
|
1
|
+
{"version":3,"sources":["@wordpress/data/src/controls.js"],"names":["isObject","createRegistryControl","SELECT","RESOLVE_SELECT","DISPATCH","select","storeNameOrDescriptor","selectorName","args","type","storeKey","name","resolveSelect","dispatch","actionName","controls","builtinControls","registry","method","hasResolver"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,QAAT,QAAyB,QAAzB;AAEA;AACA;AACA;;AACA,SAASC,qBAAT,QAAsC,WAAtC;AAEA;;AAEA,MAAMC,MAAM,GAAG,eAAf;AACA,MAAMC,cAAc,GAAG,uBAAvB;AACA,MAAMC,QAAQ,GAAG,iBAAjB;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;A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AAzB,CAAjB;AAEP,OAAO,MAAMG,eAAe,GAAG;AAC9B,GAAEd,MAAF,GAAYD,qBAAqB,CAC9BgB,QAAF,IAAgB;AAAA,QAAE;AAAEP,MAAAA,QAAF;AAAYH,MAAAA,YAAZ;AAA0BC,MAAAA;AAA1B,KAAF;AAAA,WACfS,QAAQ,CAACZ,MAAT,CAAiBK,QAAjB,EAA6BH,YAA7B,EAA6C,GAAGC,IAAhD,CADe;AAAA,GADgB,CADH;AAK9B,GAAEL,cAAF,GAAoBF,qBAAqB,CACtCgB,QAAF,IAAgB,SAAwC;AAAA,QAAtC;AAAEP,MAAAA,QAAF;AAAYH,MAAAA,YAAZ;AAA0BC,MAAAA;AAA1B,KAAsC;AACvD,UAAMU,MAAM,GAAGD,QAAQ,CAACZ,MAAT,CAAiBK,QAAjB,EAA6BH,YAA7B,EACbY,WADa,GAEZ,eAFY,GAGZ,QAHH;AAIA,WAAOF,QAAQ,CAAEC,MAAF,CAAR,CAAoBR,QAApB,EAAgCH,YAAhC,EAAgD,GAAGC,IAAnD,CAAP;AACA,GAPuC,CALX;AAc9B,GAAEJ,QAAF,GAAcH,qBAAqB,CAChCgB,QAAF,IAAgB;AAAA,QAAE;AAAEP,MAAAA,QAAF;AAAYI,MAAAA,UAAZ;AAAwBN,MAAAA;AAAxB,KAAF;AAAA,WACfS,QAAQ,CAACJ,QAAT,CAAmBH,QAAnB,EAA+BI,UAA/B,EAA6C,GAAGN,IAAhD,CADe;AAAA,GADkB;AAdL,CAAxB","sourcesContent":["/**\n * External dependencies\n */\nimport { isObject } from 'lodash';\n\n/**\n * Internal dependencies\n */\nimport { createRegistryControl } from './factory';\n\n/** @typedef {import('./types').StoreDescriptor} StoreDescriptor */\n\nconst SELECT = '@@data/SELECT';\nconst RESOLVE_SELECT = '@@data/RESOLVE_SELECT';\nconst DISPATCH = '@@data/DISPATCH';\n\n/**\n * Dispatches a control action for triggering a synchronous registry select.\n *\n * Note: This control synchronously returns the current selector value, triggering the\n * resolution, but not waiting for it.\n *\n * @param {string|StoreDescriptor} storeNameOrDescriptor Unique namespace identifier for the store\n * @param {string} selectorName The name of the selector.\n * @param {Array} args Arguments for the selector.\n *\n * @example\n * ```js\n * import { controls } from '@wordpress/data';\n *\n * // Action generator using `select`.\n * export function* myAction() {\n * const isEditorSideBarOpened = yield controls.select( 'core/edit-post', 'isEditorSideBarOpened' );\n * // Do stuff with the result from the `select`.\n * }\n * ```\n *\n * @return {Object} The control descriptor.\n */\nfunction select( storeNameOrDescriptor, selectorName, ...args ) {\n\treturn {\n\t\ttype: SELECT,\n\t\tstoreKey: isObject( storeNameOrDescriptor )\n\t\t\t? storeNameOrDescriptor.name\n\t\t\t: storeNameOrDescriptor,\n\t\tselectorName,\n\t\targs,\n\t};\n}\n\n/**\n * Dispatches a control action for triggering and resolving a registry select.\n *\n * Note: when this control action is handled, it automatically considers\n * selectors that may have a resolver. In such case, it will return a `Promise` that resolves\n * after the selector finishes resolving, with the final result value.\n *\n * @param {string|StoreDescriptor} storeNameOrDescriptor Unique namespace identifier for the store\n * @param {string} selectorName The name of the selector\n * @param {Array} args Arguments for the selector.\n *\n * @example\n * ```js\n * import { controls } from '@wordpress/data';\n *\n * // Action generator using resolveSelect\n * export function* myAction() {\n * \tconst isSidebarOpened = yield controls.resolveSelect( 'core/edit-post', 'isEditorSideBarOpened' );\n * \t// do stuff with the result from the select.\n * }\n * ```\n *\n * @return {Object} The control descriptor.\n */\nfunction resolveSelect( storeNameOrDescriptor, selectorName, ...args ) {\n\treturn {\n\t\ttype: RESOLVE_SELECT,\n\t\tstoreKey: isObject( storeNameOrDescriptor )\n\t\t\t? storeNameOrDescriptor.name\n\t\t\t: storeNameOrDescriptor,\n\t\tselectorName,\n\t\targs,\n\t};\n}\n\n/**\n * Dispatches a control action for triggering a registry dispatch.\n *\n * @param {string|StoreDescriptor} storeNameOrDescriptor Unique namespace identifier for the store\n * @param {string} actionName The name of the action to dispatch\n * @param {Array} args Arguments for the dispatch action.\n *\n * @example\n * ```js\n * import { controls } from '@wordpress/data-controls';\n *\n * // Action generator using dispatch\n * export function* myAction() {\n * yield controls.dispatch( 'core/edit-post', 'togglePublishSidebar' );\n * // do some other things.\n * }\n * ```\n *\n * @return {Object} The control descriptor.\n */\nfunction dispatch( storeNameOrDescriptor, actionName, ...args ) {\n\treturn {\n\t\ttype: DISPATCH,\n\t\tstoreKey: isObject( storeNameOrDescriptor )\n\t\t\t? storeNameOrDescriptor.name\n\t\t\t: storeNameOrDescriptor,\n\t\tactionName,\n\t\targs,\n\t};\n}\n\nexport const controls = { select, resolveSelect, dispatch };\n\nexport const builtinControls = {\n\t[ SELECT ]: createRegistryControl(\n\t\t( registry ) => ( { storeKey, selectorName, args } ) =>\n\t\t\tregistry.select( storeKey )[ selectorName ]( ...args )\n\t),\n\t[ RESOLVE_SELECT ]: createRegistryControl(\n\t\t( registry ) => ( { storeKey, selectorName, args } ) => {\n\t\t\tconst method = registry.select( storeKey )[ selectorName ]\n\t\t\t\t.hasResolver\n\t\t\t\t? 'resolveSelect'\n\t\t\t\t: 'select';\n\t\t\treturn registry[ method ]( storeKey )[ selectorName ]( ...args );\n\t\t}\n\t),\n\t[ DISPATCH ]: createRegistryControl(\n\t\t( registry ) => ( { storeKey, actionName, args } ) =>\n\t\t\tregistry.dispatch( storeKey )[ actionName ]( ...args )\n\t),\n};\n"]}
|
package/build-module/index.js
CHANGED
|
@@ -8,7 +8,7 @@ import combineReducers from 'turbo-combine-reducers';
|
|
|
8
8
|
|
|
9
9
|
import defaultRegistry from './default-registry';
|
|
10
10
|
import * as plugins from './plugins';
|
|
11
|
-
/** @typedef {import('./types').
|
|
11
|
+
/** @typedef {import('./types').StoreDescriptor} StoreDescriptor */
|
|
12
12
|
|
|
13
13
|
export { default as withSelect } from './components/with-select';
|
|
14
14
|
export { default as withDispatch } from './components/with-dispatch';
|
|
@@ -71,12 +71,12 @@ export { plugins };
|
|
|
71
71
|
|
|
72
72
|
export { combineReducers };
|
|
73
73
|
/**
|
|
74
|
-
* Given the name or
|
|
74
|
+
* Given the name or descriptor of a registered store, returns an object of the store's selectors.
|
|
75
75
|
* The selector functions are been pre-bound to pass the current state automatically.
|
|
76
76
|
* As a consumer, you need only pass arguments of the selector, if applicable.
|
|
77
77
|
*
|
|
78
|
-
* @param {string|
|
|
79
|
-
*
|
|
78
|
+
* @param {string|StoreDescriptor} storeNameOrDescriptor Unique namespace identifier for the store
|
|
79
|
+
* or the store descriptor.
|
|
80
80
|
*
|
|
81
81
|
* @example
|
|
82
82
|
* ```js
|
|
@@ -95,8 +95,8 @@ export const select = defaultRegistry.select;
|
|
|
95
95
|
* and modified so that they return promises that resolve to their eventual values,
|
|
96
96
|
* after any resolvers have ran.
|
|
97
97
|
*
|
|
98
|
-
* @param {string|
|
|
99
|
-
*
|
|
98
|
+
* @param {string|StoreDescriptor} storeNameOrDescriptor Unique namespace identifier for the store
|
|
99
|
+
* or the store descriptor.
|
|
100
100
|
*
|
|
101
101
|
* @example
|
|
102
102
|
* ```js
|
|
@@ -116,8 +116,8 @@ export const resolveSelect = defaultRegistry.resolveSelect;
|
|
|
116
116
|
* Note: Action creators returned by the dispatch will return a promise when
|
|
117
117
|
* they are called.
|
|
118
118
|
*
|
|
119
|
-
* @param {string|
|
|
120
|
-
*
|
|
119
|
+
* @param {string|StoreDescriptor} storeNameOrDescriptor Unique namespace identifier for the store
|
|
120
|
+
* or the store descriptor.
|
|
121
121
|
*
|
|
122
122
|
* @example
|
|
123
123
|
* ```js
|
|
@@ -152,12 +152,12 @@ export const dispatch = defaultRegistry.dispatch;
|
|
|
152
152
|
|
|
153
153
|
export const subscribe = defaultRegistry.subscribe;
|
|
154
154
|
/**
|
|
155
|
-
* Registers a generic store.
|
|
155
|
+
* Registers a generic store instance.
|
|
156
156
|
*
|
|
157
|
-
* @deprecated Use `register` instead.
|
|
157
|
+
* @deprecated Use `register( storeDescriptor )` instead.
|
|
158
158
|
*
|
|
159
|
-
* @param {string}
|
|
160
|
-
* @param {Object}
|
|
159
|
+
* @param {string} name Store registry name.
|
|
160
|
+
* @param {Object} store Store instance (`{ getSelectors, getActions, subscribe }`).
|
|
161
161
|
*/
|
|
162
162
|
|
|
163
163
|
export const registerGenericStore = defaultRegistry.registerGenericStore;
|
|
@@ -183,7 +183,7 @@ export const registerStore = defaultRegistry.registerStore;
|
|
|
183
183
|
|
|
184
184
|
export const use = defaultRegistry.use;
|
|
185
185
|
/**
|
|
186
|
-
* Registers a standard `@wordpress/data` store
|
|
186
|
+
* Registers a standard `@wordpress/data` store descriptor.
|
|
187
187
|
*
|
|
188
188
|
* @example
|
|
189
189
|
* ```js
|
|
@@ -198,7 +198,7 @@ export const use = defaultRegistry.use;
|
|
|
198
198
|
* register( store );
|
|
199
199
|
* ```
|
|
200
200
|
*
|
|
201
|
-
* @param {
|
|
201
|
+
* @param {StoreDescriptor} store Store descriptor.
|
|
202
202
|
*/
|
|
203
203
|
|
|
204
204
|
export const register = defaultRegistry.register;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/data/src/index.js"],"names":["combineReducers","defaultRegistry","plugins","default","withSelect","withDispatch","withRegistry","RegistryProvider","RegistryConsumer","useRegistry","useSelect","useDispatch","AsyncModeProvider","createRegistry","createRegistrySelector","createRegistryControl","controls","createReduxStore","select","resolveSelect","dispatch","subscribe","registerGenericStore","registerStore","use","register"],"mappings":"AAAA;AACA;AACA;AACA,OAAOA,eAAP,MAA4B,wBAA5B;AAEA;AACA;AACA;;AACA,OAAOC,eAAP,MAA4B,oBAA5B;AACA,OAAO,KAAKC,OAAZ,MAAyB,WAAzB;AAEA;;AAEA,SAASC,OAAO,IAAIC,UAApB,QAAsC,0BAAtC;AACA,SAASD,OAAO,IAAIE,YAApB,QAAwC,4BAAxC;AACA,SAASF,OAAO,IAAIG,YAApB,QAAwC,4BAAxC;AACA,SACCC,gBADD,EAECC,gBAFD,EAGCC,WAHD,QAIO,gCAJP;AAKA,SAASN,OAAO,IAAIO,SAApB,QAAqC,yBAArC;AACA,SAASC,WAAT,QAA4B,2BAA5B;AACA,SAASC,iBAAT,QAAkC,kCAAlC;AACA,SAASC,cAAT,QAA+B,YAA/B;AACA,SAASC,sBAAT,EAAiCC,qBAAjC,QAA8D,WAA9D;AACA,SAASC,QAAT,QAAyB,YAAzB;AACA,SAASb,OAAO,IAAIc,gBAApB,QAA4C,eAA5C;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,SAASf,OAAT;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,SAASF,eAAT;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,MAAMkB,MAAM,GAAGjB,eAAe,CAACiB,MAA/B;AAEP;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,MAAMC,aAAa,GAAGlB,eAAe,CAACkB,aAAtC;AAEP;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,MAAMC,QAAQ,GAAGnB,eAAe,CAACmB,QAAjC;AAEP;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,MAAMC,SAAS,GAAGpB,eAAe,CAACoB,SAAlC;AAEP;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,MAAMC,oBAAoB,GAAGrB,eAAe,CAACqB,oBAA7C;AAEP;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,MAAMC,aAAa,GAAGtB,eAAe,CAACsB,aAAtC;AAEP;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,MAAMC,GAAG,GAAGvB,eAAe,CAACuB,GAA5B;AAEP;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,MAAMC,QAAQ,GAAGxB,eAAe,CAACwB,QAAjC","sourcesContent":["/**\n * External dependencies\n */\nimport combineReducers from 'turbo-combine-reducers';\n\n/**\n * Internal dependencies\n */\nimport defaultRegistry from './default-registry';\nimport * as plugins from './plugins';\n\n/** @typedef {import('./types').
|
|
1
|
+
{"version":3,"sources":["@wordpress/data/src/index.js"],"names":["combineReducers","defaultRegistry","plugins","default","withSelect","withDispatch","withRegistry","RegistryProvider","RegistryConsumer","useRegistry","useSelect","useDispatch","AsyncModeProvider","createRegistry","createRegistrySelector","createRegistryControl","controls","createReduxStore","select","resolveSelect","dispatch","subscribe","registerGenericStore","registerStore","use","register"],"mappings":"AAAA;AACA;AACA;AACA,OAAOA,eAAP,MAA4B,wBAA5B;AAEA;AACA;AACA;;AACA,OAAOC,eAAP,MAA4B,oBAA5B;AACA,OAAO,KAAKC,OAAZ,MAAyB,WAAzB;AAEA;;AAEA,SAASC,OAAO,IAAIC,UAApB,QAAsC,0BAAtC;AACA,SAASD,OAAO,IAAIE,YAApB,QAAwC,4BAAxC;AACA,SAASF,OAAO,IAAIG,YAApB,QAAwC,4BAAxC;AACA,SACCC,gBADD,EAECC,gBAFD,EAGCC,WAHD,QAIO,gCAJP;AAKA,SAASN,OAAO,IAAIO,SAApB,QAAqC,yBAArC;AACA,SAASC,WAAT,QAA4B,2BAA5B;AACA,SAASC,iBAAT,QAAkC,kCAAlC;AACA,SAASC,cAAT,QAA+B,YAA/B;AACA,SAASC,sBAAT,EAAiCC,qBAAjC,QAA8D,WAA9D;AACA,SAASC,QAAT,QAAyB,YAAzB;AACA,SAASb,OAAO,IAAIc,gBAApB,QAA4C,eAA5C;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,SAASf,OAAT;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,SAASF,eAAT;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,MAAMkB,MAAM,GAAGjB,eAAe,CAACiB,MAA/B;AAEP;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,MAAMC,aAAa,GAAGlB,eAAe,CAACkB,aAAtC;AAEP;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,MAAMC,QAAQ,GAAGnB,eAAe,CAACmB,QAAjC;AAEP;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,MAAMC,SAAS,GAAGpB,eAAe,CAACoB,SAAlC;AAEP;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,MAAMC,oBAAoB,GAAGrB,eAAe,CAACqB,oBAA7C;AAEP;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,MAAMC,aAAa,GAAGtB,eAAe,CAACsB,aAAtC;AAEP;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,MAAMC,GAAG,GAAGvB,eAAe,CAACuB,GAA5B;AAEP;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,MAAMC,QAAQ,GAAGxB,eAAe,CAACwB,QAAjC","sourcesContent":["/**\n * External dependencies\n */\nimport combineReducers from 'turbo-combine-reducers';\n\n/**\n * Internal dependencies\n */\nimport defaultRegistry from './default-registry';\nimport * as plugins from './plugins';\n\n/** @typedef {import('./types').StoreDescriptor} StoreDescriptor */\n\nexport { default as withSelect } from './components/with-select';\nexport { default as withDispatch } from './components/with-dispatch';\nexport { default as withRegistry } from './components/with-registry';\nexport {\n\tRegistryProvider,\n\tRegistryConsumer,\n\tuseRegistry,\n} from './components/registry-provider';\nexport { default as useSelect } from './components/use-select';\nexport { useDispatch } from './components/use-dispatch';\nexport { AsyncModeProvider } from './components/async-mode-provider';\nexport { createRegistry } from './registry';\nexport { createRegistrySelector, createRegistryControl } from './factory';\nexport { controls } from './controls';\nexport { default as createReduxStore } from './redux-store';\n\n/**\n * Object of available plugins to use with a registry.\n *\n * @see [use](#use)\n *\n * @type {Object}\n */\nexport { plugins };\n\n/**\n * The combineReducers helper function turns an object whose values are different\n * reducing functions into a single reducing function you can pass to registerReducer.\n *\n * @param {Object} reducers An object whose values correspond to different reducing\n * functions that need to be combined into one.\n *\n * @example\n * ```js\n * import { combineReducers, createReduxStore, register } from '@wordpress/data';\n *\n * const prices = ( state = {}, action ) => {\n * \treturn action.type === 'SET_PRICE' ?\n * \t\t{\n * \t\t\t...state,\n * \t\t\t[ action.item ]: action.price,\n * \t\t} :\n * \t\tstate;\n * };\n *\n * const discountPercent = ( state = 0, action ) => {\n * \treturn action.type === 'START_SALE' ?\n * \t\taction.discountPercent :\n * \t\tstate;\n * };\n *\n * const store = createReduxStore( 'my-shop', {\n * \treducer: combineReducers( {\n * \t\tprices,\n * \t\tdiscountPercent,\n * \t} ),\n * } );\n * register( store );\n * ```\n *\n * @return {Function} A reducer that invokes every reducer inside the reducers\n * object, and constructs a state object with the same shape.\n */\nexport { combineReducers };\n\n/**\n * Given the name or descriptor of a registered store, returns an object of the store's selectors.\n * The selector functions are been pre-bound to pass the current state automatically.\n * As a consumer, you need only pass arguments of the selector, if applicable.\n *\n * @param {string|StoreDescriptor} storeNameOrDescriptor Unique namespace identifier for the store\n * or the store descriptor.\n *\n * @example\n * ```js\n * import { select } from '@wordpress/data';\n *\n * select( 'my-shop' ).getPrice( 'hammer' );\n * ```\n *\n * @return {Object} Object containing the store's selectors.\n */\nexport const select = defaultRegistry.select;\n\n/**\n * Given the name of a registered store, returns an object containing the store's\n * selectors pre-bound to state so that you only need to supply additional arguments,\n * and modified so that they return promises that resolve to their eventual values,\n * after any resolvers have ran.\n *\n * @param {string|StoreDescriptor} storeNameOrDescriptor Unique namespace identifier for the store\n * or the store descriptor.\n *\n * @example\n * ```js\n * import { resolveSelect } from '@wordpress/data';\n *\n * resolveSelect( 'my-shop' ).getPrice( 'hammer' ).then(console.log)\n * ```\n *\n * @return {Object} Object containing the store's promise-wrapped selectors.\n */\nexport const resolveSelect = defaultRegistry.resolveSelect;\n\n/**\n * Given the name of a registered store, returns an object of the store's action creators.\n * Calling an action creator will cause it to be dispatched, updating the state value accordingly.\n *\n * Note: Action creators returned by the dispatch will return a promise when\n * they are called.\n *\n * @param {string|StoreDescriptor} storeNameOrDescriptor Unique namespace identifier for the store\n * or the store descriptor.\n *\n * @example\n * ```js\n * import { dispatch } from '@wordpress/data';\n *\n * dispatch( 'my-shop' ).setPrice( 'hammer', 9.75 );\n * ```\n * @return {Object} Object containing the action creators.\n */\nexport const dispatch = defaultRegistry.dispatch;\n\n/**\n * Given a listener function, the function will be called any time the state value\n * of one of the registered stores has changed. This function returns a `unsubscribe`\n * function used to stop the subscription.\n *\n * @param {Function} listener Callback function.\n *\n * @example\n * ```js\n * import { subscribe } from '@wordpress/data';\n *\n * const unsubscribe = subscribe( () => {\n * \t// You could use this opportunity to test whether the derived result of a\n * \t// selector has subsequently changed as the result of a state update.\n * } );\n *\n * // Later, if necessary...\n * unsubscribe();\n * ```\n */\nexport const subscribe = defaultRegistry.subscribe;\n\n/**\n * Registers a generic store instance.\n *\n * @deprecated Use `register( storeDescriptor )` instead.\n *\n * @param {string} name Store registry name.\n * @param {Object} store Store instance (`{ getSelectors, getActions, subscribe }`).\n */\nexport const registerGenericStore = defaultRegistry.registerGenericStore;\n\n/**\n * Registers a standard `@wordpress/data` store.\n *\n * @deprecated Use `register` instead.\n *\n * @param {string} storeName Unique namespace identifier for the store.\n * @param {Object} options Store description (reducer, actions, selectors, resolvers).\n *\n * @return {Object} Registered store object.\n */\nexport const registerStore = defaultRegistry.registerStore;\n\n/**\n * Extends a registry to inherit functionality provided by a given plugin. A\n * plugin is an object with properties aligning to that of a registry, merged\n * to extend the default registry behavior.\n *\n * @param {Object} plugin Plugin object.\n */\nexport const use = defaultRegistry.use;\n\n/**\n * Registers a standard `@wordpress/data` store descriptor.\n *\n * @example\n * ```js\n * import { createReduxStore, register } from '@wordpress/data';\n *\n * const store = createReduxStore( 'demo', {\n * reducer: ( state = 'OK' ) => state,\n * selectors: {\n * getValue: ( state ) => state,\n * },\n * } );\n * register( store );\n * ```\n *\n * @param {StoreDescriptor} store Store descriptor.\n */\nexport const register = defaultRegistry.register;\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/data/src/plugins/index.js"],"names":["default","
|
|
1
|
+
{"version":3,"sources":["@wordpress/data/src/plugins/index.js"],"names":["default","persistence"],"mappings":"AAAA,SAASA,OAAO,IAAIC,WAApB,QAAuC,eAAvC","sourcesContent":["export { default as persistence } from './persistence';\n"]}
|
|
@@ -21,11 +21,11 @@ import createThunkMiddleware from './thunk-middleware';
|
|
|
21
21
|
import metadataReducer from './metadata/reducer';
|
|
22
22
|
import * as metadataSelectors from './metadata/selectors';
|
|
23
23
|
import * as metadataActions from './metadata/actions';
|
|
24
|
-
/** @typedef {import('../types').
|
|
24
|
+
/** @typedef {import('../types').DataRegistry} DataRegistry */
|
|
25
25
|
|
|
26
|
-
/** @typedef {import('../types').
|
|
26
|
+
/** @typedef {import('../types').StoreDescriptor} StoreDescriptor */
|
|
27
27
|
|
|
28
|
-
/** @typedef {import('../types').
|
|
28
|
+
/** @typedef {import('../types').ReduxStoreConfig} ReduxStoreConfig */
|
|
29
29
|
|
|
30
30
|
const trimUndefinedValues = array => {
|
|
31
31
|
const result = [...array];
|
|
@@ -69,7 +69,7 @@ function createResolversCache() {
|
|
|
69
69
|
};
|
|
70
70
|
}
|
|
71
71
|
/**
|
|
72
|
-
* Creates a data store
|
|
72
|
+
* Creates a data store descriptor for the provided Redux store configuration containing
|
|
73
73
|
* properties describing reducer, actions, selectors, controls and resolvers.
|
|
74
74
|
*
|
|
75
75
|
* @example
|
|
@@ -84,12 +84,12 @@ function createResolversCache() {
|
|
|
84
84
|
* } );
|
|
85
85
|
* ```
|
|
86
86
|
*
|
|
87
|
-
* @param {string}
|
|
88
|
-
* @param {
|
|
89
|
-
*
|
|
90
|
-
*
|
|
87
|
+
* @param {string} key Unique namespace identifier.
|
|
88
|
+
* @param {ReduxStoreConfig} options Registered store options, with properties
|
|
89
|
+
* describing reducer, actions, selectors,
|
|
90
|
+
* and resolvers.
|
|
91
91
|
*
|
|
92
|
-
* @return {
|
|
92
|
+
* @return {StoreDescriptor} Store Object.
|
|
93
93
|
*/
|
|
94
94
|
|
|
95
95
|
|
|
@@ -199,12 +199,12 @@ export default function createReduxStore(key, options) {
|
|
|
199
199
|
/**
|
|
200
200
|
* Creates a redux store for a namespace.
|
|
201
201
|
*
|
|
202
|
-
* @param {string}
|
|
203
|
-
* @param {Object}
|
|
204
|
-
*
|
|
205
|
-
*
|
|
206
|
-
* @param {
|
|
207
|
-
* @param {Object}
|
|
202
|
+
* @param {string} key Unique namespace identifier.
|
|
203
|
+
* @param {Object} options Registered store options, with properties
|
|
204
|
+
* describing reducer, actions, selectors,
|
|
205
|
+
* and resolvers.
|
|
206
|
+
* @param {DataRegistry} registry Registry reference.
|
|
207
|
+
* @param {Object} thunkArgs Argument object for the thunk middleware.
|
|
208
208
|
* @return {Object} Newly created redux store.
|
|
209
209
|
*/
|
|
210
210
|
|