@wordpress/data 7.6.0 → 8.1.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.
@@ -42,8 +42,8 @@ import { createEmitter } from './utils/emitter';
42
42
  * @property {Function} registerStore registers store.
43
43
  */
44
44
 
45
- function isObject(object) {
46
- return object !== null && typeof object === 'object';
45
+ function getStoreName(storeNameOrDescriptor) {
46
+ return typeof storeNameOrDescriptor === 'string' ? storeNameOrDescriptor : storeNameOrDescriptor.name;
47
47
  }
48
48
  /**
49
49
  * Creates a new store registry, given an optional object of initial store
@@ -61,7 +61,7 @@ export function createRegistry() {
61
61
  let parent = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
62
62
  const stores = {};
63
63
  const emitter = createEmitter();
64
- const listeningStores = new Set();
64
+ let listeningStores = null;
65
65
  /**
66
66
  * Global listener called for each store's update.
67
67
  */
@@ -70,16 +70,39 @@ export function createRegistry() {
70
70
  emitter.emit();
71
71
  }
72
72
  /**
73
- * Subscribe to changes to any data.
73
+ * Subscribe to changes to any data, either in all stores in registry, or
74
+ * in one specific store.
74
75
  *
75
- * @param {Function} listener Listener function.
76
+ * @param {Function} listener Listener function.
77
+ * @param {string|StoreDescriptor?} storeNameOrDescriptor Optional store name.
76
78
  *
77
79
  * @return {Function} Unsubscribe function.
78
80
  */
79
81
 
80
82
 
81
- const subscribe = listener => {
82
- return emitter.subscribe(listener);
83
+ const subscribe = (listener, storeNameOrDescriptor) => {
84
+ // subscribe to all stores
85
+ if (!storeNameOrDescriptor) {
86
+ return emitter.subscribe(listener);
87
+ } // subscribe to one store
88
+
89
+
90
+ const storeName = getStoreName(storeNameOrDescriptor);
91
+ const store = stores[storeName];
92
+
93
+ if (store) {
94
+ return store.subscribe(listener);
95
+ } // Trying to access a store that hasn't been registered,
96
+ // this is a pattern rarely used but seen in some places.
97
+ // We fallback to global `subscribe` here for backward-compatibility for now.
98
+ // See https://github.com/WordPress/gutenberg/pull/27466 for more info.
99
+
100
+
101
+ if (!parent) {
102
+ return emitter.subscribe(listener);
103
+ }
104
+
105
+ return parent.subscribe(listener, storeNameOrDescriptor);
83
106
  };
84
107
  /**
85
108
  * Calls a selector given the current state and extra arguments.
@@ -92,8 +115,10 @@ export function createRegistry() {
92
115
 
93
116
 
94
117
  function select(storeNameOrDescriptor) {
95
- const storeName = isObject(storeNameOrDescriptor) ? storeNameOrDescriptor.name : storeNameOrDescriptor;
96
- listeningStores.add(storeName);
118
+ var _listeningStores;
119
+
120
+ const storeName = getStoreName(storeNameOrDescriptor);
121
+ (_listeningStores = listeningStores) === null || _listeningStores === void 0 ? void 0 : _listeningStores.add(storeName);
97
122
  const store = stores[storeName];
98
123
 
99
124
  if (store) {
@@ -104,12 +129,13 @@ export function createRegistry() {
104
129
  }
105
130
 
106
131
  function __unstableMarkListeningStores(callback, ref) {
107
- listeningStores.clear();
132
+ listeningStores = new Set();
108
133
 
109
134
  try {
110
135
  return callback.call(this);
111
136
  } finally {
112
137
  ref.current = Array.from(listeningStores);
138
+ listeningStores = null;
113
139
  }
114
140
  }
115
141
  /**
@@ -126,8 +152,10 @@ export function createRegistry() {
126
152
 
127
153
 
128
154
  function resolveSelect(storeNameOrDescriptor) {
129
- const storeName = isObject(storeNameOrDescriptor) ? storeNameOrDescriptor.name : storeNameOrDescriptor;
130
- listeningStores.add(storeName);
155
+ var _listeningStores2;
156
+
157
+ const storeName = getStoreName(storeNameOrDescriptor);
158
+ (_listeningStores2 = listeningStores) === null || _listeningStores2 === void 0 ? void 0 : _listeningStores2.add(storeName);
131
159
  const store = stores[storeName];
132
160
 
133
161
  if (store) {
@@ -150,8 +178,10 @@ export function createRegistry() {
150
178
 
151
179
 
152
180
  function suspendSelect(storeNameOrDescriptor) {
153
- const storeName = isObject(storeNameOrDescriptor) ? storeNameOrDescriptor.name : storeNameOrDescriptor;
154
- listeningStores.add(storeName);
181
+ var _listeningStores3;
182
+
183
+ const storeName = getStoreName(storeNameOrDescriptor);
184
+ (_listeningStores3 = listeningStores) === null || _listeningStores3 === void 0 ? void 0 : _listeningStores3.add(storeName);
155
185
  const store = stores[storeName];
156
186
 
157
187
  if (store) {
@@ -171,7 +201,7 @@ export function createRegistry() {
171
201
 
172
202
 
173
203
  function dispatch(storeNameOrDescriptor) {
174
- const storeName = isObject(storeNameOrDescriptor) ? storeNameOrDescriptor.name : storeNameOrDescriptor;
204
+ const storeName = getStoreName(storeNameOrDescriptor);
175
205
  const store = stores[storeName];
176
206
 
177
207
  if (store) {
@@ -278,33 +308,6 @@ export function createRegistry() {
278
308
  registerStoreInstance(storeName, store);
279
309
  return store.store;
280
310
  }
281
- /**
282
- * Subscribe handler to a store.
283
- *
284
- * @param {string|StoreDescriptor} storeNameOrDescriptor The store name.
285
- * @param {Function} handler The function subscribed to the store.
286
- * @return {Function} A function to unsubscribe the handler.
287
- */
288
-
289
-
290
- function __unstableSubscribeStore(storeNameOrDescriptor, handler) {
291
- const storeName = isObject(storeNameOrDescriptor) ? storeNameOrDescriptor.name : storeNameOrDescriptor;
292
- const store = stores[storeName];
293
-
294
- if (store) {
295
- return store.subscribe(handler);
296
- } // Trying to access a store that hasn't been registered,
297
- // this is a pattern rarely used but seen in some places.
298
- // We fallback to regular `subscribe` here for backward-compatibility for now.
299
- // See https://github.com/WordPress/gutenberg/pull/27466 for more info.
300
-
301
-
302
- if (!parent) {
303
- return subscribe(handler);
304
- }
305
-
306
- return parent.__unstableSubscribeStore(storeNameOrDescriptor, handler);
307
- }
308
311
 
309
312
  function batch(callback) {
310
313
  emitter.pause();
@@ -328,8 +331,7 @@ export function createRegistry() {
328
331
  register,
329
332
  registerGenericStore,
330
333
  registerStore,
331
- __unstableMarkListeningStores,
332
- __unstableSubscribeStore
334
+ __unstableMarkListeningStores
333
335
  }; //
334
336
  // TODO:
335
337
  // This function will be deprecated as soon as it is no longer internally referenced.
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/data/src/registry.js"],"names":["mapValues","deprecated","createReduxStore","coreDataStore","createEmitter","isObject","object","createRegistry","storeConfigs","parent","stores","emitter","listeningStores","Set","globalListener","emit","subscribe","listener","select","storeNameOrDescriptor","storeName","name","add","store","getSelectors","__unstableMarkListeningStores","callback","ref","clear","call","current","Array","from","resolveSelect","getResolveSelectors","suspendSelect","getSuspendSelectors","dispatch","getActions","withPlugins","attributes","attribute","key","registry","apply","arguments","registerStoreInstance","TypeError","currentSubscribe","unsubscribeFromEmitter","unsubscribeFromStore","isPaused","register","instantiate","registerGenericStore","since","alternative","registerStore","options","reducer","__unstableSubscribeStore","handler","batch","pause","Object","values","forEach","resume","namespaces","use","plugin","config","entries"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,SAAT,QAA0B,QAA1B;AAEA;AACA;AACA;;AACA,OAAOC,UAAP,MAAuB,uBAAvB;AAEA;AACA;AACA;;AACA,OAAOC,gBAAP,MAA6B,eAA7B;AACA,OAAOC,aAAP,MAA0B,SAA1B;AACA,SAASC,aAAT,QAA8B,iBAA9B;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,QAAT,CAAmBC,MAAnB,EAA4B;AAC3B,SAAOA,MAAM,KAAK,IAAX,IAAmB,OAAOA,MAAP,KAAkB,QAA5C;AACA;AAED;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,GAAGP,aAAa,EAA7B;AACA,QAAMQ,eAAe,GAAG,IAAIC,GAAJ,EAAxB;AAEA;AACD;AACA;;AACC,WAASC,cAAT,GAA0B;AACzBH,IAAAA,OAAO,CAACI,IAAR;AACA;AAED;AACD;AACA;AACA;AACA;AACA;AACA;;;AACC,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AAK9D,MAAL,EAAc;AACbA,IAAAA,MAAM,CAACO,SAAP,CAAkBF,cAAlB;AACA;;AAED,SAAOyB,WAAW,CAAEI,QAAF,CAAlB;AACA","sourcesContent":["/**\n * External dependencies\n */\nimport { mapValues } from 'lodash';\n\n/**\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';\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 isObject( object ) {\n\treturn object !== null && typeof object === 'object';\n}\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\tconst listeningStores = new Set();\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.\n\t *\n\t * @param {Function} listener Listener function.\n\t *\n\t * @return {Function} Unsubscribe function.\n\t */\n\tconst subscribe = ( listener ) => {\n\t\treturn emitter.subscribe( listener );\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 = isObject( storeNameOrDescriptor )\n\t\t\t? storeNameOrDescriptor.name\n\t\t\t: 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.clear();\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}\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 = isObject( storeNameOrDescriptor )\n\t\t\t? storeNameOrDescriptor.name\n\t\t\t: 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 = isObject( storeNameOrDescriptor )\n\t\t\t? storeNameOrDescriptor.name\n\t\t\t: 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 = isObject( storeNameOrDescriptor )\n\t\t\t? storeNameOrDescriptor.name\n\t\t\t: 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 mapValues( attributes, ( attribute, key ) => {\n\t\t\tif ( typeof attribute !== 'function' ) {\n\t\t\t\treturn attribute;\n\t\t\t}\n\t\t\treturn function () {\n\t\t\t\treturn registry[ key ].apply( null, arguments );\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\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\t/**\n\t * Subscribe handler to a store.\n\t *\n\t * @param {string|StoreDescriptor} storeNameOrDescriptor The store name.\n\t * @param {Function} handler The function subscribed to the store.\n\t * @return {Function} A function to unsubscribe the handler.\n\t */\n\tfunction __unstableSubscribeStore( storeNameOrDescriptor, handler ) {\n\t\tconst storeName = isObject( storeNameOrDescriptor )\n\t\t\t? storeNameOrDescriptor.name\n\t\t\t: storeNameOrDescriptor;\n\n\t\tconst store = stores[ storeName ];\n\t\tif ( store ) {\n\t\t\treturn store.subscribe( handler );\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 regular `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 subscribe( handler );\n\t\t}\n\n\t\treturn parent.__unstableSubscribeStore(\n\t\t\tstoreNameOrDescriptor,\n\t\t\thandler\n\t\t);\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\t__unstableSubscribeStore,\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\treturn withPlugins( registry );\n}\n"]}
1
+ {"version":3,"sources":["@wordpress/data/src/registry.js"],"names":["mapValues","deprecated","createReduxStore","coreDataStore","createEmitter","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","attribute","key","registry","apply","arguments","registerStoreInstance","TypeError","currentSubscribe","unsubscribeFromEmitter","unsubscribeFromStore","isPaused","register","instantiate","registerGenericStore","since","alternative","registerStore","options","reducer","batch","pause","Object","values","forEach","resume","namespaces","use","plugin","config","entries"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,SAAT,QAA0B,QAA1B;AAEA;AACA;AACA;;AACA,OAAOC,UAAP,MAAuB,uBAAvB;AAEA;AACA;AACA;;AACA,OAAOC,gBAAP,MAA6B,eAA7B;AACA,OAAOC,aAAP,MAA0B,SAA1B;AACA,SAASC,aAAT,QAA8B,iBAA9B;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,GAAGR,aAAa,EAA7B;AACA,MAAIS,eAAe,GAAG,IAAtB;AAEA;AACD;AACA;;AACC,WAASC,cAAT,GAA0B;AACzBF,IAAAA,OAAO,CAACG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AACA;;AAED,SAAOuB,WAAW,CAAEI,QAAF,CAAlB;AACA","sourcesContent":["/**\n * External dependencies\n */\nimport { mapValues } from 'lodash';\n\n/**\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';\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 mapValues( attributes, ( attribute, key ) => {\n\t\t\tif ( typeof attribute !== 'function' ) {\n\t\t\t\treturn attribute;\n\t\t\t}\n\t\t\treturn function () {\n\t\t\t\treturn registry[ key ].apply( null, arguments );\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\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\treturn withPlugins( registry );\n}\n"]}
@@ -122,10 +122,14 @@ export const suspendSelect: any;
122
122
  export const dispatch: Function;
123
123
  /**
124
124
  * Given a listener function, the function will be called any time the state value
125
- * of one of the registered stores has changed. This function returns a `unsubscribe`
126
- * function used to stop the subscription.
125
+ * of one of the registered stores has changed. If you specify the optional
126
+ * `storeNameOrDescriptor` parameter, the listener function will be called only
127
+ * on updates on that one specific registered store.
127
128
  *
128
- * @param {Function} listener Callback function.
129
+ * This function returns an `unsubscribe` function used to stop the subscription.
130
+ *
131
+ * @param {Function} listener Callback function.
132
+ * @param {string|StoreDescriptor?} storeNameOrDescriptor Optional store name.
129
133
  *
130
134
  * @example
131
135
  * ```js
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.js"],"names":[],"mappings":";;;;;;;;;AAyCA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AACH,8BAnCW,OAAO,SAAS,EAAE,eAAe,CAmCQ;AAEpD;;;;;;;;;;;;;;;;;;GAkBG;AACH,8BAA6C;AAE7C;;;;;;;;;;;;;;;;;;GAkBG;AACH,gCAA2D;AAE3D;;;;;;;;;;GAUG;AACH,gCAA2D;AAE3D;;;;;;;;;;;;;;;;;;;GAmBG;AACH,gCAAiD;AAEjD;;;;;;;;;;;;;;;;;;;GAmBG;AACH,iCAAmD;AAEnD;;;;;;;GAOG;AACH,4CAAyE;AAEzE;;;;;;;;;GASG;AACH,qCAA2D;AAE3D;;;;;;GAMG;AACH,sBAAuC;AAEvC;;;;;;;;;;;;;;;;;GAiBG;AACH,2BAAiD"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.js"],"names":[],"mappings":";;;;;;;;;AAyCA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AACH,8BAnCW,OAAO,SAAS,EAAE,eAAe,CAmCQ;AAEpD;;;;;;;;;;;;;;;;;;GAkBG;AACH,8BAA6C;AAE7C;;;;;;;;;;;;;;;;;;GAkBG;AACH,gCAA2D;AAE3D;;;;;;;;;;GAUG;AACH,gCAA2D;AAE3D;;;;;;;;;;;;;;;;;;;GAmBG;AACH,gCAAiD;AAEjD;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,iCAAmD;AAEnD;;;;;;;GAOG;AACH,4CAAyE;AAEzE;;;;;;;;;GASG;AACH,qCAA2D;AAE3D;;;;;;GAMG;AACH,sBAAuC;AAEvC;;;;;;;;;;;;;;;;;GAiBG;AACH,2BAAiD"}
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/redux-store/index.js"],"names":[],"mappings":"AA4EA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,yEAPW,MAAM,+KA8HhB;2BAlMa,OAAO,UAAU,EAAE,YAAY;iCAEhC,OAAO,UAAU,EAAE,eAAe,CAAC,CAAC,CAAC;0DAIrC,OAAO,UAAU,EAAE,gBAAgB,CAAC,KAAK,EAAC,OAAO,EAAC,SAAS,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/redux-store/index.js"],"names":[],"mappings":"AAqFA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,yEAPW,MAAM,+KA8HhB;2BA3Ma,OAAO,UAAU,EAAE,YAAY;iCAEhC,OAAO,UAAU,EAAE,eAAe,CAAC,CAAC,CAAC;0DAIrC,OAAO,UAAU,EAAE,gBAAgB,CAAC,KAAK,EAAC,OAAO,EAAC,SAAS,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../src/registry.js"],"names":[],"mappings":"AAiDA;;;;;;;;GAQG;AACH,8CALW,MAAM,WACN,MAAM,UAEL,cAAc,CAySzB"}
1
+ {"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../src/registry.js"],"names":[],"mappings":"AAkDA;;;;;;;;GAQG;AACH,8CALW,MAAM,WACN,MAAM,UAEL,cAAc,CAwRzB"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wordpress/data",
3
- "version": "7.6.0",
3
+ "version": "8.1.0",
4
4
  "description": "Data module for WordPress.",
5
5
  "author": "The WordPress Contributors",
6
6
  "license": "GPL-2.0-or-later",
@@ -29,12 +29,12 @@
29
29
  "sideEffects": false,
30
30
  "dependencies": {
31
31
  "@babel/runtime": "^7.16.0",
32
- "@wordpress/compose": "^5.20.0",
33
- "@wordpress/deprecated": "^3.22.0",
34
- "@wordpress/element": "^4.20.0",
35
- "@wordpress/is-shallow-equal": "^4.22.0",
36
- "@wordpress/priority-queue": "^2.22.0",
37
- "@wordpress/redux-routine": "^4.22.0",
32
+ "@wordpress/compose": "^6.1.0",
33
+ "@wordpress/deprecated": "^3.24.0",
34
+ "@wordpress/element": "^5.1.0",
35
+ "@wordpress/is-shallow-equal": "^4.24.0",
36
+ "@wordpress/priority-queue": "^2.24.0",
37
+ "@wordpress/redux-routine": "^4.24.0",
38
38
  "equivalent-key-map": "^0.2.2",
39
39
  "is-plain-object": "^5.0.0",
40
40
  "is-promise": "^4.0.0",
@@ -44,10 +44,10 @@
44
44
  "use-memo-one": "^1.1.1"
45
45
  },
46
46
  "peerDependencies": {
47
- "react": "^17.0.0"
47
+ "react": "^18.0.0"
48
48
  },
49
49
  "publishConfig": {
50
50
  "access": "public"
51
51
  },
52
- "gitHead": "7ac04f446242452d3cb24372f9ca58f0cae97715"
52
+ "gitHead": "200bee7b06b15f6fa655e25b6ab69cbd6b49a357"
53
53
  }
@@ -242,7 +242,7 @@ export default function useSelect( mapSelect, deps ) {
242
242
  onStoreChange();
243
243
 
244
244
  const unsubscribers = listeningStores.current.map( ( storeName ) =>
245
- registry.__unstableSubscribeStore( storeName, onChange )
245
+ registry.subscribe( onChange, storeName )
246
246
  );
247
247
 
248
248
  isMounted.current = true;
@@ -374,7 +374,7 @@ export function useSuspenseSelect( mapSelect, deps ) {
374
374
  onStoreChange();
375
375
 
376
376
  const unsubscribers = listeningStores.current.map( ( storeName ) =>
377
- registry.__unstableSubscribeStore( storeName, onChange )
377
+ registry.subscribe( onChange, storeName )
378
378
  );
379
379
 
380
380
  isMounted.current = true;
@@ -118,7 +118,8 @@ describe( 'useSelect', () => {
118
118
  expect( screen.getByRole( 'status' ) ).toHaveTextContent( 'bar' );
119
119
  } );
120
120
 
121
- it( 'avoid calling nested listener after unmounted', async () => {
121
+ // TODO: this might be impossible to pull off in React 18 without `useSyncExternalStore`
122
+ it.skip( 'avoid calling nested listener after unmounted', async () => {
122
123
  registry.registerStore( 'toggler', {
123
124
  reducer: ( state = false, action ) =>
124
125
  action.type === 'TOGGLE' ? ! state : state,
@@ -593,7 +594,7 @@ describe( 'useSelect', () => {
593
594
  'count2'
594
595
  );
595
596
 
596
- screen.getByText( 'Toggle' ).click();
597
+ act( () => screen.getByText( 'Toggle' ).click() );
597
598
 
598
599
  expect( selectCount1 ).toHaveBeenCalledTimes( 2 );
599
600
  expect( selectCount2 ).toHaveBeenCalledTimes( 2 );
@@ -1145,4 +1146,38 @@ describe( 'useSelect', () => {
1145
1146
  expect( screen.getByRole( 'status' ) ).toHaveTextContent( '10' );
1146
1147
  } );
1147
1148
  } );
1149
+
1150
+ describe( 'static store selection mode', () => {
1151
+ it( 'can read the current value from store', () => {
1152
+ registry.registerStore( 'testStore', {
1153
+ reducer: ( s = 0, a ) => ( a.type === 'INC' ? s + 1 : s ),
1154
+ actions: { inc: () => ( { type: 'INC' } ) },
1155
+ selectors: { get: ( s ) => s },
1156
+ } );
1157
+
1158
+ const record = jest.fn();
1159
+
1160
+ function TestComponent() {
1161
+ const { get } = useSelect( 'testStore' );
1162
+ return (
1163
+ <button onClick={ () => record( get() ) }>record</button>
1164
+ );
1165
+ }
1166
+
1167
+ render(
1168
+ <RegistryProvider value={ registry }>
1169
+ <TestComponent />
1170
+ </RegistryProvider>
1171
+ );
1172
+
1173
+ fireEvent.click( screen.getByRole( 'button' ) );
1174
+ expect( record ).toHaveBeenLastCalledWith( 0 );
1175
+
1176
+ // no need to act() as the component doesn't react to the updates
1177
+ registry.dispatch( 'testStore' ).inc();
1178
+
1179
+ fireEvent.click( screen.getByRole( 'button' ) );
1180
+ expect( record ).toHaveBeenLastCalledWith( 1 );
1181
+ } );
1182
+ } );
1148
1183
  } );
@@ -139,10 +139,11 @@ describe( 'useSuspenseSelect', () => {
139
139
  }
140
140
 
141
141
  render() {
142
+ const { children } = this.props;
142
143
  if ( this.state.error ) {
143
144
  return <div aria-label="error">{ this.state.error }</div>;
144
145
  }
145
- return this.props.children;
146
+ return children;
146
147
  }
147
148
  }
148
149
 
package/src/index.js CHANGED
@@ -159,10 +159,14 @@ export const dispatch = defaultRegistry.dispatch;
159
159
 
160
160
  /**
161
161
  * Given a listener function, the function will be called any time the state value
162
- * of one of the registered stores has changed. This function returns a `unsubscribe`
163
- * function used to stop the subscription.
162
+ * of one of the registered stores has changed. If you specify the optional
163
+ * `storeNameOrDescriptor` parameter, the listener function will be called only
164
+ * on updates on that one specific registered store.
164
165
  *
165
- * @param {Function} listener Callback function.
166
+ * This function returns an `unsubscribe` function used to stop the subscription.
167
+ *
168
+ * @param {Function} listener Callback function.
169
+ * @param {string|StoreDescriptor?} storeNameOrDescriptor Optional store name.
166
170
  *
167
171
  * @example
168
172
  * ```js
@@ -26,9 +26,10 @@ describe( 'persistence', () => {
26
26
  } );
27
27
 
28
28
  it( 'should not mutate options', () => {
29
- const options = Object.freeze( { persist: true, reducer() {} } );
30
-
31
- registry.registerStore( 'test', options );
29
+ expect( () => {
30
+ const options = Object.freeze( { persist: true, reducer() {} } );
31
+ registry.registerStore( 'test', options );
32
+ } ).not.toThrowError( /object is not extensible/ );
32
33
  } );
33
34
 
34
35
  it( 'should load a persisted value as initialState', () => {
@@ -43,6 +43,15 @@ const trimUndefinedValues = ( array ) => {
43
43
  return result;
44
44
  };
45
45
 
46
+ // Convert Map objects to plain objects
47
+ const mapToObject = ( key, state ) => {
48
+ if ( state instanceof Map ) {
49
+ return Object.fromEntries( state );
50
+ }
51
+
52
+ return state;
53
+ };
54
+
46
55
  /**
47
56
  * Create a cache to track whether resolvers started running or not.
48
57
  *
@@ -256,6 +265,9 @@ function instantiateReduxStore( key, options, registry, thunkArgs ) {
256
265
  window.__REDUX_DEVTOOLS_EXTENSION__( {
257
266
  name: key,
258
267
  instanceId: key,
268
+ serialize: {
269
+ replacer: mapToObject,
270
+ },
259
271
  } )
260
272
  );
261
273
  }
package/src/registry.js CHANGED
@@ -43,10 +43,11 @@ import { createEmitter } from './utils/emitter';
43
43
  * @property {Function} registerStore registers store.
44
44
  */
45
45
 
46
- function isObject( object ) {
47
- return object !== null && typeof object === 'object';
46
+ function getStoreName( storeNameOrDescriptor ) {
47
+ return typeof storeNameOrDescriptor === 'string'
48
+ ? storeNameOrDescriptor
49
+ : storeNameOrDescriptor.name;
48
50
  }
49
-
50
51
  /**
51
52
  * Creates a new store registry, given an optional object of initial store
52
53
  * configurations.
@@ -59,7 +60,7 @@ function isObject( object ) {
59
60
  export function createRegistry( storeConfigs = {}, parent = null ) {
60
61
  const stores = {};
61
62
  const emitter = createEmitter();
62
- const listeningStores = new Set();
63
+ let listeningStores = null;
63
64
 
64
65
  /**
65
66
  * Global listener called for each store's update.
@@ -69,14 +70,36 @@ export function createRegistry( storeConfigs = {}, parent = null ) {
69
70
  }
70
71
 
71
72
  /**
72
- * Subscribe to changes to any data.
73
+ * Subscribe to changes to any data, either in all stores in registry, or
74
+ * in one specific store.
73
75
  *
74
- * @param {Function} listener Listener function.
76
+ * @param {Function} listener Listener function.
77
+ * @param {string|StoreDescriptor?} storeNameOrDescriptor Optional store name.
75
78
  *
76
79
  * @return {Function} Unsubscribe function.
77
80
  */
78
- const subscribe = ( listener ) => {
79
- return emitter.subscribe( listener );
81
+ const subscribe = ( listener, storeNameOrDescriptor ) => {
82
+ // subscribe to all stores
83
+ if ( ! storeNameOrDescriptor ) {
84
+ return emitter.subscribe( listener );
85
+ }
86
+
87
+ // subscribe to one store
88
+ const storeName = getStoreName( storeNameOrDescriptor );
89
+ const store = stores[ storeName ];
90
+ if ( store ) {
91
+ return store.subscribe( listener );
92
+ }
93
+
94
+ // Trying to access a store that hasn't been registered,
95
+ // this is a pattern rarely used but seen in some places.
96
+ // We fallback to global `subscribe` here for backward-compatibility for now.
97
+ // See https://github.com/WordPress/gutenberg/pull/27466 for more info.
98
+ if ( ! parent ) {
99
+ return emitter.subscribe( listener );
100
+ }
101
+
102
+ return parent.subscribe( listener, storeNameOrDescriptor );
80
103
  };
81
104
 
82
105
  /**
@@ -88,10 +111,8 @@ export function createRegistry( storeConfigs = {}, parent = null ) {
88
111
  * @return {*} The selector's returned value.
89
112
  */
90
113
  function select( storeNameOrDescriptor ) {
91
- const storeName = isObject( storeNameOrDescriptor )
92
- ? storeNameOrDescriptor.name
93
- : storeNameOrDescriptor;
94
- listeningStores.add( storeName );
114
+ const storeName = getStoreName( storeNameOrDescriptor );
115
+ listeningStores?.add( storeName );
95
116
  const store = stores[ storeName ];
96
117
  if ( store ) {
97
118
  return store.getSelectors();
@@ -101,11 +122,12 @@ export function createRegistry( storeConfigs = {}, parent = null ) {
101
122
  }
102
123
 
103
124
  function __unstableMarkListeningStores( callback, ref ) {
104
- listeningStores.clear();
125
+ listeningStores = new Set();
105
126
  try {
106
127
  return callback.call( this );
107
128
  } finally {
108
129
  ref.current = Array.from( listeningStores );
130
+ listeningStores = null;
109
131
  }
110
132
  }
111
133
 
@@ -121,10 +143,8 @@ export function createRegistry( storeConfigs = {}, parent = null ) {
121
143
  * @return {Object} Each key of the object matches the name of a selector.
122
144
  */
123
145
  function resolveSelect( storeNameOrDescriptor ) {
124
- const storeName = isObject( storeNameOrDescriptor )
125
- ? storeNameOrDescriptor.name
126
- : storeNameOrDescriptor;
127
- listeningStores.add( storeName );
146
+ const storeName = getStoreName( storeNameOrDescriptor );
147
+ listeningStores?.add( storeName );
128
148
  const store = stores[ storeName ];
129
149
  if ( store ) {
130
150
  return store.getResolveSelectors();
@@ -145,10 +165,8 @@ export function createRegistry( storeConfigs = {}, parent = null ) {
145
165
  * @return {Object} Object containing the store's suspense-wrapped selectors.
146
166
  */
147
167
  function suspendSelect( storeNameOrDescriptor ) {
148
- const storeName = isObject( storeNameOrDescriptor )
149
- ? storeNameOrDescriptor.name
150
- : storeNameOrDescriptor;
151
- listeningStores.add( storeName );
168
+ const storeName = getStoreName( storeNameOrDescriptor );
169
+ listeningStores?.add( storeName );
152
170
  const store = stores[ storeName ];
153
171
  if ( store ) {
154
172
  return store.getSuspendSelectors();
@@ -166,9 +184,7 @@ export function createRegistry( storeConfigs = {}, parent = null ) {
166
184
  * @return {*} The action's returned value.
167
185
  */
168
186
  function dispatch( storeNameOrDescriptor ) {
169
- const storeName = isObject( storeNameOrDescriptor )
170
- ? storeNameOrDescriptor.name
171
- : storeNameOrDescriptor;
187
+ const storeName = getStoreName( storeNameOrDescriptor );
172
188
  const store = stores[ storeName ];
173
189
  if ( store ) {
174
190
  return store.getActions();
@@ -268,37 +284,6 @@ export function createRegistry( storeConfigs = {}, parent = null ) {
268
284
  return store.store;
269
285
  }
270
286
 
271
- /**
272
- * Subscribe handler to a store.
273
- *
274
- * @param {string|StoreDescriptor} storeNameOrDescriptor The store name.
275
- * @param {Function} handler The function subscribed to the store.
276
- * @return {Function} A function to unsubscribe the handler.
277
- */
278
- function __unstableSubscribeStore( storeNameOrDescriptor, handler ) {
279
- const storeName = isObject( storeNameOrDescriptor )
280
- ? storeNameOrDescriptor.name
281
- : storeNameOrDescriptor;
282
-
283
- const store = stores[ storeName ];
284
- if ( store ) {
285
- return store.subscribe( handler );
286
- }
287
-
288
- // Trying to access a store that hasn't been registered,
289
- // this is a pattern rarely used but seen in some places.
290
- // We fallback to regular `subscribe` here for backward-compatibility for now.
291
- // See https://github.com/WordPress/gutenberg/pull/27466 for more info.
292
- if ( ! parent ) {
293
- return subscribe( handler );
294
- }
295
-
296
- return parent.__unstableSubscribeStore(
297
- storeNameOrDescriptor,
298
- handler
299
- );
300
- }
301
-
302
287
  function batch( callback ) {
303
288
  emitter.pause();
304
289
  Object.values( stores ).forEach( ( store ) => store.emitter.pause() );
@@ -321,7 +306,6 @@ export function createRegistry( storeConfigs = {}, parent = null ) {
321
306
  registerGenericStore,
322
307
  registerStore,
323
308
  __unstableMarkListeningStores,
324
- __unstableSubscribeStore,
325
309
  };
326
310
 
327
311
  //