@wordpress/data 8.0.0 → 8.2.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.
- package/CHANGELOG.md +4 -0
- package/LICENSE.md +1 -1
- package/build/components/use-select/index.js +139 -239
- package/build/components/use-select/index.js.map +1 -1
- package/build/registry.js +12 -5
- package/build/registry.js.map +1 -1
- package/build-module/components/use-select/index.js +140 -239
- package/build-module/components/use-select/index.js.map +1 -1
- package/build-module/registry.js +12 -5
- package/build-module/registry.js.map +1 -1
- package/build-types/components/use-select/index.d.ts +0 -13
- package/build-types/components/use-select/index.d.ts.map +1 -1
- package/build-types/registry.d.ts.map +1 -1
- package/package.json +8 -8
- package/src/components/use-select/index.js +142 -275
- package/src/components/use-select/test/index.js +52 -20
- package/src/components/use-select/test/suspense.js +74 -1
- package/src/registry.js +6 -5
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/data/src/registry.js"],"names":["mapValues","deprecated","createReduxStore","coreDataStore","createEmitter","getStoreName","storeNameOrDescriptor","name","createRegistry","storeConfigs","parent","stores","emitter","listeningStores","Set","globalListener","emit","subscribe","listener","storeName","store","select","add","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","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,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C/B,QAAK,CAAEU,MAAP,EAAgB;AACf;AACA;;AAEDzB,IAAAA,QAAQ,GAAG,EACV,GAAGA,QADO;AAEV,SAAGyB,MAAM,CAAEzB,QAAF,EAAYe,OAAZ;AAFC,KAAX;AAKA,WAAOf,QAAP;AACA;;AAEDA,EAAAA,QAAQ,CAACS,QAAT,CAAmBhD,aAAnB;;AAEA,OAAM,MAAM,CAAEI,IAAF,EAAQ6D,MAAR,CAAZ,IAAgCP,MAAM,CAACQ,OAAP,CAAgB5D,YAAhB,CAAhC,EAAiE;AAChEiC,IAAAA,QAAQ,CAACS,QAAT,CAAmBjD,gBAAgB,CAAEK,IAAF,EAAQ6D,MAAR,CAAnC;AACA;;AAED,MAAK1D,MAAL,EAAc;AACbA,IAAAA,MAAM,CAACO,SAAP,CAAkBF,cAAlB;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\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, 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.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 = 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"]}
|
|
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,MAA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B,CAAhC,EAAiE;AAChEgC,IAAAA,QAAQ,CAACS,QAAT,CAAmBhD,gBAAgB,CAAEK,IAAF,EAAQ4D,MAAR,CAAnC;AACA;;AAED,MAAKzD,MAAL,EAAc;AACbA,IAAAA,MAAM,CAACM,SAAP,CAAkBF,cAAlB;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"]}
|
|
@@ -1,16 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @typedef {import('../../types').StoreDescriptor<C>} StoreDescriptor
|
|
3
|
-
* @template C
|
|
4
|
-
*/
|
|
5
|
-
/**
|
|
6
|
-
* @typedef {import('../../types').ReduxStoreConfig<State,Actions,Selectors>} ReduxStoreConfig
|
|
7
|
-
* @template State,Actions,Selectors
|
|
8
|
-
*/
|
|
9
|
-
/**
|
|
10
|
-
* @typedef {import('../../types').UseSelectReturn<T>} UseSelectReturn
|
|
11
|
-
* @template T
|
|
12
|
-
*/
|
|
13
|
-
/** @typedef {import('../../types').MapSelect} MapSelect */
|
|
14
1
|
/**
|
|
15
2
|
* Custom react hook for retrieving props from registered selectors.
|
|
16
3
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/components/use-select/index.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/components/use-select/index.js"],"names":[],"mappings":"AA6JA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+DG;AACH,8IAlDW,OAAO,EAAE,4CAuEnB;AAED;;;;;;;;;;;;;;GAcG;AACH,qEAFY,MAAM,CAIjB;iCA/OY,OAAO,aAAa,EAAE,eAAe,CAAC,CAAC,CAAC;0DAIxC,OAAO,aAAa,EAAE,gBAAgB,CAAC,KAAK,EAAC,OAAO,EAAC,SAAS,CAAC;iCAI/D,OAAO,aAAa,EAAE,eAAe,CAAC,CAAC,CAAC;wBAGvC,OAAO,aAAa,EAAE,SAAS"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../src/registry.js"],"names":[],"mappings":"AAkDA;;;;;;;;GAQG;AACH,8CALW,MAAM,WACN,MAAM,UAEL,cAAc,
|
|
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": "8.
|
|
3
|
+
"version": "8.2.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": "^6.
|
|
33
|
-
"@wordpress/deprecated": "^3.
|
|
34
|
-
"@wordpress/element": "^5.
|
|
35
|
-
"@wordpress/is-shallow-equal": "^4.
|
|
36
|
-
"@wordpress/priority-queue": "^2.
|
|
37
|
-
"@wordpress/redux-routine": "^4.
|
|
32
|
+
"@wordpress/compose": "^6.2.0",
|
|
33
|
+
"@wordpress/deprecated": "^3.25.0",
|
|
34
|
+
"@wordpress/element": "^5.2.0",
|
|
35
|
+
"@wordpress/is-shallow-equal": "^4.25.0",
|
|
36
|
+
"@wordpress/priority-queue": "^2.25.0",
|
|
37
|
+
"@wordpress/redux-routine": "^4.25.0",
|
|
38
38
|
"equivalent-key-map": "^0.2.2",
|
|
39
39
|
"is-plain-object": "^5.0.0",
|
|
40
40
|
"is-promise": "^4.0.0",
|
|
@@ -49,5 +49,5 @@
|
|
|
49
49
|
"publishConfig": {
|
|
50
50
|
"access": "public"
|
|
51
51
|
},
|
|
52
|
-
"gitHead": "
|
|
52
|
+
"gitHead": "204c880ff65295768e9695dfee6c7a9fee1fdd05"
|
|
53
53
|
}
|
|
@@ -1,8 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* External dependencies
|
|
3
|
-
*/
|
|
4
|
-
import { useMemoOne } from 'use-memo-one';
|
|
5
|
-
|
|
6
1
|
/**
|
|
7
2
|
* WordPress dependencies
|
|
8
3
|
*/
|
|
@@ -11,11 +6,10 @@ import {
|
|
|
11
6
|
useRef,
|
|
12
7
|
useCallback,
|
|
13
8
|
useMemo,
|
|
14
|
-
|
|
9
|
+
useSyncExternalStore,
|
|
15
10
|
useDebugValue,
|
|
16
11
|
} from '@wordpress/element';
|
|
17
12
|
import isShallowEqual from '@wordpress/is-shallow-equal';
|
|
18
|
-
import { useIsomorphicLayoutEffect } from '@wordpress/compose';
|
|
19
13
|
|
|
20
14
|
/**
|
|
21
15
|
* Internal dependencies
|
|
@@ -23,7 +17,6 @@ import { useIsomorphicLayoutEffect } from '@wordpress/compose';
|
|
|
23
17
|
import useRegistry from '../registry-provider/use-registry';
|
|
24
18
|
import useAsyncMode from '../async-mode-provider/use-async-mode';
|
|
25
19
|
|
|
26
|
-
const noop = () => {};
|
|
27
20
|
const renderQueue = createQueue();
|
|
28
21
|
|
|
29
22
|
/**
|
|
@@ -40,6 +33,128 @@ const renderQueue = createQueue();
|
|
|
40
33
|
*/
|
|
41
34
|
/** @typedef {import('../../types').MapSelect} MapSelect */
|
|
42
35
|
|
|
36
|
+
function Store( registry, suspense ) {
|
|
37
|
+
const select = suspense ? registry.suspendSelect : registry.select;
|
|
38
|
+
const queueContext = {};
|
|
39
|
+
let lastMapSelect;
|
|
40
|
+
let lastMapResult;
|
|
41
|
+
let lastMapResultValid = false;
|
|
42
|
+
let lastIsAsync;
|
|
43
|
+
let subscribe;
|
|
44
|
+
|
|
45
|
+
const createSubscriber = ( stores ) => ( listener ) => {
|
|
46
|
+
// Invalidate the value right after subscription was created. React will
|
|
47
|
+
// call `getValue` after subscribing, to detect store updates that happened
|
|
48
|
+
// in the interval between the `getValue` call during render and creating
|
|
49
|
+
// the subscription, which is slightly delayed. We need to ensure that this
|
|
50
|
+
// second `getValue` call will compute a fresh value.
|
|
51
|
+
lastMapResultValid = false;
|
|
52
|
+
|
|
53
|
+
const onStoreChange = () => {
|
|
54
|
+
// Invalidate the value on store update, so that a fresh value is computed.
|
|
55
|
+
lastMapResultValid = false;
|
|
56
|
+
listener();
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
const onChange = () => {
|
|
60
|
+
if ( lastIsAsync ) {
|
|
61
|
+
renderQueue.add( queueContext, onStoreChange );
|
|
62
|
+
} else {
|
|
63
|
+
onStoreChange();
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const unsubs = stores.map( ( storeName ) => {
|
|
68
|
+
return registry.subscribe( onChange, storeName );
|
|
69
|
+
} );
|
|
70
|
+
|
|
71
|
+
return () => {
|
|
72
|
+
// The return value of the subscribe function could be undefined if the store is a custom generic store.
|
|
73
|
+
for ( const unsub of unsubs ) {
|
|
74
|
+
unsub?.();
|
|
75
|
+
}
|
|
76
|
+
// Cancel existing store updates that were already scheduled.
|
|
77
|
+
renderQueue.cancel( queueContext );
|
|
78
|
+
};
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
return ( mapSelect, resubscribe, isAsync ) => {
|
|
82
|
+
const selectValue = () => mapSelect( select, registry );
|
|
83
|
+
|
|
84
|
+
function updateValue( selectFromStore ) {
|
|
85
|
+
// If the last value is valid, and the `mapSelect` callback hasn't changed,
|
|
86
|
+
// then we can safely return the cached value. The value can change only on
|
|
87
|
+
// store update, and in that case value will be invalidated by the listener.
|
|
88
|
+
if ( lastMapResultValid && mapSelect === lastMapSelect ) {
|
|
89
|
+
return lastMapResult;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const mapResult = selectFromStore();
|
|
93
|
+
|
|
94
|
+
// If the new value is shallow-equal to the old one, keep the old one so
|
|
95
|
+
// that we don't trigger unwanted updates that do a `===` check.
|
|
96
|
+
if ( ! isShallowEqual( lastMapResult, mapResult ) ) {
|
|
97
|
+
lastMapResult = mapResult;
|
|
98
|
+
}
|
|
99
|
+
lastMapResultValid = true;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function getValue() {
|
|
103
|
+
// Update the value in case it's been invalidated or `mapSelect` has changed.
|
|
104
|
+
updateValue( selectValue );
|
|
105
|
+
return lastMapResult;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// When transitioning from async to sync mode, cancel existing store updates
|
|
109
|
+
// that have been scheduled, and invalidate the value so that it's freshly
|
|
110
|
+
// computed. It might have been changed by the update we just cancelled.
|
|
111
|
+
if ( lastIsAsync && ! isAsync ) {
|
|
112
|
+
lastMapResultValid = false;
|
|
113
|
+
renderQueue.cancel( queueContext );
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// Either initialize the `subscribe` function, or create a new one if `mapSelect`
|
|
117
|
+
// changed and has dependencies.
|
|
118
|
+
// Usage without dependencies, `useSelect( ( s ) => { ... } )`, will subscribe
|
|
119
|
+
// only once, at mount, and won't resubscibe even if `mapSelect` changes.
|
|
120
|
+
if ( ! subscribe || ( resubscribe && mapSelect !== lastMapSelect ) ) {
|
|
121
|
+
// Find out what stores the `mapSelect` callback is selecting from and
|
|
122
|
+
// use that list to create subscriptions to specific stores.
|
|
123
|
+
const listeningStores = { current: null };
|
|
124
|
+
updateValue( () =>
|
|
125
|
+
registry.__unstableMarkListeningStores(
|
|
126
|
+
selectValue,
|
|
127
|
+
listeningStores
|
|
128
|
+
)
|
|
129
|
+
);
|
|
130
|
+
subscribe = createSubscriber( listeningStores.current );
|
|
131
|
+
} else {
|
|
132
|
+
updateValue( selectValue );
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
lastIsAsync = isAsync;
|
|
136
|
+
lastMapSelect = mapSelect;
|
|
137
|
+
|
|
138
|
+
// Return a pair of functions that can be passed to `useSyncExternalStore`.
|
|
139
|
+
return { subscribe, getValue };
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function useStaticSelect( storeName ) {
|
|
144
|
+
return useRegistry().select( storeName );
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function useMappingSelect( suspense, mapSelect, deps ) {
|
|
148
|
+
const registry = useRegistry();
|
|
149
|
+
const isAsync = useAsyncMode();
|
|
150
|
+
const store = useMemo( () => Store( registry, suspense ), [ registry ] );
|
|
151
|
+
const selector = useCallback( mapSelect, deps );
|
|
152
|
+
const { subscribe, getValue } = store( selector, !! deps, isAsync );
|
|
153
|
+
const result = useSyncExternalStore( subscribe, getValue, getValue );
|
|
154
|
+
useDebugValue( result );
|
|
155
|
+
return result;
|
|
156
|
+
}
|
|
157
|
+
|
|
43
158
|
/**
|
|
44
159
|
* Custom react hook for retrieving props from registered selectors.
|
|
45
160
|
*
|
|
@@ -105,162 +220,26 @@ const renderQueue = createQueue();
|
|
|
105
220
|
* @return {UseSelectReturn<T>} A custom react hook.
|
|
106
221
|
*/
|
|
107
222
|
export default function useSelect( mapSelect, deps ) {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
if (
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
// on every invocation whether or not we have a real function
|
|
119
|
-
// for `mapSelect`. we'll create this intermediate variable to
|
|
120
|
-
// fulfill that need and then reference it with our "real"
|
|
121
|
-
// `_mapSelect` if we can.
|
|
122
|
-
const callbackMapper = useCallback(
|
|
123
|
-
hasMappingFunction ? mapSelect : noop,
|
|
124
|
-
deps
|
|
125
|
-
);
|
|
126
|
-
const _mapSelect = hasMappingFunction ? callbackMapper : null;
|
|
127
|
-
|
|
128
|
-
const registry = useRegistry();
|
|
129
|
-
const isAsync = useAsyncMode();
|
|
130
|
-
|
|
131
|
-
const latestRegistry = useRef( registry );
|
|
132
|
-
const latestMapSelect = useRef();
|
|
133
|
-
const latestIsAsync = useRef( isAsync );
|
|
134
|
-
const latestMapOutput = useRef();
|
|
135
|
-
const latestMapOutputError = useRef();
|
|
136
|
-
|
|
137
|
-
// Keep track of the stores being selected in the _mapSelect function,
|
|
138
|
-
// and only subscribe to those stores later.
|
|
139
|
-
const listeningStores = useRef( [] );
|
|
140
|
-
const wrapSelect = useCallback(
|
|
141
|
-
( callback ) =>
|
|
142
|
-
registry.__unstableMarkListeningStores(
|
|
143
|
-
() => callback( registry.select, registry ),
|
|
144
|
-
listeningStores
|
|
145
|
-
),
|
|
146
|
-
[ registry ]
|
|
147
|
-
);
|
|
148
|
-
|
|
149
|
-
// Generate a "flag" for used in the effect dependency array.
|
|
150
|
-
// It's different than just using `mapSelect` since deps could be undefined,
|
|
151
|
-
// in that case, we would still want to memoize it.
|
|
152
|
-
const depsChangedFlag = useMemo( () => ( {} ), deps || [] );
|
|
153
|
-
|
|
154
|
-
let mapOutput;
|
|
155
|
-
|
|
156
|
-
let selectorRan = false;
|
|
157
|
-
if ( _mapSelect ) {
|
|
158
|
-
mapOutput = latestMapOutput.current;
|
|
159
|
-
const hasReplacedRegistry = latestRegistry.current !== registry;
|
|
160
|
-
const hasReplacedMapSelect = latestMapSelect.current !== _mapSelect;
|
|
161
|
-
const hasLeftAsyncMode = latestIsAsync.current && ! isAsync;
|
|
162
|
-
const lastMapSelectFailed = !! latestMapOutputError.current;
|
|
163
|
-
|
|
164
|
-
if (
|
|
165
|
-
hasReplacedRegistry ||
|
|
166
|
-
hasReplacedMapSelect ||
|
|
167
|
-
hasLeftAsyncMode ||
|
|
168
|
-
lastMapSelectFailed
|
|
169
|
-
) {
|
|
170
|
-
try {
|
|
171
|
-
mapOutput = wrapSelect( _mapSelect );
|
|
172
|
-
selectorRan = true;
|
|
173
|
-
} catch ( error ) {
|
|
174
|
-
let errorMessage = `An error occurred while running 'mapSelect': ${ error.message }`;
|
|
175
|
-
|
|
176
|
-
if ( latestMapOutputError.current ) {
|
|
177
|
-
errorMessage += `\nThe error may be correlated with this previous error:\n`;
|
|
178
|
-
errorMessage += `${ latestMapOutputError.current.stack }\n\n`;
|
|
179
|
-
errorMessage += 'Original stack trace:';
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
// eslint-disable-next-line no-console
|
|
183
|
-
console.error( errorMessage );
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
useIsomorphicLayoutEffect( () => {
|
|
189
|
-
if ( ! hasMappingFunction ) {
|
|
190
|
-
return;
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
latestRegistry.current = registry;
|
|
194
|
-
latestMapSelect.current = _mapSelect;
|
|
195
|
-
latestIsAsync.current = isAsync;
|
|
196
|
-
if ( selectorRan ) {
|
|
197
|
-
latestMapOutput.current = mapOutput;
|
|
198
|
-
}
|
|
199
|
-
latestMapOutputError.current = undefined;
|
|
200
|
-
} );
|
|
201
|
-
|
|
202
|
-
// React can sometimes clear the `useMemo` cache.
|
|
203
|
-
// We use the cache-stable `useMemoOne` to avoid
|
|
204
|
-
// losing queues.
|
|
205
|
-
const queueContext = useMemoOne( () => ( { queue: true } ), [ registry ] );
|
|
206
|
-
const [ , forceRender ] = useReducer( ( s ) => s + 1, 0 );
|
|
207
|
-
const isMounted = useRef( false );
|
|
208
|
-
|
|
209
|
-
useIsomorphicLayoutEffect( () => {
|
|
210
|
-
if ( ! hasMappingFunction ) {
|
|
211
|
-
return;
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
const onStoreChange = () => {
|
|
215
|
-
try {
|
|
216
|
-
const newMapOutput = wrapSelect( latestMapSelect.current );
|
|
217
|
-
|
|
218
|
-
if ( isShallowEqual( latestMapOutput.current, newMapOutput ) ) {
|
|
219
|
-
return;
|
|
220
|
-
}
|
|
221
|
-
latestMapOutput.current = newMapOutput;
|
|
222
|
-
} catch ( error ) {
|
|
223
|
-
latestMapOutputError.current = error;
|
|
224
|
-
}
|
|
225
|
-
forceRender();
|
|
226
|
-
};
|
|
227
|
-
|
|
228
|
-
const onChange = () => {
|
|
229
|
-
if ( ! isMounted.current ) {
|
|
230
|
-
return;
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
if ( latestIsAsync.current ) {
|
|
234
|
-
renderQueue.add( queueContext, onStoreChange );
|
|
235
|
-
} else {
|
|
236
|
-
onStoreChange();
|
|
237
|
-
}
|
|
238
|
-
};
|
|
239
|
-
|
|
240
|
-
// Catch any possible state changes during mount before the subscription
|
|
241
|
-
// could be set.
|
|
242
|
-
onStoreChange();
|
|
243
|
-
|
|
244
|
-
const unsubscribers = listeningStores.current.map( ( storeName ) =>
|
|
245
|
-
registry.subscribe( onChange, storeName )
|
|
223
|
+
// On initial call, on mount, determine the mode of this `useSelect` call
|
|
224
|
+
// and then never allow it to change on subsequent updates.
|
|
225
|
+
const staticSelectMode = typeof mapSelect !== 'function';
|
|
226
|
+
const staticSelectModeRef = useRef( staticSelectMode );
|
|
227
|
+
|
|
228
|
+
if ( staticSelectMode !== staticSelectModeRef.current ) {
|
|
229
|
+
const prevMode = staticSelectModeRef.current ? 'static' : 'mapping';
|
|
230
|
+
const nextMode = staticSelectMode ? 'static' : 'mapping';
|
|
231
|
+
throw new Error(
|
|
232
|
+
`Switching useSelect from ${ prevMode } to ${ nextMode } is not allowed`
|
|
246
233
|
);
|
|
234
|
+
}
|
|
247
235
|
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
};
|
|
256
|
-
// If you're tempted to eliminate the spread dependencies below don't do it!
|
|
257
|
-
// We're passing these in from the calling function and want to make sure we're
|
|
258
|
-
// examining every individual value inside the `deps` array.
|
|
259
|
-
}, [ registry, wrapSelect, hasMappingFunction, depsChangedFlag ] );
|
|
260
|
-
|
|
261
|
-
useDebugValue( mapOutput );
|
|
262
|
-
|
|
263
|
-
return hasMappingFunction ? mapOutput : registry.select( mapSelect );
|
|
236
|
+
/* eslint-disable react-hooks/rules-of-hooks */
|
|
237
|
+
// `staticSelectMode` is not allowed to change during the hook instance's,
|
|
238
|
+
// lifetime, so the rules of hooks are not really violated.
|
|
239
|
+
return staticSelectMode
|
|
240
|
+
? useStaticSelect( mapSelect )
|
|
241
|
+
: useMappingSelect( false, mapSelect, deps );
|
|
242
|
+
/* eslint-enable react-hooks/rules-of-hooks */
|
|
264
243
|
}
|
|
265
244
|
|
|
266
245
|
/**
|
|
@@ -279,117 +258,5 @@ export default function useSelect( mapSelect, deps ) {
|
|
|
279
258
|
* @return {Object} Data object returned by the `mapSelect` function.
|
|
280
259
|
*/
|
|
281
260
|
export function useSuspenseSelect( mapSelect, deps ) {
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
const registry = useRegistry();
|
|
285
|
-
const isAsync = useAsyncMode();
|
|
286
|
-
|
|
287
|
-
const latestRegistry = useRef( registry );
|
|
288
|
-
const latestMapSelect = useRef();
|
|
289
|
-
const latestIsAsync = useRef( isAsync );
|
|
290
|
-
const latestMapOutput = useRef();
|
|
291
|
-
const latestMapOutputError = useRef();
|
|
292
|
-
|
|
293
|
-
// Keep track of the stores being selected in the `mapSelect` function,
|
|
294
|
-
// and only subscribe to those stores later.
|
|
295
|
-
const listeningStores = useRef( [] );
|
|
296
|
-
const wrapSelect = useCallback(
|
|
297
|
-
( callback ) =>
|
|
298
|
-
registry.__unstableMarkListeningStores(
|
|
299
|
-
() => callback( registry.suspendSelect, registry ),
|
|
300
|
-
listeningStores
|
|
301
|
-
),
|
|
302
|
-
[ registry ]
|
|
303
|
-
);
|
|
304
|
-
|
|
305
|
-
// Generate a "flag" for used in the effect dependency array.
|
|
306
|
-
// It's different than just using `mapSelect` since deps could be undefined,
|
|
307
|
-
// in that case, we would still want to memoize it.
|
|
308
|
-
const depsChangedFlag = useMemo( () => ( {} ), deps || [] );
|
|
309
|
-
|
|
310
|
-
let mapOutput = latestMapOutput.current;
|
|
311
|
-
let mapOutputError = latestMapOutputError.current;
|
|
312
|
-
|
|
313
|
-
const hasReplacedRegistry = latestRegistry.current !== registry;
|
|
314
|
-
const hasReplacedMapSelect = latestMapSelect.current !== _mapSelect;
|
|
315
|
-
const hasLeftAsyncMode = latestIsAsync.current && ! isAsync;
|
|
316
|
-
|
|
317
|
-
let selectorRan = false;
|
|
318
|
-
if ( hasReplacedRegistry || hasReplacedMapSelect || hasLeftAsyncMode ) {
|
|
319
|
-
try {
|
|
320
|
-
mapOutput = wrapSelect( _mapSelect );
|
|
321
|
-
selectorRan = true;
|
|
322
|
-
} catch ( error ) {
|
|
323
|
-
mapOutputError = error;
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
useIsomorphicLayoutEffect( () => {
|
|
328
|
-
latestRegistry.current = registry;
|
|
329
|
-
latestMapSelect.current = _mapSelect;
|
|
330
|
-
latestIsAsync.current = isAsync;
|
|
331
|
-
if ( selectorRan ) {
|
|
332
|
-
latestMapOutput.current = mapOutput;
|
|
333
|
-
}
|
|
334
|
-
latestMapOutputError.current = mapOutputError;
|
|
335
|
-
} );
|
|
336
|
-
|
|
337
|
-
// React can sometimes clear the `useMemo` cache.
|
|
338
|
-
// We use the cache-stable `useMemoOne` to avoid
|
|
339
|
-
// losing queues.
|
|
340
|
-
const queueContext = useMemoOne( () => ( { queue: true } ), [ registry ] );
|
|
341
|
-
const [ , forceRender ] = useReducer( ( s ) => s + 1, 0 );
|
|
342
|
-
const isMounted = useRef( false );
|
|
343
|
-
|
|
344
|
-
useIsomorphicLayoutEffect( () => {
|
|
345
|
-
const onStoreChange = () => {
|
|
346
|
-
try {
|
|
347
|
-
const newMapOutput = wrapSelect( latestMapSelect.current );
|
|
348
|
-
|
|
349
|
-
if ( isShallowEqual( latestMapOutput.current, newMapOutput ) ) {
|
|
350
|
-
return;
|
|
351
|
-
}
|
|
352
|
-
latestMapOutput.current = newMapOutput;
|
|
353
|
-
} catch ( error ) {
|
|
354
|
-
latestMapOutputError.current = error;
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
forceRender();
|
|
358
|
-
};
|
|
359
|
-
|
|
360
|
-
const onChange = () => {
|
|
361
|
-
if ( ! isMounted.current ) {
|
|
362
|
-
return;
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
if ( latestIsAsync.current ) {
|
|
366
|
-
renderQueue.add( queueContext, onStoreChange );
|
|
367
|
-
} else {
|
|
368
|
-
onStoreChange();
|
|
369
|
-
}
|
|
370
|
-
};
|
|
371
|
-
|
|
372
|
-
// catch any possible state changes during mount before the subscription
|
|
373
|
-
// could be set.
|
|
374
|
-
onStoreChange();
|
|
375
|
-
|
|
376
|
-
const unsubscribers = listeningStores.current.map( ( storeName ) =>
|
|
377
|
-
registry.subscribe( onChange, storeName )
|
|
378
|
-
);
|
|
379
|
-
|
|
380
|
-
isMounted.current = true;
|
|
381
|
-
|
|
382
|
-
return () => {
|
|
383
|
-
// The return value of the subscribe function could be undefined if the store is a custom generic store.
|
|
384
|
-
unsubscribers.forEach( ( unsubscribe ) => unsubscribe?.() );
|
|
385
|
-
renderQueue.cancel( queueContext );
|
|
386
|
-
isMounted.current = false;
|
|
387
|
-
};
|
|
388
|
-
}, [ registry, wrapSelect, depsChangedFlag ] );
|
|
389
|
-
|
|
390
|
-
if ( mapOutputError ) {
|
|
391
|
-
throw mapOutputError;
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
return mapOutput;
|
|
261
|
+
return useMappingSelect( true, mapSelect, deps );
|
|
395
262
|
}
|