@wordpress/data 8.6.0 → 9.0.0

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.
@@ -37,48 +37,87 @@ function Store(registry, suspense) {
37
37
  let lastMapResult;
38
38
  let lastMapResultValid = false;
39
39
  let lastIsAsync;
40
- let subscribe;
41
-
42
- const createSubscriber = stores => listener => {
43
- // Invalidate the value right after subscription was created. React will
44
- // call `getValue` after subscribing, to detect store updates that happened
45
- // in the interval between the `getValue` call during render and creating
46
- // the subscription, which is slightly delayed. We need to ensure that this
47
- // second `getValue` call will compute a fresh value.
48
- lastMapResultValid = false;
49
-
50
- const onStoreChange = () => {
51
- // Invalidate the value on store update, so that a fresh value is computed.
40
+ let subscriber;
41
+
42
+ const createSubscriber = stores => {
43
+ // The set of stores the `subscribe` function is supposed to subscribe to. Here it is
44
+ // initialized, and then the `updateStores` function can add new stores to it.
45
+ const activeStores = [...stores]; // The `subscribe` function, which is passed to the `useSyncExternalStore` hook, could
46
+ // be called multiple times to establish multiple subscriptions. That's why we need to
47
+ // keep a set of active subscriptions;
48
+
49
+ const activeSubscriptions = new Set();
50
+
51
+ function subscribe(listener) {
52
+ // Invalidate the value right after subscription was created. React will
53
+ // call `getValue` after subscribing, to detect store updates that happened
54
+ // in the interval between the `getValue` call during render and creating
55
+ // the subscription, which is slightly delayed. We need to ensure that this
56
+ // second `getValue` call will compute a fresh value.
52
57
  lastMapResultValid = false;
53
- listener();
54
- };
55
58
 
56
- const onChange = () => {
57
- if (lastIsAsync) {
58
- renderQueue.add(queueContext, onStoreChange);
59
- } else {
60
- onStoreChange();
59
+ const onStoreChange = () => {
60
+ // Invalidate the value on store update, so that a fresh value is computed.
61
+ lastMapResultValid = false;
62
+ listener();
63
+ };
64
+
65
+ const onChange = () => {
66
+ if (lastIsAsync) {
67
+ renderQueue.add(queueContext, onStoreChange);
68
+ } else {
69
+ onStoreChange();
70
+ }
71
+ };
72
+
73
+ const unsubs = [];
74
+
75
+ function subscribeStore(storeName) {
76
+ unsubs.push(registry.subscribe(onChange, storeName));
61
77
  }
62
- };
63
78
 
64
- const unsubs = stores.map(storeName => {
65
- return registry.subscribe(onChange, storeName);
66
- });
67
- return () => {
68
- // The return value of the subscribe function could be undefined if the store is a custom generic store.
69
- for (const unsub of unsubs) {
70
- unsub === null || unsub === void 0 ? void 0 : unsub();
71
- } // Cancel existing store updates that were already scheduled.
79
+ for (const storeName of activeStores) {
80
+ subscribeStore(storeName);
81
+ }
72
82
 
83
+ activeSubscriptions.add(subscribeStore);
84
+ return () => {
85
+ activeSubscriptions.delete(subscribeStore);
73
86
 
74
- renderQueue.cancel(queueContext);
87
+ for (const unsub of unsubs.values()) {
88
+ // The return value of the subscribe function could be undefined if the store is a custom generic store.
89
+ unsub === null || unsub === void 0 ? void 0 : unsub();
90
+ } // Cancel existing store updates that were already scheduled.
91
+
92
+
93
+ renderQueue.cancel(queueContext);
94
+ };
95
+ } // Check if `newStores` contains some stores we're not subscribed to yet, and add them.
96
+
97
+
98
+ function updateStores(newStores) {
99
+ for (const newStore of newStores) {
100
+ if (activeStores.includes(newStore)) {
101
+ continue;
102
+ } // New `subscribe` calls will subscribe to `newStore`, too.
103
+
104
+
105
+ activeStores.push(newStore); // Add `newStore` to existing subscriptions.
106
+
107
+ for (const subscription of activeSubscriptions) {
108
+ subscription(newStore);
109
+ }
110
+ }
111
+ }
112
+
113
+ return {
114
+ subscribe,
115
+ updateStores
75
116
  };
76
117
  };
77
118
 
78
- return (mapSelect, resubscribe, isAsync) => {
79
- const selectValue = () => mapSelect(select, registry);
80
-
81
- function updateValue(selectFromStore) {
119
+ return (mapSelect, isAsync) => {
120
+ function updateValue() {
82
121
  // If the last value is valid, and the `mapSelect` callback hasn't changed,
83
122
  // then we can safely return the cached value. The value can change only on
84
123
  // store update, and in that case value will be invalidated by the listener.
@@ -86,19 +125,31 @@ function Store(registry, suspense) {
86
125
  return lastMapResult;
87
126
  }
88
127
 
89
- const mapResult = selectFromStore(); // If the new value is shallow-equal to the old one, keep the old one so
128
+ const listeningStores = {
129
+ current: null
130
+ };
131
+
132
+ const mapResult = registry.__unstableMarkListeningStores(() => mapSelect(select, registry), listeningStores);
133
+
134
+ if (!subscriber) {
135
+ subscriber = createSubscriber(listeningStores.current);
136
+ } else {
137
+ subscriber.updateStores(listeningStores.current);
138
+ } // If the new value is shallow-equal to the old one, keep the old one so
90
139
  // that we don't trigger unwanted updates that do a `===` check.
91
140
 
141
+
92
142
  if (!isShallowEqual(lastMapResult, mapResult)) {
93
143
  lastMapResult = mapResult;
94
144
  }
95
145
 
146
+ lastMapSelect = mapSelect;
96
147
  lastMapResultValid = true;
97
148
  }
98
149
 
99
150
  function getValue() {
100
151
  // Update the value in case it's been invalidated or `mapSelect` has changed.
101
- updateValue(selectValue);
152
+ updateValue();
102
153
  return lastMapResult;
103
154
  } // When transitioning from async to sync mode, cancel existing store updates
104
155
  // that have been scheduled, and invalidate the value so that it's freshly
@@ -108,29 +159,13 @@ function Store(registry, suspense) {
108
159
  if (lastIsAsync && !isAsync) {
109
160
  lastMapResultValid = false;
110
161
  renderQueue.cancel(queueContext);
111
- } // Either initialize the `subscribe` function, or create a new one if `mapSelect`
112
- // changed and has dependencies.
113
- // Usage without dependencies, `useSelect( ( s ) => { ... } )`, will subscribe
114
- // only once, at mount, and won't resubscibe even if `mapSelect` changes.
115
-
116
-
117
- if (!subscribe || resubscribe && mapSelect !== lastMapSelect) {
118
- // Find out what stores the `mapSelect` callback is selecting from and
119
- // use that list to create subscriptions to specific stores.
120
- const listeningStores = {
121
- current: null
122
- };
123
- updateValue(() => registry.__unstableMarkListeningStores(selectValue, listeningStores));
124
- subscribe = createSubscriber(listeningStores.current);
125
- } else {
126
- updateValue(selectValue);
127
162
  }
128
163
 
129
- lastIsAsync = isAsync;
130
- lastMapSelect = mapSelect; // Return a pair of functions that can be passed to `useSyncExternalStore`.
164
+ updateValue();
165
+ lastIsAsync = isAsync; // Return a pair of functions that can be passed to `useSyncExternalStore`.
131
166
 
132
167
  return {
133
- subscribe,
168
+ subscribe: subscriber.subscribe,
134
169
  getValue
135
170
  };
136
171
  };
@@ -148,7 +183,7 @@ function useMappingSelect(suspense, mapSelect, deps) {
148
183
  const {
149
184
  subscribe,
150
185
  getValue
151
- } = store(selector, !!deps, isAsync);
186
+ } = store(selector, isAsync);
152
187
  const result = useSyncExternalStore(subscribe, getValue, getValue);
153
188
  useDebugValue(result);
154
189
  return result;
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/data/src/components/use-select/index.js"],"names":["createQueue","useRef","useCallback","useMemo","useSyncExternalStore","useDebugValue","isShallowEqual","useRegistry","useAsyncMode","renderQueue","Store","registry","suspense","select","suspendSelect","queueContext","lastMapSelect","lastMapResult","lastMapResultValid","lastIsAsync","subscribe","createSubscriber","stores","listener","onStoreChange","onChange","add","unsubs","map","storeName","unsub","cancel","mapSelect","resubscribe","isAsync","selectValue","updateValue","selectFromStore","mapResult","getValue","listeningStores","current","__unstableMarkListeningStores","useStaticSelect","useMappingSelect","deps","store","selector","result","useSelect","staticSelectMode","staticSelectModeRef","prevMode","nextMode","Error","useSuspenseSelect"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,WAAT,QAA4B,2BAA5B;AACA,SACCC,MADD,EAECC,WAFD,EAGCC,OAHD,EAICC,oBAJD,EAKCC,aALD,QAMO,oBANP;AAOA,OAAOC,cAAP,MAA2B,6BAA3B;AAEA;AACA;AACA;;AACA,OAAOC,WAAP,MAAwB,mCAAxB;AACA,OAAOC,YAAP,MAAyB,uCAAzB;AAEA,MAAMC,WAAW,GAAGT,WAAW,EAA/B;AAEA;AACA;AACA;AACA;;AACA;AACA;AACA;AACA;AACA;AACA;;AACA;;AACA;AACA;AACA;AACA;;AAEA,SAASU,KAAT,CAAgBC,QAAhB,EAA0BC,QAA1B,EAAqC;AACpC,QAAMC,MAAM,GAAGD,QAAQ,GAAGD,QAAQ,CAACG,aAAZ,GAA4BH,QAAQ,CAACE,MAA5D;AACA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AU,OAAOC,QAAU,iBADnD,CAAN;AAGA;AAED;AACA;AACA;;;AACA,SAAOH,gBAAgB,GACpBP,eAAe,CAAEX,SAAF,CADK,GAEpBY,gBAAgB,CAAE,KAAF,EAASZ,SAAT,EAAoBa,IAApB,CAFnB;AAGA;AACA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASU,iBAAT,CAA4BvB,SAA5B,EAAuCa,IAAvC,EAA8C;AACpD,SAAOD,gBAAgB,CAAE,IAAF,EAAQZ,SAAR,EAAmBa,IAAnB,CAAvB;AACA","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { createQueue } from '@wordpress/priority-queue';\nimport {\n\tuseRef,\n\tuseCallback,\n\tuseMemo,\n\tuseSyncExternalStore,\n\tuseDebugValue,\n} from '@wordpress/element';\nimport isShallowEqual from '@wordpress/is-shallow-equal';\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/**\n * @typedef {import('../../types').StoreDescriptor<C>} StoreDescriptor\n * @template {import('../../types').AnyConfig} C\n */\n/**\n * @typedef {import('../../types').ReduxStoreConfig<State,Actions,Selectors>} ReduxStoreConfig\n * @template State\n * @template {Record<string,import('../../types').ActionCreator>} Actions\n * @template Selectors\n */\n/** @typedef {import('../../types').MapSelect} MapSelect */\n/**\n * @typedef {import('../../types').UseSelectReturn<T>} UseSelectReturn\n * @template {MapSelect|StoreDescriptor<any>} T\n */\n\nfunction Store( registry, suspense ) {\n\tconst select = suspense ? registry.suspendSelect : registry.select;\n\tconst queueContext = {};\n\tlet lastMapSelect;\n\tlet lastMapResult;\n\tlet lastMapResultValid = false;\n\tlet lastIsAsync;\n\tlet subscribe;\n\n\tconst createSubscriber = ( stores ) => ( listener ) => {\n\t\t// Invalidate the value right after subscription was created. React will\n\t\t// call `getValue` after subscribing, to detect store updates that happened\n\t\t// in the interval between the `getValue` call during render and creating\n\t\t// the subscription, which is slightly delayed. We need to ensure that this\n\t\t// second `getValue` call will compute a fresh value.\n\t\tlastMapResultValid = false;\n\n\t\tconst onStoreChange = () => {\n\t\t\t// Invalidate the value on store update, so that a fresh value is computed.\n\t\t\tlastMapResultValid = false;\n\t\t\tlistener();\n\t\t};\n\n\t\tconst onChange = () => {\n\t\t\tif ( lastIsAsync ) {\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 unsubs = stores.map( ( storeName ) => {\n\t\t\treturn registry.subscribe( onChange, storeName );\n\t\t} );\n\n\t\treturn () => {\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\tfor ( const unsub of unsubs ) {\n\t\t\t\tunsub?.();\n\t\t\t}\n\t\t\t// Cancel existing store updates that were already scheduled.\n\t\t\trenderQueue.cancel( queueContext );\n\t\t};\n\t};\n\n\treturn ( mapSelect, resubscribe, isAsync ) => {\n\t\tconst selectValue = () => mapSelect( select, registry );\n\n\t\tfunction updateValue( selectFromStore ) {\n\t\t\t// If the last value is valid, and the `mapSelect` callback hasn't changed,\n\t\t\t// then we can safely return the cached value. The value can change only on\n\t\t\t// store update, and in that case value will be invalidated by the listener.\n\t\t\tif ( lastMapResultValid && mapSelect === lastMapSelect ) {\n\t\t\t\treturn lastMapResult;\n\t\t\t}\n\n\t\t\tconst mapResult = selectFromStore();\n\n\t\t\t// If the new value is shallow-equal to the old one, keep the old one so\n\t\t\t// that we don't trigger unwanted updates that do a `===` check.\n\t\t\tif ( ! isShallowEqual( lastMapResult, mapResult ) ) {\n\t\t\t\tlastMapResult = mapResult;\n\t\t\t}\n\t\t\tlastMapResultValid = true;\n\t\t}\n\n\t\tfunction getValue() {\n\t\t\t// Update the value in case it's been invalidated or `mapSelect` has changed.\n\t\t\tupdateValue( selectValue );\n\t\t\treturn lastMapResult;\n\t\t}\n\n\t\t// When transitioning from async to sync mode, cancel existing store updates\n\t\t// that have been scheduled, and invalidate the value so that it's freshly\n\t\t// computed. It might have been changed by the update we just cancelled.\n\t\tif ( lastIsAsync && ! isAsync ) {\n\t\t\tlastMapResultValid = false;\n\t\t\trenderQueue.cancel( queueContext );\n\t\t}\n\n\t\t// Either initialize the `subscribe` function, or create a new one if `mapSelect`\n\t\t// changed and has dependencies.\n\t\t// Usage without dependencies, `useSelect( ( s ) => { ... } )`, will subscribe\n\t\t// only once, at mount, and won't resubscibe even if `mapSelect` changes.\n\t\tif ( ! subscribe || ( resubscribe && mapSelect !== lastMapSelect ) ) {\n\t\t\t// Find out what stores the `mapSelect` callback is selecting from and\n\t\t\t// use that list to create subscriptions to specific stores.\n\t\t\tconst listeningStores = { current: null };\n\t\t\tupdateValue( () =>\n\t\t\t\tregistry.__unstableMarkListeningStores(\n\t\t\t\t\tselectValue,\n\t\t\t\t\tlisteningStores\n\t\t\t\t)\n\t\t\t);\n\t\t\tsubscribe = createSubscriber( listeningStores.current );\n\t\t} else {\n\t\t\tupdateValue( selectValue );\n\t\t}\n\n\t\tlastIsAsync = isAsync;\n\t\tlastMapSelect = mapSelect;\n\n\t\t// Return a pair of functions that can be passed to `useSyncExternalStore`.\n\t\treturn { subscribe, getValue };\n\t};\n}\n\nfunction useStaticSelect( storeName ) {\n\treturn useRegistry().select( storeName );\n}\n\nfunction useMappingSelect( suspense, mapSelect, deps ) {\n\tconst registry = useRegistry();\n\tconst isAsync = useAsyncMode();\n\tconst store = useMemo( () => Store( registry, suspense ), [ registry ] );\n\tconst selector = useCallback( mapSelect, deps );\n\tconst { subscribe, getValue } = store( selector, !! deps, isAsync );\n\tconst result = useSyncExternalStore( subscribe, getValue, getValue );\n\tuseDebugValue( result );\n\treturn result;\n}\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 * @template {MapSelect | StoreDescriptor<any>} T\n * @param {T} mapSelect Function called on every state change. The returned value is\n * exposed to the component implementing this hook. The function\n * receives the `registry.select` method on the first argument\n * and the `registry` on the second argument.\n * When a store key is passed, all selectors for the store will be\n * returned. This is only meant for usage of these selectors in event\n * callbacks, not for data needed to create the element tree.\n * @param {unknown[]} deps If provided, this memoizes the mapSelect so the same `mapSelect` is\n * invoked on every state change unless the dependencies change.\n *\n * @example\n * ```js\n * import { useSelect } from '@wordpress/data';\n * import { store as myCustomStore } from 'my-custom-store';\n *\n * function HammerPriceDisplay( { currency } ) {\n * const price = useSelect( ( select ) => {\n * return select( myCustomStore ).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 * import { store as myCustomStore } from 'my-custom-store';\n *\n * function Paste( { children } ) {\n * const { getSettings } = useSelect( myCustomStore );\n * function onPaste() {\n * // Do something with the settings.\n * const settings = getSettings();\n * }\n * return <div onPaste={ onPaste }>{ children }</div>;\n * }\n * ```\n * @return {UseSelectReturn<T>} A custom react hook.\n */\nexport default function useSelect( mapSelect, deps ) {\n\t// On initial call, on mount, determine the mode of this `useSelect` call\n\t// and then never allow it to change on subsequent updates.\n\tconst staticSelectMode = typeof mapSelect !== 'function';\n\tconst staticSelectModeRef = useRef( staticSelectMode );\n\n\tif ( staticSelectMode !== staticSelectModeRef.current ) {\n\t\tconst prevMode = staticSelectModeRef.current ? 'static' : 'mapping';\n\t\tconst nextMode = staticSelectMode ? 'static' : 'mapping';\n\t\tthrow new Error(\n\t\t\t`Switching useSelect from ${ prevMode } to ${ nextMode } is not allowed`\n\t\t);\n\t}\n\n\t/* eslint-disable react-hooks/rules-of-hooks */\n\t// `staticSelectMode` is not allowed to change during the hook instance's,\n\t// lifetime, so the rules of hooks are not really violated.\n\treturn staticSelectMode\n\t\t? useStaticSelect( mapSelect )\n\t\t: useMappingSelect( false, mapSelect, deps );\n\t/* eslint-enable react-hooks/rules-of-hooks */\n}\n\n/**\n * A variant of the `useSelect` hook that has the same API, but will throw a\n * suspense Promise if any of the called selectors is in an unresolved state.\n *\n * @param {Function} mapSelect Function called on every state change. The\n * returned value is exposed to the component\n * using this hook. The function receives the\n * `registry.suspendSelect` method as the first\n * argument and the `registry` as the second one.\n * @param {Array} deps A dependency array used to memoize the `mapSelect`\n * so that the same `mapSelect` is invoked on every\n * state change unless the dependencies change.\n *\n * @return {Object} Data object returned by the `mapSelect` function.\n */\nexport function useSuspenseSelect( mapSelect, deps ) {\n\treturn useMappingSelect( true, mapSelect, deps );\n}\n"]}
1
+ {"version":3,"sources":["@wordpress/data/src/components/use-select/index.js"],"names":["createQueue","useRef","useCallback","useMemo","useSyncExternalStore","useDebugValue","isShallowEqual","useRegistry","useAsyncMode","renderQueue","Store","registry","suspense","select","suspendSelect","queueContext","lastMapSelect","lastMapResult","lastMapResultValid","lastIsAsync","subscriber","createSubscriber","stores","activeStores","activeSubscriptions","Set","subscribe","listener","onStoreChange","onChange","add","unsubs","subscribeStore","storeName","push","delete","unsub","values","cancel","updateStores","newStores","newStore","includes","subscription","mapSelect","isAsync","updateValue","listeningStores","current","mapResult","__unstableMarkListeningStores","getValue","useStaticSelect","useMappingSelect","deps","store","selector","result","useSelect","staticSelectMode","staticSelectModeRef","prevMode","nextMode","Error","useSuspenseSelect"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,W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,eAAe,CAAER,SAAF,CADK,GAEpBS,gBAAgB,CAAE,KAAF,EAAST,SAAT,EAAoBU,IAApB,CAFnB;AAGA;AACA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASU,iBAAT,CAA4BpB,SAA5B,EAAuCU,IAAvC,EAA8C;AACpD,SAAOD,gBAAgB,CAAE,IAAF,EAAQT,SAAR,EAAmBU,IAAnB,CAAvB;AACA","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { createQueue } from '@wordpress/priority-queue';\nimport {\n\tuseRef,\n\tuseCallback,\n\tuseMemo,\n\tuseSyncExternalStore,\n\tuseDebugValue,\n} from '@wordpress/element';\nimport isShallowEqual from '@wordpress/is-shallow-equal';\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/**\n * @typedef {import('../../types').StoreDescriptor<C>} StoreDescriptor\n * @template {import('../../types').AnyConfig} C\n */\n/**\n * @typedef {import('../../types').ReduxStoreConfig<State,Actions,Selectors>} ReduxStoreConfig\n * @template State\n * @template {Record<string,import('../../types').ActionCreator>} Actions\n * @template Selectors\n */\n/** @typedef {import('../../types').MapSelect} MapSelect */\n/**\n * @typedef {import('../../types').UseSelectReturn<T>} UseSelectReturn\n * @template {MapSelect|StoreDescriptor<any>} T\n */\n\nfunction Store( registry, suspense ) {\n\tconst select = suspense ? registry.suspendSelect : registry.select;\n\tconst queueContext = {};\n\tlet lastMapSelect;\n\tlet lastMapResult;\n\tlet lastMapResultValid = false;\n\tlet lastIsAsync;\n\tlet subscriber;\n\n\tconst createSubscriber = ( stores ) => {\n\t\t// The set of stores the `subscribe` function is supposed to subscribe to. Here it is\n\t\t// initialized, and then the `updateStores` function can add new stores to it.\n\t\tconst activeStores = [ ...stores ];\n\n\t\t// The `subscribe` function, which is passed to the `useSyncExternalStore` hook, could\n\t\t// be called multiple times to establish multiple subscriptions. That's why we need to\n\t\t// keep a set of active subscriptions;\n\t\tconst activeSubscriptions = new Set();\n\n\t\tfunction subscribe( listener ) {\n\t\t\t// Invalidate the value right after subscription was created. React will\n\t\t\t// call `getValue` after subscribing, to detect store updates that happened\n\t\t\t// in the interval between the `getValue` call during render and creating\n\t\t\t// the subscription, which is slightly delayed. We need to ensure that this\n\t\t\t// second `getValue` call will compute a fresh value.\n\t\t\tlastMapResultValid = false;\n\n\t\t\tconst onStoreChange = () => {\n\t\t\t\t// Invalidate the value on store update, so that a fresh value is computed.\n\t\t\t\tlastMapResultValid = false;\n\t\t\t\tlistener();\n\t\t\t};\n\n\t\t\tconst onChange = () => {\n\t\t\t\tif ( lastIsAsync ) {\n\t\t\t\t\trenderQueue.add( queueContext, onStoreChange );\n\t\t\t\t} else {\n\t\t\t\t\tonStoreChange();\n\t\t\t\t}\n\t\t\t};\n\n\t\t\tconst unsubs = [];\n\t\t\tfunction subscribeStore( storeName ) {\n\t\t\t\tunsubs.push( registry.subscribe( onChange, storeName ) );\n\t\t\t}\n\n\t\t\tfor ( const storeName of activeStores ) {\n\t\t\t\tsubscribeStore( storeName );\n\t\t\t}\n\n\t\t\tactiveSubscriptions.add( subscribeStore );\n\n\t\t\treturn () => {\n\t\t\t\tactiveSubscriptions.delete( subscribeStore );\n\n\t\t\t\tfor ( const unsub of unsubs.values() ) {\n\t\t\t\t\t// The return value of the subscribe function could be undefined if the store is a custom generic store.\n\t\t\t\t\tunsub?.();\n\t\t\t\t}\n\t\t\t\t// Cancel existing store updates that were already scheduled.\n\t\t\t\trenderQueue.cancel( queueContext );\n\t\t\t};\n\t\t}\n\n\t\t// Check if `newStores` contains some stores we're not subscribed to yet, and add them.\n\t\tfunction updateStores( newStores ) {\n\t\t\tfor ( const newStore of newStores ) {\n\t\t\t\tif ( activeStores.includes( newStore ) ) {\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\n\t\t\t\t// New `subscribe` calls will subscribe to `newStore`, too.\n\t\t\t\tactiveStores.push( newStore );\n\n\t\t\t\t// Add `newStore` to existing subscriptions.\n\t\t\t\tfor ( const subscription of activeSubscriptions ) {\n\t\t\t\t\tsubscription( newStore );\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn { subscribe, updateStores };\n\t};\n\n\treturn ( mapSelect, isAsync ) => {\n\t\tfunction updateValue() {\n\t\t\t// If the last value is valid, and the `mapSelect` callback hasn't changed,\n\t\t\t// then we can safely return the cached value. The value can change only on\n\t\t\t// store update, and in that case value will be invalidated by the listener.\n\t\t\tif ( lastMapResultValid && mapSelect === lastMapSelect ) {\n\t\t\t\treturn lastMapResult;\n\t\t\t}\n\n\t\t\tconst listeningStores = { current: null };\n\t\t\tconst mapResult = registry.__unstableMarkListeningStores(\n\t\t\t\t() => mapSelect( select, registry ),\n\t\t\t\tlisteningStores\n\t\t\t);\n\n\t\t\tif ( ! subscriber ) {\n\t\t\t\tsubscriber = createSubscriber( listeningStores.current );\n\t\t\t} else {\n\t\t\t\tsubscriber.updateStores( listeningStores.current );\n\t\t\t}\n\n\t\t\t// If the new value is shallow-equal to the old one, keep the old one so\n\t\t\t// that we don't trigger unwanted updates that do a `===` check.\n\t\t\tif ( ! isShallowEqual( lastMapResult, mapResult ) ) {\n\t\t\t\tlastMapResult = mapResult;\n\t\t\t}\n\t\t\tlastMapSelect = mapSelect;\n\t\t\tlastMapResultValid = true;\n\t\t}\n\n\t\tfunction getValue() {\n\t\t\t// Update the value in case it's been invalidated or `mapSelect` has changed.\n\t\t\tupdateValue();\n\t\t\treturn lastMapResult;\n\t\t}\n\n\t\t// When transitioning from async to sync mode, cancel existing store updates\n\t\t// that have been scheduled, and invalidate the value so that it's freshly\n\t\t// computed. It might have been changed by the update we just cancelled.\n\t\tif ( lastIsAsync && ! isAsync ) {\n\t\t\tlastMapResultValid = false;\n\t\t\trenderQueue.cancel( queueContext );\n\t\t}\n\n\t\tupdateValue();\n\n\t\tlastIsAsync = isAsync;\n\n\t\t// Return a pair of functions that can be passed to `useSyncExternalStore`.\n\t\treturn { subscribe: subscriber.subscribe, getValue };\n\t};\n}\n\nfunction useStaticSelect( storeName ) {\n\treturn useRegistry().select( storeName );\n}\n\nfunction useMappingSelect( suspense, mapSelect, deps ) {\n\tconst registry = useRegistry();\n\tconst isAsync = useAsyncMode();\n\tconst store = useMemo( () => Store( registry, suspense ), [ registry ] );\n\tconst selector = useCallback( mapSelect, deps );\n\tconst { subscribe, getValue } = store( selector, isAsync );\n\tconst result = useSyncExternalStore( subscribe, getValue, getValue );\n\tuseDebugValue( result );\n\treturn result;\n}\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 * @template {MapSelect | StoreDescriptor<any>} T\n * @param {T} mapSelect Function called on every state change. The returned value is\n * exposed to the component implementing this hook. The function\n * receives the `registry.select` method on the first argument\n * and the `registry` on the second argument.\n * When a store key is passed, all selectors for the store will be\n * returned. This is only meant for usage of these selectors in event\n * callbacks, not for data needed to create the element tree.\n * @param {unknown[]} deps If provided, this memoizes the mapSelect so the same `mapSelect` is\n * invoked on every state change unless the dependencies change.\n *\n * @example\n * ```js\n * import { useSelect } from '@wordpress/data';\n * import { store as myCustomStore } from 'my-custom-store';\n *\n * function HammerPriceDisplay( { currency } ) {\n * const price = useSelect( ( select ) => {\n * return select( myCustomStore ).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 * import { store as myCustomStore } from 'my-custom-store';\n *\n * function Paste( { children } ) {\n * const { getSettings } = useSelect( myCustomStore );\n * function onPaste() {\n * // Do something with the settings.\n * const settings = getSettings();\n * }\n * return <div onPaste={ onPaste }>{ children }</div>;\n * }\n * ```\n * @return {UseSelectReturn<T>} A custom react hook.\n */\nexport default function useSelect( mapSelect, deps ) {\n\t// On initial call, on mount, determine the mode of this `useSelect` call\n\t// and then never allow it to change on subsequent updates.\n\tconst staticSelectMode = typeof mapSelect !== 'function';\n\tconst staticSelectModeRef = useRef( staticSelectMode );\n\n\tif ( staticSelectMode !== staticSelectModeRef.current ) {\n\t\tconst prevMode = staticSelectModeRef.current ? 'static' : 'mapping';\n\t\tconst nextMode = staticSelectMode ? 'static' : 'mapping';\n\t\tthrow new Error(\n\t\t\t`Switching useSelect from ${ prevMode } to ${ nextMode } is not allowed`\n\t\t);\n\t}\n\n\t/* eslint-disable react-hooks/rules-of-hooks */\n\t// `staticSelectMode` is not allowed to change during the hook instance's,\n\t// lifetime, so the rules of hooks are not really violated.\n\treturn staticSelectMode\n\t\t? useStaticSelect( mapSelect )\n\t\t: useMappingSelect( false, mapSelect, deps );\n\t/* eslint-enable react-hooks/rules-of-hooks */\n}\n\n/**\n * A variant of the `useSelect` hook that has the same API, but will throw a\n * suspense Promise if any of the called selectors is in an unresolved state.\n *\n * @param {Function} mapSelect Function called on every state change. The\n * returned value is exposed to the component\n * using this hook. The function receives the\n * `registry.suspendSelect` method as the first\n * argument and the `registry` as the second one.\n * @param {Array} deps A dependency array used to memoize the `mapSelect`\n * so that the same `mapSelect` is invoked on every\n * state change unless the dependencies change.\n *\n * @return {Object} Data object returned by the `mapSelect` function.\n */\nexport function useSuspenseSelect( mapSelect, deps ) {\n\treturn useMappingSelect( true, mapSelect, deps );\n}\n"]}
@@ -226,12 +226,20 @@ export function createRegistry() {
226
226
  /**
227
227
  * Registers a store instance.
228
228
  *
229
- * @param {string} name Store registry name.
230
- * @param {Object} store Store instance object (getSelectors, getActions, subscribe).
229
+ * @param {string} name Store registry name.
230
+ * @param {Function} createStore Function that creates a store object (getSelectors, getActions, subscribe).
231
231
  */
232
232
 
233
233
 
234
- function registerStoreInstance(name, store) {
234
+ function registerStoreInstance(name, createStore) {
235
+ if (stores[name]) {
236
+ // eslint-disable-next-line no-console
237
+ console.error('Store "' + name + '" is already registered.');
238
+ return stores[name];
239
+ }
240
+
241
+ const store = createStore();
242
+
235
243
  if (typeof store.getSelectors !== 'function') {
236
244
  throw new TypeError('store.getSelectors must be a function');
237
245
  }
@@ -278,6 +286,8 @@ export function createRegistry() {
278
286
  // ignore it.
279
287
  }
280
288
  }
289
+
290
+ return store;
281
291
  }
282
292
  /**
283
293
  * Registers a new store given a store descriptor.
@@ -287,7 +297,7 @@ export function createRegistry() {
287
297
 
288
298
 
289
299
  function register(store) {
290
- registerStoreInstance(store.name, store.instantiate(registry));
300
+ registerStoreInstance(store.name, () => store.instantiate(registry));
291
301
  }
292
302
 
293
303
  function registerGenericStore(name, store) {
@@ -295,7 +305,7 @@ export function createRegistry() {
295
305
  since: '5.9',
296
306
  alternative: 'wp.data.register( storeDescriptor )'
297
307
  });
298
- registerStoreInstance(name, store);
308
+ registerStoreInstance(name, () => store);
299
309
  }
300
310
  /**
301
311
  * Registers a standard `@wordpress/data` store.
@@ -312,8 +322,7 @@ export function createRegistry() {
312
322
  throw new TypeError('Must specify store reducer');
313
323
  }
314
324
 
315
- const store = createReduxStore(storeName, options).instantiate(registry);
316
- registerStoreInstance(storeName, store);
325
+ const store = registerStoreInstance(storeName, () => createReduxStore(storeName, options).instantiate(registry));
317
326
  return store.store;
318
327
  }
319
328
 
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/data/src/registry.js"],"names":["deprecated","createReduxStore","coreDataStore","createEmitter","lock","unlock","getStoreName","storeNameOrDescriptor","name","createRegistry","storeConfigs","parent","stores","emitter","listeningStores","globalListener","emit","subscribe","listener","storeName","store","select","add","getSelectors","__unstableMarkListeningStores","callback","ref","Set","call","current","Array","from","resolveSelect","getResolveSelectors","suspendSelect","getSuspendSelectors","dispatch","getActions","withPlugins","attributes","Object","fromEntries","entries","map","key","attribute","registry","apply","arguments","registerStoreInstance","TypeError","currentSubscribe","unsubscribeFromEmitter","unsubscribeFromStore","isPaused","registerPrivateActions","privateActionsOf","registerPrivateSelectors","privateSelectorsOf","e","register","instantiate","registerGenericStore","since","alternative","registerStore","options","reducer","batch","pause","values","forEach","resume","namespaces","use","plugin","config","registryWithPlugins","privateActions","privateSelectors"],"mappings":"AAAA;AACA;AACA;AACA,OAAOA,UAAP,MAAuB,uBAAvB;AAEA;AACA;AACA;;AACA,OAAOC,gBAAP,MAA6B,eAA7B;AACA,OAAOC,aAAP,MAA0B,SAA1B;AACA,SAASC,aAAT,QAA8B,iBAA9B;AACA,SAASC,IAAT,EAAeC,MAAf,QAA6B,gBAA7B;AAEA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;;AAEA,SAASC,YAAT,CAAuBC,qBAAvB,EAA+C;AAC9C,SAAO,OAAOA,qBAAP,KAAiC,QAAjC,GACJA,qBADI,GAEJA,qBAAqB,CAACC,IAFzB;AAGA;AACD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACA,OAAO,SAASC,cAAT,GAA4D;AAAA,MAAnCC,YAAmC,uEAApB,EAAoB;AAAA,MAAhBC,MAAgB,uEAAP,IAAO;AAClE,QAAMC,MAAM,GAAG,EAAf;AACA,QAAMC,OAAO,GAAGV,aAAa,EAA7B;AACA,MAAIW,eAAe,GAAG,IAAtB;AAEA;AACD;AACA;;AACC,WAASC,cAAT,GAA0B;AACzBF,IAAAA,OAAO,CAACG,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EAAsBT,OAAtB,EAAgC;AAC/B,QAAK,CAAES,MAAP,EAAgB;AACf;AACA;;AAED7B,IAAAA,QAAQ,GAAG,EACV,GAAGA,QADO;AAEV,SAAG6B,MAAM,CAAE7B,QAAF,EAAYoB,OAAZ;AAFC,KAAX;AAKA,WAAOpB,QAAP;AACA;;AAEDA,EAAAA,QAAQ,CAACc,QAAT,CAAmB1D,aAAnB;;AAEA,OAAM,MAAM,CAAEM,IAAF,EAAQoE,MAAR,CAAZ,IAAgCpC,MAAM,CAACE,OAAP,CAAgBhC,YAAhB,CAAhC,EAAiE;AAChEoC,IAAAA,QAAQ,CAACc,QAAT,CAAmB3D,gBAAgB,CAAEO,IAAF,EAAQoE,MAAR,CAAnC;AACA;;AAED,MAAKjE,MAAL,EAAc;AACbA,IAAAA,MAAM,CAACM,SAAP,CAAkBF,cAAlB;AACA;;AAED,QAAM8D,mBAAmB,GAAGvC,WAAW,CAAEQ,QAAF,CAAvC;AACA1C,EAAAA,IAAI,CAAEyE,mBAAF,EAAuB;AAC1BrB,IAAAA,gBAAgB,EAAIhD,IAAF,IAAY;AAC7B,UAAI;AACH,eAAOH,MAAM,CAAEO,MAAM,CAAEJ,IAAF,CAAN,CAAeY,KAAjB,CAAN,CAA+B0D,cAAtC;AACA,OAFD,CAEE,OAAQnB,CAAR,EAAY;AACb;AACA;AACA,eAAO,EAAP;AACA;AACD,KATyB;AAU1BD,IAAAA,kBAAkB,EAAIlD,IAAF,IAAY;AAC/B,UAAI;AACH,eAAOH,MAAM,CAAEO,MAAM,CAAEJ,IAAF,CAAN,CAAeY,KAAjB,CAAN,CAA+B2D,gBAAtC;AACA,OAFD,CAEE,OAAQpB,CAAR,EAAY;AACb,eAAO,EAAP;AACA;AACD;AAhByB,GAAvB,CAAJ;AAkBA,SAAOkB,mBAAP;AACA","sourcesContent":["/**\n * WordPress dependencies\n */\nimport deprecated from '@wordpress/deprecated';\n\n/**\n * Internal dependencies\n */\nimport createReduxStore from './redux-store';\nimport coreDataStore from './store';\nimport { createEmitter } from './utils/emitter';\nimport { lock, unlock } from './private-apis';\n\n/** @typedef {import('./types').StoreDescriptor} StoreDescriptor */\n\n/**\n * @typedef {Object} WPDataRegistry An isolated orchestrator of store registrations.\n *\n * @property {Function} registerGenericStore Given a namespace key and settings\n * object, registers a new generic\n * store.\n * @property {Function} registerStore Given a namespace key and settings\n * object, registers a new namespace\n * store.\n * @property {Function} subscribe Given a function callback, invokes\n * the callback on any change to state\n * within any registered store.\n * @property {Function} select Given a namespace key, returns an\n * object of the store's registered\n * selectors.\n * @property {Function} dispatch Given a namespace key, returns an\n * object of the store's registered\n * action dispatchers.\n */\n\n/**\n * @typedef {Object} WPDataPlugin An object of registry function overrides.\n *\n * @property {Function} registerStore registers store.\n */\n\nfunction getStoreName( storeNameOrDescriptor ) {\n\treturn typeof storeNameOrDescriptor === 'string'\n\t\t? storeNameOrDescriptor\n\t\t: storeNameOrDescriptor.name;\n}\n/**\n * Creates a new store registry, given an optional object of initial store\n * configurations.\n *\n * @param {Object} storeConfigs Initial store configurations.\n * @param {Object?} parent Parent registry.\n *\n * @return {WPDataRegistry} Data registry.\n */\nexport function createRegistry( storeConfigs = {}, parent = null ) {\n\tconst stores = {};\n\tconst emitter = createEmitter();\n\tlet listeningStores = null;\n\n\t/**\n\t * Global listener called for each store's update.\n\t */\n\tfunction globalListener() {\n\t\temitter.emit();\n\t}\n\n\t/**\n\t * Subscribe to changes to any data, either in all stores in registry, or\n\t * in one specific store.\n\t *\n\t * @param {Function} listener Listener function.\n\t * @param {string|StoreDescriptor?} storeNameOrDescriptor Optional store name.\n\t *\n\t * @return {Function} Unsubscribe function.\n\t */\n\tconst subscribe = ( listener, storeNameOrDescriptor ) => {\n\t\t// subscribe to all stores\n\t\tif ( ! storeNameOrDescriptor ) {\n\t\t\treturn emitter.subscribe( listener );\n\t\t}\n\n\t\t// subscribe to one store\n\t\tconst storeName = getStoreName( storeNameOrDescriptor );\n\t\tconst store = stores[ storeName ];\n\t\tif ( store ) {\n\t\t\treturn store.subscribe( listener );\n\t\t}\n\n\t\t// Trying to access a store that hasn't been registered,\n\t\t// this is a pattern rarely used but seen in some places.\n\t\t// We fallback to global `subscribe` here for backward-compatibility for now.\n\t\t// See https://github.com/WordPress/gutenberg/pull/27466 for more info.\n\t\tif ( ! parent ) {\n\t\t\treturn emitter.subscribe( listener );\n\t\t}\n\n\t\treturn parent.subscribe( listener, storeNameOrDescriptor );\n\t};\n\n\t/**\n\t * Calls a selector given the current state and extra arguments.\n\t *\n\t * @param {string|StoreDescriptor} storeNameOrDescriptor Unique namespace identifier for the store\n\t * or the store descriptor.\n\t *\n\t * @return {*} The selector's returned value.\n\t */\n\tfunction select( storeNameOrDescriptor ) {\n\t\tconst storeName = getStoreName( storeNameOrDescriptor );\n\t\tlisteningStores?.add( storeName );\n\t\tconst store = stores[ storeName ];\n\t\tif ( store ) {\n\t\t\treturn store.getSelectors();\n\t\t}\n\n\t\treturn parent?.select( storeName );\n\t}\n\n\tfunction __unstableMarkListeningStores( callback, ref ) {\n\t\tlisteningStores = new Set();\n\t\ttry {\n\t\t\treturn callback.call( this );\n\t\t} finally {\n\t\t\tref.current = Array.from( listeningStores );\n\t\t\tlisteningStores = null;\n\t\t}\n\t}\n\n\t/**\n\t * Given a store descriptor, returns an object containing the store's selectors pre-bound to\n\t * state so that you only need to supply additional arguments, and modified so that they return\n\t * promises that resolve to their eventual values, after any resolvers have ran.\n\t *\n\t * @param {StoreDescriptor|string} storeNameOrDescriptor The store descriptor. The legacy calling\n\t * convention of passing the store name is\n\t * also supported.\n\t *\n\t * @return {Object} Each key of the object matches the name of a selector.\n\t */\n\tfunction resolveSelect( storeNameOrDescriptor ) {\n\t\tconst storeName = getStoreName( storeNameOrDescriptor );\n\t\tlisteningStores?.add( storeName );\n\t\tconst store = stores[ storeName ];\n\t\tif ( store ) {\n\t\t\treturn store.getResolveSelectors();\n\t\t}\n\n\t\treturn parent && parent.resolveSelect( storeName );\n\t}\n\n\t/**\n\t * Given a store descriptor, returns an object containing the store's selectors pre-bound to\n\t * state so that you only need to supply additional arguments, and modified so that they throw\n\t * promises in case the selector is not resolved yet.\n\t *\n\t * @param {StoreDescriptor|string} storeNameOrDescriptor The store descriptor. The legacy calling\n\t * convention of passing the store name is\n\t * also supported.\n\t *\n\t * @return {Object} Object containing the store's suspense-wrapped selectors.\n\t */\n\tfunction suspendSelect( storeNameOrDescriptor ) {\n\t\tconst storeName = getStoreName( storeNameOrDescriptor );\n\t\tlisteningStores?.add( storeName );\n\t\tconst store = stores[ storeName ];\n\t\tif ( store ) {\n\t\t\treturn store.getSuspendSelectors();\n\t\t}\n\n\t\treturn parent && parent.suspendSelect( storeName );\n\t}\n\n\t/**\n\t * Returns the available actions for a part of the state.\n\t *\n\t * @param {string|StoreDescriptor} storeNameOrDescriptor Unique namespace identifier for the store\n\t * or the store descriptor.\n\t *\n\t * @return {*} The action's returned value.\n\t */\n\tfunction dispatch( storeNameOrDescriptor ) {\n\t\tconst storeName = getStoreName( storeNameOrDescriptor );\n\t\tconst store = stores[ storeName ];\n\t\tif ( store ) {\n\t\t\treturn store.getActions();\n\t\t}\n\n\t\treturn parent && parent.dispatch( storeName );\n\t}\n\n\t//\n\t// Deprecated\n\t// TODO: Remove this after `use()` is removed.\n\tfunction withPlugins( attributes ) {\n\t\treturn Object.fromEntries(\n\t\t\tObject.entries( attributes ).map( ( [ key, attribute ] ) => {\n\t\t\t\tif ( typeof attribute !== 'function' ) {\n\t\t\t\t\treturn [ key, attribute ];\n\t\t\t\t}\n\t\t\t\treturn [\n\t\t\t\t\tkey,\n\t\t\t\t\tfunction () {\n\t\t\t\t\t\treturn registry[ key ].apply( null, arguments );\n\t\t\t\t\t},\n\t\t\t\t];\n\t\t\t} )\n\t\t);\n\t}\n\n\t/**\n\t * Registers a store instance.\n\t *\n\t * @param {string} name Store registry name.\n\t * @param {Object} store Store instance object (getSelectors, getActions, subscribe).\n\t */\n\tfunction registerStoreInstance( name, store ) {\n\t\tif ( typeof store.getSelectors !== 'function' ) {\n\t\t\tthrow new TypeError( 'store.getSelectors must be a function' );\n\t\t}\n\t\tif ( typeof store.getActions !== 'function' ) {\n\t\t\tthrow new TypeError( 'store.getActions must be a function' );\n\t\t}\n\t\tif ( typeof store.subscribe !== 'function' ) {\n\t\t\tthrow new TypeError( 'store.subscribe must be a function' );\n\t\t}\n\t\t// The emitter is used to keep track of active listeners when the registry\n\t\t// get paused, that way, when resumed we should be able to call all these\n\t\t// pending listeners.\n\t\tstore.emitter = createEmitter();\n\t\tconst currentSubscribe = store.subscribe;\n\t\tstore.subscribe = ( listener ) => {\n\t\t\tconst unsubscribeFromEmitter = store.emitter.subscribe( listener );\n\t\t\tconst unsubscribeFromStore = currentSubscribe( () => {\n\t\t\t\tif ( store.emitter.isPaused ) {\n\t\t\t\t\tstore.emitter.emit();\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tlistener();\n\t\t\t} );\n\n\t\t\treturn () => {\n\t\t\t\tunsubscribeFromStore?.();\n\t\t\t\tunsubscribeFromEmitter?.();\n\t\t\t};\n\t\t};\n\t\tstores[ name ] = store;\n\t\tstore.subscribe( globalListener );\n\n\t\t// Copy private actions and selectors from the parent store.\n\t\tif ( parent ) {\n\t\t\ttry {\n\t\t\t\tunlock( store.store ).registerPrivateActions(\n\t\t\t\t\tunlock( parent ).privateActionsOf( name )\n\t\t\t\t);\n\t\t\t\tunlock( store.store ).registerPrivateSelectors(\n\t\t\t\t\tunlock( parent ).privateSelectorsOf( name )\n\t\t\t\t);\n\t\t\t} catch ( e ) {\n\t\t\t\t// unlock() throws if store.store was not locked.\n\t\t\t\t// The error indicates there's nothing to do here so let's\n\t\t\t\t// ignore it.\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Registers a new store given a store descriptor.\n\t *\n\t * @param {StoreDescriptor} store Store descriptor.\n\t */\n\tfunction register( store ) {\n\t\tregisterStoreInstance( store.name, store.instantiate( registry ) );\n\t}\n\n\tfunction registerGenericStore( name, store ) {\n\t\tdeprecated( 'wp.data.registerGenericStore', {\n\t\t\tsince: '5.9',\n\t\t\talternative: 'wp.data.register( storeDescriptor )',\n\t\t} );\n\t\tregisterStoreInstance( name, store );\n\t}\n\n\t/**\n\t * Registers a standard `@wordpress/data` store.\n\t *\n\t * @param {string} storeName Unique namespace identifier.\n\t * @param {Object} options Store description (reducer, actions, selectors, resolvers).\n\t *\n\t * @return {Object} Registered store object.\n\t */\n\tfunction registerStore( storeName, options ) {\n\t\tif ( ! options.reducer ) {\n\t\t\tthrow new TypeError( 'Must specify store reducer' );\n\t\t}\n\n\t\tconst store = createReduxStore( storeName, options ).instantiate(\n\t\t\tregistry\n\t\t);\n\t\tregisterStoreInstance( storeName, store );\n\t\treturn store.store;\n\t}\n\n\tfunction batch( callback ) {\n\t\temitter.pause();\n\t\tObject.values( stores ).forEach( ( store ) => store.emitter.pause() );\n\t\tcallback();\n\t\temitter.resume();\n\t\tObject.values( stores ).forEach( ( store ) => store.emitter.resume() );\n\t}\n\n\tlet registry = {\n\t\tbatch,\n\t\tstores,\n\t\tnamespaces: stores, // TODO: Deprecate/remove this.\n\t\tsubscribe,\n\t\tselect,\n\t\tresolveSelect,\n\t\tsuspendSelect,\n\t\tdispatch,\n\t\tuse,\n\t\tregister,\n\t\tregisterGenericStore,\n\t\tregisterStore,\n\t\t__unstableMarkListeningStores,\n\t};\n\n\t//\n\t// TODO:\n\t// This function will be deprecated as soon as it is no longer internally referenced.\n\tfunction use( plugin, options ) {\n\t\tif ( ! plugin ) {\n\t\t\treturn;\n\t\t}\n\n\t\tregistry = {\n\t\t\t...registry,\n\t\t\t...plugin( registry, options ),\n\t\t};\n\n\t\treturn registry;\n\t}\n\n\tregistry.register( coreDataStore );\n\n\tfor ( const [ name, config ] of Object.entries( storeConfigs ) ) {\n\t\tregistry.register( createReduxStore( name, config ) );\n\t}\n\n\tif ( parent ) {\n\t\tparent.subscribe( globalListener );\n\t}\n\n\tconst registryWithPlugins = withPlugins( registry );\n\tlock( registryWithPlugins, {\n\t\tprivateActionsOf: ( name ) => {\n\t\t\ttry {\n\t\t\t\treturn unlock( stores[ name ].store ).privateActions;\n\t\t\t} catch ( e ) {\n\t\t\t\t// unlock() throws an error the store was not locked – this means\n\t\t\t\t// there no private actions are available\n\t\t\t\treturn {};\n\t\t\t}\n\t\t},\n\t\tprivateSelectorsOf: ( name ) => {\n\t\t\ttry {\n\t\t\t\treturn unlock( stores[ name ].store ).privateSelectors;\n\t\t\t} catch ( e ) {\n\t\t\t\treturn {};\n\t\t\t}\n\t\t},\n\t} );\n\treturn registryWithPlugins;\n}\n"]}
1
+ {"version":3,"sources":["@wordpress/data/src/registry.js"],"names":["deprecated","createReduxStore","coreDataStore","createEmitter","lock","unlock","getStoreName","storeNameOrDescriptor","name","createRegistry","storeConfigs","parent","stores","emitter","listeningStores","globalListener","emit","subscribe","listener","storeName","store","select","add","getSelectors","__unstableMarkListeningStores","callback","ref","Set","call","current","Array","from","resolveSelect","getResolveSelectors","suspendSelect","getSuspendSelectors","dispatch","getActions","withPlugins","attributes","Object","fromEntries","entries","map","key","attribute","registry","apply","arguments","registerStoreInstance","createStore","console","error","TypeError","currentSubscribe","unsubscribeFromEmitter","unsubscribeFromStore","isPaused","registerPrivateActions","privateActionsOf","registerPrivateSelectors","privateSelectorsOf","e","register","instantiate","registerGenericStore","since","alternative","registerStore","options","reducer","batch","pause","values","forEach","resume","namespaces","use","plugin","config","registryWithPlugins","privateActions","privateSelectors"],"mappings":"AAAA;AACA;AACA;AACA,OAAOA,UAAP,MAAuB,uBAAvB;AAEA;AACA;AACA;;AACA,OAAOC,gBAAP,MAA6B,eAA7B;AACA,OAAOC,aAAP,MAA0B,SAA1B;AACA,SAASC,aAAT,QAA8B,iBAA9B;AACA,SAASC,IAAT,EAAeC,MAAf,QAA6B,gBAA7B;AAEA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AAC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,CAAkBF,cAAlB;AACA;;AAED,QAAMiE,mBAAmB,GAAG1C,WAAW,CAAEQ,QAAF,CAAvC;AACA1C,EAAAA,IAAI,CAAE4E,mBAAF,EAAuB;AAC1BrB,IAAAA,gBAAgB,EAAInD,IAAF,IAAY;AAC7B,UAAI;AACH,eAAOH,MAAM,CAAEO,MAAM,CAAEJ,IAAF,CAAN,CAAeY,KAAjB,CAAN,CAA+B6D,cAAtC;AACA,OAFD,CAEE,OAAQnB,CAAR,EAAY;AACb;AACA;AACA,eAAO,EAAP;AACA;AACD,KATyB;AAU1BD,IAAAA,kBAAkB,EAAIrD,IAAF,IAAY;AAC/B,UAAI;AACH,eAAOH,MAAM,CAAEO,MAAM,CAAEJ,IAAF,CAAN,CAAeY,KAAjB,CAAN,CAA+B8D,gBAAtC;AACA,OAFD,CAEE,OAAQpB,CAAR,EAAY;AACb,eAAO,EAAP;AACA;AACD;AAhByB,GAAvB,CAAJ;AAkBA,SAAOkB,mBAAP;AACA","sourcesContent":["/**\n * WordPress dependencies\n */\nimport deprecated from '@wordpress/deprecated';\n\n/**\n * Internal dependencies\n */\nimport createReduxStore from './redux-store';\nimport coreDataStore from './store';\nimport { createEmitter } from './utils/emitter';\nimport { lock, unlock } from './private-apis';\n\n/** @typedef {import('./types').StoreDescriptor} StoreDescriptor */\n\n/**\n * @typedef {Object} WPDataRegistry An isolated orchestrator of store registrations.\n *\n * @property {Function} registerGenericStore Given a namespace key and settings\n * object, registers a new generic\n * store.\n * @property {Function} registerStore Given a namespace key and settings\n * object, registers a new namespace\n * store.\n * @property {Function} subscribe Given a function callback, invokes\n * the callback on any change to state\n * within any registered store.\n * @property {Function} select Given a namespace key, returns an\n * object of the store's registered\n * selectors.\n * @property {Function} dispatch Given a namespace key, returns an\n * object of the store's registered\n * action dispatchers.\n */\n\n/**\n * @typedef {Object} WPDataPlugin An object of registry function overrides.\n *\n * @property {Function} registerStore registers store.\n */\n\nfunction getStoreName( storeNameOrDescriptor ) {\n\treturn typeof storeNameOrDescriptor === 'string'\n\t\t? storeNameOrDescriptor\n\t\t: storeNameOrDescriptor.name;\n}\n/**\n * Creates a new store registry, given an optional object of initial store\n * configurations.\n *\n * @param {Object} storeConfigs Initial store configurations.\n * @param {Object?} parent Parent registry.\n *\n * @return {WPDataRegistry} Data registry.\n */\nexport function createRegistry( storeConfigs = {}, parent = null ) {\n\tconst stores = {};\n\tconst emitter = createEmitter();\n\tlet listeningStores = null;\n\n\t/**\n\t * Global listener called for each store's update.\n\t */\n\tfunction globalListener() {\n\t\temitter.emit();\n\t}\n\n\t/**\n\t * Subscribe to changes to any data, either in all stores in registry, or\n\t * in one specific store.\n\t *\n\t * @param {Function} listener Listener function.\n\t * @param {string|StoreDescriptor?} storeNameOrDescriptor Optional store name.\n\t *\n\t * @return {Function} Unsubscribe function.\n\t */\n\tconst subscribe = ( listener, storeNameOrDescriptor ) => {\n\t\t// subscribe to all stores\n\t\tif ( ! storeNameOrDescriptor ) {\n\t\t\treturn emitter.subscribe( listener );\n\t\t}\n\n\t\t// subscribe to one store\n\t\tconst storeName = getStoreName( storeNameOrDescriptor );\n\t\tconst store = stores[ storeName ];\n\t\tif ( store ) {\n\t\t\treturn store.subscribe( listener );\n\t\t}\n\n\t\t// Trying to access a store that hasn't been registered,\n\t\t// this is a pattern rarely used but seen in some places.\n\t\t// We fallback to global `subscribe` here for backward-compatibility for now.\n\t\t// See https://github.com/WordPress/gutenberg/pull/27466 for more info.\n\t\tif ( ! parent ) {\n\t\t\treturn emitter.subscribe( listener );\n\t\t}\n\n\t\treturn parent.subscribe( listener, storeNameOrDescriptor );\n\t};\n\n\t/**\n\t * Calls a selector given the current state and extra arguments.\n\t *\n\t * @param {string|StoreDescriptor} storeNameOrDescriptor Unique namespace identifier for the store\n\t * or the store descriptor.\n\t *\n\t * @return {*} The selector's returned value.\n\t */\n\tfunction select( storeNameOrDescriptor ) {\n\t\tconst storeName = getStoreName( storeNameOrDescriptor );\n\t\tlisteningStores?.add( storeName );\n\t\tconst store = stores[ storeName ];\n\t\tif ( store ) {\n\t\t\treturn store.getSelectors();\n\t\t}\n\n\t\treturn parent?.select( storeName );\n\t}\n\n\tfunction __unstableMarkListeningStores( callback, ref ) {\n\t\tlisteningStores = new Set();\n\t\ttry {\n\t\t\treturn callback.call( this );\n\t\t} finally {\n\t\t\tref.current = Array.from( listeningStores );\n\t\t\tlisteningStores = null;\n\t\t}\n\t}\n\n\t/**\n\t * Given a store descriptor, returns an object containing the store's selectors pre-bound to\n\t * state so that you only need to supply additional arguments, and modified so that they return\n\t * promises that resolve to their eventual values, after any resolvers have ran.\n\t *\n\t * @param {StoreDescriptor|string} storeNameOrDescriptor The store descriptor. The legacy calling\n\t * convention of passing the store name is\n\t * also supported.\n\t *\n\t * @return {Object} Each key of the object matches the name of a selector.\n\t */\n\tfunction resolveSelect( storeNameOrDescriptor ) {\n\t\tconst storeName = getStoreName( storeNameOrDescriptor );\n\t\tlisteningStores?.add( storeName );\n\t\tconst store = stores[ storeName ];\n\t\tif ( store ) {\n\t\t\treturn store.getResolveSelectors();\n\t\t}\n\n\t\treturn parent && parent.resolveSelect( storeName );\n\t}\n\n\t/**\n\t * Given a store descriptor, returns an object containing the store's selectors pre-bound to\n\t * state so that you only need to supply additional arguments, and modified so that they throw\n\t * promises in case the selector is not resolved yet.\n\t *\n\t * @param {StoreDescriptor|string} storeNameOrDescriptor The store descriptor. The legacy calling\n\t * convention of passing the store name is\n\t * also supported.\n\t *\n\t * @return {Object} Object containing the store's suspense-wrapped selectors.\n\t */\n\tfunction suspendSelect( storeNameOrDescriptor ) {\n\t\tconst storeName = getStoreName( storeNameOrDescriptor );\n\t\tlisteningStores?.add( storeName );\n\t\tconst store = stores[ storeName ];\n\t\tif ( store ) {\n\t\t\treturn store.getSuspendSelectors();\n\t\t}\n\n\t\treturn parent && parent.suspendSelect( storeName );\n\t}\n\n\t/**\n\t * Returns the available actions for a part of the state.\n\t *\n\t * @param {string|StoreDescriptor} storeNameOrDescriptor Unique namespace identifier for the store\n\t * or the store descriptor.\n\t *\n\t * @return {*} The action's returned value.\n\t */\n\tfunction dispatch( storeNameOrDescriptor ) {\n\t\tconst storeName = getStoreName( storeNameOrDescriptor );\n\t\tconst store = stores[ storeName ];\n\t\tif ( store ) {\n\t\t\treturn store.getActions();\n\t\t}\n\n\t\treturn parent && parent.dispatch( storeName );\n\t}\n\n\t//\n\t// Deprecated\n\t// TODO: Remove this after `use()` is removed.\n\tfunction withPlugins( attributes ) {\n\t\treturn Object.fromEntries(\n\t\t\tObject.entries( attributes ).map( ( [ key, attribute ] ) => {\n\t\t\t\tif ( typeof attribute !== 'function' ) {\n\t\t\t\t\treturn [ key, attribute ];\n\t\t\t\t}\n\t\t\t\treturn [\n\t\t\t\t\tkey,\n\t\t\t\t\tfunction () {\n\t\t\t\t\t\treturn registry[ key ].apply( null, arguments );\n\t\t\t\t\t},\n\t\t\t\t];\n\t\t\t} )\n\t\t);\n\t}\n\n\t/**\n\t * Registers a store instance.\n\t *\n\t * @param {string} name Store registry name.\n\t * @param {Function} createStore Function that creates a store object (getSelectors, getActions, subscribe).\n\t */\n\tfunction registerStoreInstance( name, createStore ) {\n\t\tif ( stores[ name ] ) {\n\t\t\t// eslint-disable-next-line no-console\n\t\t\tconsole.error( 'Store \"' + name + '\" is already registered.' );\n\t\t\treturn stores[ name ];\n\t\t}\n\n\t\tconst store = createStore();\n\n\t\tif ( typeof store.getSelectors !== 'function' ) {\n\t\t\tthrow new TypeError( 'store.getSelectors must be a function' );\n\t\t}\n\t\tif ( typeof store.getActions !== 'function' ) {\n\t\t\tthrow new TypeError( 'store.getActions must be a function' );\n\t\t}\n\t\tif ( typeof store.subscribe !== 'function' ) {\n\t\t\tthrow new TypeError( 'store.subscribe must be a function' );\n\t\t}\n\t\t// The emitter is used to keep track of active listeners when the registry\n\t\t// get paused, that way, when resumed we should be able to call all these\n\t\t// pending listeners.\n\t\tstore.emitter = createEmitter();\n\t\tconst currentSubscribe = store.subscribe;\n\t\tstore.subscribe = ( listener ) => {\n\t\t\tconst unsubscribeFromEmitter = store.emitter.subscribe( listener );\n\t\t\tconst unsubscribeFromStore = currentSubscribe( () => {\n\t\t\t\tif ( store.emitter.isPaused ) {\n\t\t\t\t\tstore.emitter.emit();\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tlistener();\n\t\t\t} );\n\n\t\t\treturn () => {\n\t\t\t\tunsubscribeFromStore?.();\n\t\t\t\tunsubscribeFromEmitter?.();\n\t\t\t};\n\t\t};\n\t\tstores[ name ] = store;\n\t\tstore.subscribe( globalListener );\n\n\t\t// Copy private actions and selectors from the parent store.\n\t\tif ( parent ) {\n\t\t\ttry {\n\t\t\t\tunlock( store.store ).registerPrivateActions(\n\t\t\t\t\tunlock( parent ).privateActionsOf( name )\n\t\t\t\t);\n\t\t\t\tunlock( store.store ).registerPrivateSelectors(\n\t\t\t\t\tunlock( parent ).privateSelectorsOf( name )\n\t\t\t\t);\n\t\t\t} catch ( e ) {\n\t\t\t\t// unlock() throws if store.store was not locked.\n\t\t\t\t// The error indicates there's nothing to do here so let's\n\t\t\t\t// ignore it.\n\t\t\t}\n\t\t}\n\n\t\treturn store;\n\t}\n\n\t/**\n\t * Registers a new store given a store descriptor.\n\t *\n\t * @param {StoreDescriptor} store Store descriptor.\n\t */\n\tfunction register( store ) {\n\t\tregisterStoreInstance( store.name, () =>\n\t\t\tstore.instantiate( registry )\n\t\t);\n\t}\n\n\tfunction registerGenericStore( name, store ) {\n\t\tdeprecated( 'wp.data.registerGenericStore', {\n\t\t\tsince: '5.9',\n\t\t\talternative: 'wp.data.register( storeDescriptor )',\n\t\t} );\n\t\tregisterStoreInstance( name, () => store );\n\t}\n\n\t/**\n\t * Registers a standard `@wordpress/data` store.\n\t *\n\t * @param {string} storeName Unique namespace identifier.\n\t * @param {Object} options Store description (reducer, actions, selectors, resolvers).\n\t *\n\t * @return {Object} Registered store object.\n\t */\n\tfunction registerStore( storeName, options ) {\n\t\tif ( ! options.reducer ) {\n\t\t\tthrow new TypeError( 'Must specify store reducer' );\n\t\t}\n\n\t\tconst store = registerStoreInstance( storeName, () =>\n\t\t\tcreateReduxStore( storeName, options ).instantiate( registry )\n\t\t);\n\n\t\treturn store.store;\n\t}\n\n\tfunction batch( callback ) {\n\t\temitter.pause();\n\t\tObject.values( stores ).forEach( ( store ) => store.emitter.pause() );\n\t\tcallback();\n\t\temitter.resume();\n\t\tObject.values( stores ).forEach( ( store ) => store.emitter.resume() );\n\t}\n\n\tlet registry = {\n\t\tbatch,\n\t\tstores,\n\t\tnamespaces: stores, // TODO: Deprecate/remove this.\n\t\tsubscribe,\n\t\tselect,\n\t\tresolveSelect,\n\t\tsuspendSelect,\n\t\tdispatch,\n\t\tuse,\n\t\tregister,\n\t\tregisterGenericStore,\n\t\tregisterStore,\n\t\t__unstableMarkListeningStores,\n\t};\n\n\t//\n\t// TODO:\n\t// This function will be deprecated as soon as it is no longer internally referenced.\n\tfunction use( plugin, options ) {\n\t\tif ( ! plugin ) {\n\t\t\treturn;\n\t\t}\n\n\t\tregistry = {\n\t\t\t...registry,\n\t\t\t...plugin( registry, options ),\n\t\t};\n\n\t\treturn registry;\n\t}\n\n\tregistry.register( coreDataStore );\n\n\tfor ( const [ name, config ] of Object.entries( storeConfigs ) ) {\n\t\tregistry.register( createReduxStore( name, config ) );\n\t}\n\n\tif ( parent ) {\n\t\tparent.subscribe( globalListener );\n\t}\n\n\tconst registryWithPlugins = withPlugins( registry );\n\tlock( registryWithPlugins, {\n\t\tprivateActionsOf: ( name ) => {\n\t\t\ttry {\n\t\t\t\treturn unlock( stores[ name ].store ).privateActions;\n\t\t\t} catch ( e ) {\n\t\t\t\t// unlock() throws an error the store was not locked – this means\n\t\t\t\t// there no private actions are available\n\t\t\t\treturn {};\n\t\t\t}\n\t\t},\n\t\tprivateSelectorsOf: ( name ) => {\n\t\t\ttry {\n\t\t\t\treturn unlock( stores[ name ].store ).privateSelectors;\n\t\t\t} catch ( e ) {\n\t\t\t\treturn {};\n\t\t\t}\n\t\t},\n\t} );\n\treturn registryWithPlugins;\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/components/use-select/index.js"],"names":[],"mappings":"AA+JA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+DG;AACH,8IAlDW,OAAO,EAAE,4CAuEnB;AAED;;;;;;;;;;;;;;GAcG;AACH,qEAFY,MAAM,CAIjB;yEAjPY,OAAO,aAAa,EAAE,eAAe,CAAC,CAAC,CAAC;sHAIxC,OAAO,aAAa,EAAE,gBAAgB,CAAC,KAAK,EAAC,OAAO,EAAC,SAAS,CAAC;wBAK9D,OAAO,aAAa,EAAE,SAAS;sHAEhC,OAAO,aAAa,EAAE,eAAe,CAAC,CAAC,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/components/use-select/index.js"],"names":[],"mappings":"AA6LA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+DG;AACH,8IAlDW,OAAO,EAAE,4CAuEnB;AAED;;;;;;;;;;;;;;GAcG;AACH,qEAFY,MAAM,CAIjB;yEA/QY,OAAO,aAAa,EAAE,eAAe,CAAC,CAAC,CAAC;sHAIxC,OAAO,aAAa,EAAE,gBAAgB,CAAC,KAAK,EAAC,OAAO,EAAC,SAAS,CAAC;wBAK9D,OAAO,aAAa,EAAE,SAAS;sHAEhC,OAAO,aAAa,EAAE,eAAe,CAAC,CAAC,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../src/registry.js"],"names":[],"mappings":"AA8CA;;;;;;;;GAQG;AACH,8CALW,MAAM,WACN,MAAM,UAEL,cAAc,CAgUzB"}
1
+ {"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../src/registry.js"],"names":[],"mappings":"AA8CA;;;;;;;;GAQG;AACH,8CALW,MAAM,WACN,MAAM,UAEL,cAAc,CA4UzB"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wordpress/data",
3
- "version": "8.6.0",
3
+ "version": "9.0.0",
4
4
  "description": "Data module for WordPress.",
5
5
  "author": "The WordPress Contributors",
6
6
  "license": "GPL-2.0-or-later",
@@ -29,13 +29,13 @@
29
29
  "sideEffects": false,
30
30
  "dependencies": {
31
31
  "@babel/runtime": "^7.16.0",
32
- "@wordpress/compose": "^6.6.0",
33
- "@wordpress/deprecated": "^3.29.0",
34
- "@wordpress/element": "^5.6.0",
35
- "@wordpress/is-shallow-equal": "^4.29.0",
36
- "@wordpress/priority-queue": "^2.29.0",
37
- "@wordpress/private-apis": "^0.11.0",
38
- "@wordpress/redux-routine": "^4.29.0",
32
+ "@wordpress/compose": "^6.7.0",
33
+ "@wordpress/deprecated": "^3.30.0",
34
+ "@wordpress/element": "^5.7.0",
35
+ "@wordpress/is-shallow-equal": "^4.30.0",
36
+ "@wordpress/priority-queue": "^2.30.0",
37
+ "@wordpress/private-apis": "^0.12.0",
38
+ "@wordpress/redux-routine": "^4.30.0",
39
39
  "deepmerge": "^4.3.0",
40
40
  "equivalent-key-map": "^0.2.2",
41
41
  "is-plain-object": "^5.0.0",
@@ -50,5 +50,5 @@
50
50
  "publishConfig": {
51
51
  "access": "public"
52
52
  },
53
- "gitHead": "9534a7b3bbf07c1d40b94fdb7a3d091f297bfb06"
53
+ "gitHead": "d5c28a67b11e91e3e4b8e90346bfcb90909364d6"
54
54
  }