@wordpress/data 10.44.0 → 10.45.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 +6 -0
- package/README.md +26 -0
- package/build/index.cjs +3 -0
- package/build/index.cjs.map +2 -2
- package/build/redux-store/keyed-reducer.cjs +44 -0
- package/build/redux-store/keyed-reducer.cjs.map +7 -0
- package/build/redux-store/metadata/reducer.cjs +2 -1
- package/build/redux-store/metadata/reducer.cjs.map +2 -2
- package/build/redux-store/metadata/utils.cjs +0 -16
- package/build/redux-store/metadata/utils.cjs.map +2 -2
- package/build-module/index.mjs +2 -0
- package/build-module/index.mjs.map +2 -2
- package/build-module/redux-store/keyed-reducer.mjs +19 -0
- package/build-module/redux-store/keyed-reducer.mjs.map +7 -0
- package/build-module/redux-store/metadata/reducer.mjs +3 -2
- package/build-module/redux-store/metadata/reducer.mjs.map +2 -2
- package/build-module/redux-store/metadata/utils.mjs +0 -15
- package/build-module/redux-store/metadata/utils.mjs.map +2 -2
- package/build-types/index.d.ts +1 -0
- package/build-types/index.d.ts.map +1 -1
- package/build-types/redux-store/keyed-reducer.d.ts +26 -0
- package/build-types/redux-store/keyed-reducer.d.ts.map +1 -0
- package/build-types/redux-store/metadata/reducer.d.ts.map +1 -1
- package/build-types/redux-store/metadata/utils.d.ts +0 -12
- package/build-types/redux-store/metadata/utils.d.ts.map +1 -1
- package/package.json +9 -9
- package/src/index.ts +1 -0
- package/src/redux-store/keyed-reducer.ts +52 -0
- package/src/redux-store/metadata/reducer.ts +8 -4
- package/src/redux-store/metadata/test/utils.js +1 -38
- package/src/redux-store/metadata/utils.ts +0 -40
- package/src/redux-store/test/keyed-reducer.js +41 -0
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
## Unreleased
|
|
4
4
|
|
|
5
|
+
## 10.45.0 (2026-04-29)
|
|
6
|
+
|
|
7
|
+
### New Features
|
|
8
|
+
|
|
9
|
+
- Export `keyedReducer` higher-order reducer helper, consolidating duplicated copies previously maintained in `@wordpress/core-data` and `@wordpress/notices` ([#77364](https://github.com/WordPress/gutenberg/pull/77364)).
|
|
10
|
+
|
|
5
11
|
## 10.44.0 (2026-04-15)
|
|
6
12
|
|
|
7
13
|
## 10.43.0 (2026-04-01)
|
package/README.md
CHANGED
|
@@ -506,6 +506,32 @@ _Returns_
|
|
|
506
506
|
|
|
507
507
|
- `DispatchReturn< StoreNameOrDescriptor >`: Object containing the action creators.
|
|
508
508
|
|
|
509
|
+
### keyedReducer
|
|
510
|
+
|
|
511
|
+
Higher-order reducer creator which creates a combined reducer object, keyed by a property on the action object.
|
|
512
|
+
|
|
513
|
+
_Usage_
|
|
514
|
+
|
|
515
|
+
```js
|
|
516
|
+
import { keyedReducer } from '@wordpress/data';
|
|
517
|
+
|
|
518
|
+
const itemsByContext = keyedReducer( 'context' )( ( state = [], action ) => {
|
|
519
|
+
switch ( action.type ) {
|
|
520
|
+
case 'ADD_ITEM':
|
|
521
|
+
return [ ...state, action.item ];
|
|
522
|
+
}
|
|
523
|
+
return state;
|
|
524
|
+
} );
|
|
525
|
+
```
|
|
526
|
+
|
|
527
|
+
_Parameters_
|
|
528
|
+
|
|
529
|
+
- _actionProperty_ `string`: Action property by which to key object.
|
|
530
|
+
|
|
531
|
+
_Returns_
|
|
532
|
+
|
|
533
|
+
- Higher-order reducer.
|
|
534
|
+
|
|
509
535
|
### plugins
|
|
510
536
|
|
|
511
537
|
Object of available plugins to use with a registry.
|
package/build/index.cjs
CHANGED
|
@@ -41,6 +41,7 @@ __export(index_exports, {
|
|
|
41
41
|
createRegistrySelector: () => import_factory.createRegistrySelector,
|
|
42
42
|
createSelector: () => import_create_selector.createSelector,
|
|
43
43
|
dispatch: () => import_dispatch.dispatch,
|
|
44
|
+
keyedReducer: () => import_keyed_reducer.keyedReducer,
|
|
44
45
|
plugins: () => plugins,
|
|
45
46
|
register: () => register,
|
|
46
47
|
registerGenericStore: () => registerGenericStore,
|
|
@@ -74,6 +75,7 @@ var import_factory = require("./factory.cjs");
|
|
|
74
75
|
var import_create_selector = require("./create-selector.cjs");
|
|
75
76
|
var import_controls = require("./controls.cjs");
|
|
76
77
|
var import_redux_store2 = __toESM(require("./redux-store/index.cjs"));
|
|
78
|
+
var import_keyed_reducer = require("./redux-store/keyed-reducer.cjs");
|
|
77
79
|
var import_dispatch = require("./dispatch.cjs");
|
|
78
80
|
var import_select = require("./select.cjs");
|
|
79
81
|
var combineReducers = import_redux_store.combineReducers;
|
|
@@ -101,6 +103,7 @@ var register = (store) => import_default_registry.default.register(store);
|
|
|
101
103
|
createRegistrySelector,
|
|
102
104
|
createSelector,
|
|
103
105
|
dispatch,
|
|
106
|
+
keyedReducer,
|
|
104
107
|
plugins,
|
|
105
108
|
register,
|
|
106
109
|
registerGenericStore,
|
package/build/index.cjs.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/index.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * Internal dependencies\n */\nimport defaultRegistry from './default-registry';\nimport * as plugins from './plugins';\nimport { combineReducers as combineReducersModule } from './redux-store';\n\nimport type {\n\tStoreDescriptor,\n\tReduxStoreConfig,\n\tcombineReducers as CombineReducers,\n\tAnyConfig,\n\tCurriedSelectorsResolveOf,\n} from './types';\n\nexport { default as withSelect } from './components/with-select';\nexport { default as withDispatch } from './components/with-dispatch';\nexport { default as withRegistry } from './components/with-registry';\nexport {\n\tRegistryProvider,\n\tRegistryConsumer,\n\tuseRegistry,\n} from './components/registry-provider';\nexport {\n\tdefault as useSelect,\n\tuseSuspenseSelect,\n} from './components/use-select';\nexport { useDispatch } from './components/use-dispatch';\nexport { AsyncModeProvider } from './components/async-mode-provider';\nexport { createRegistry } from './registry';\nexport { createRegistrySelector, createRegistryControl } from './factory';\nexport { createSelector } from './create-selector';\nexport { controls } from './controls';\nexport { default as createReduxStore } from './redux-store';\nexport { dispatch } from './dispatch';\nexport { select } from './select';\n\nexport type * from './types';\n\n/**\n * Object of available plugins to use with a registry.\n *\n * @see [use](#use)\n */\nexport { plugins };\n\n/**\n * The combineReducers helper function turns an object whose values are different\n * reducing functions into a single reducing function you can pass to registerReducer.\n *\n * @type {import('./types').combineReducers}\n * @param {Object} reducers An object whose values correspond to different reducing\n * functions that need to be combined into one.\n *\n * @example\n * ```js\n * import { combineReducers, createReduxStore, register } from '@wordpress/data';\n *\n * const prices = ( state = {}, action ) => {\n * \treturn action.type === 'SET_PRICE' ?\n * \t\t{\n * \t\t\t...state,\n * \t\t\t[ action.item ]: action.price,\n * \t\t} :\n * \t\tstate;\n * };\n *\n * const discountPercent = ( state = 0, action ) => {\n * \treturn action.type === 'START_SALE' ?\n * \t\taction.discountPercent :\n * \t\tstate;\n * };\n *\n * const store = createReduxStore( 'my-shop', {\n * \treducer: combineReducers( {\n * \t\tprices,\n * \t\tdiscountPercent,\n * \t} ),\n * } );\n * register( store );\n * ```\n *\n * @return {Function} A reducer that invokes every reducer inside the reducers\n * object, and constructs a state object with the same shape.\n */\nexport const combineReducers =\n\tcombineReducersModule as unknown as CombineReducers;\n\n/**\n * Given a store descriptor, returns an object containing the store's selectors\n * pre-bound to state so that you only need to supply additional arguments, and\n * modified so that they return promises that resolve to their eventual values,\n * after any resolvers have ran.\n *\n * @param {StoreDescriptor|string} storeNameOrDescriptor The store descriptor. The legacy calling\n * convention of passing the store name is\n * also supported.\n *\n * @example\n * ```js\n * import { resolveSelect } from '@wordpress/data';\n * import { store as myCustomStore } from 'my-custom-store';\n *\n * resolveSelect( myCustomStore ).getPrice( 'hammer' ).then(console.log)\n * ```\n *\n * @return Object containing the store's promise-wrapped selectors.\n */\nexport function resolveSelect< T extends StoreDescriptor< AnyConfig > >(\n\tstoreNameOrDescriptor: T | string\n): CurriedSelectorsResolveOf< T > {\n\treturn defaultRegistry.resolveSelect(\n\t\tstoreNameOrDescriptor\n\t) as CurriedSelectorsResolveOf< T >;\n}\n\n/**\n * Given a store descriptor, returns an object containing the store's selectors pre-bound to state\n * so that you only need to supply additional arguments, and modified so that they throw promises\n * in case the selector is not resolved yet.\n *\n * @param {StoreDescriptor|string} storeNameOrDescriptor The store descriptor. The legacy calling\n * convention of passing the store name is\n * also supported.\n *\n * @return {Object} Object containing the store's suspense-wrapped selectors.\n */\nexport const suspendSelect = (\n\tstoreNameOrDescriptor:\n\t\t| string\n\t\t| StoreDescriptor< ReduxStoreConfig< any, any, any > >\n): any => defaultRegistry.suspendSelect( storeNameOrDescriptor );\n\n/**\n * Given a listener function, the function will be called any time the state value\n * of one of the registered stores has changed. If you specify the optional\n * `storeNameOrDescriptor` parameter, the listener function will be called only\n * on updates on that one specific registered store.\n *\n * This function returns an `unsubscribe` function used to stop the subscription.\n *\n * @param {Function} listener Callback function.\n * @param {string|StoreDescriptor?} storeNameOrDescriptor Optional store name.\n *\n * @example\n * ```js\n * import { subscribe } from '@wordpress/data';\n *\n * const unsubscribe = subscribe( () => {\n * \t// You could use this opportunity to test whether the derived result of a\n * \t// selector has subsequently changed as the result of a state update.\n * } );\n *\n * // Later, if necessary...\n * unsubscribe();\n * ```\n */\nexport const subscribe = (\n\tlistener: () => void,\n\tstoreNameOrDescriptor?:\n\t\t| string\n\t\t| StoreDescriptor< ReduxStoreConfig< any, any, any > >\n): ( () => void ) =>\n\tdefaultRegistry.subscribe( listener, storeNameOrDescriptor );\n\n/**\n * Registers a generic store instance.\n *\n * @deprecated Use `register( storeDescriptor )` instead.\n *\n * @param {string} name Store registry name.\n * @param {Object} store Store instance (`{ getSelectors, getActions, subscribe }`).\n */\nexport const registerGenericStore: Function =\n\tdefaultRegistry.registerGenericStore;\n\n/**\n * Registers a standard `@wordpress/data` store.\n *\n * @deprecated Use `register` instead.\n *\n * @param {string} storeName Unique namespace identifier for the store.\n * @param {Object} options Store description (reducer, actions, selectors, resolvers).\n *\n * @return {Object} Registered store object.\n */\nexport const registerStore: Function = defaultRegistry.registerStore;\n\n/**\n * Extends a registry to inherit functionality provided by a given plugin. A\n * plugin is an object with properties aligning to that of a registry, merged\n * to extend the default registry behavior.\n *\n * @param {Object} plugin Plugin object.\n */\nexport const use: any = defaultRegistry.use;\n\n/**\n * Registers a standard `@wordpress/data` store descriptor.\n *\n * @example\n * ```js\n * import { createReduxStore, register } from '@wordpress/data';\n *\n * const store = createReduxStore( 'demo', {\n * reducer: ( state = 'OK' ) => state,\n * selectors: {\n * getValue: ( state ) => state,\n * },\n * } );\n * register( store );\n * ```\n *\n * @param {StoreDescriptor} store Store descriptor.\n */\nexport const register = (\n\tstore: StoreDescriptor< ReduxStoreConfig< any, any, any > >\n): void => defaultRegistry.register( store );\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,8BAA4B;AAC5B,cAAyB;AACzB,yBAAyD;AAUzD,yBAAsC;AACtC,2BAAwC;AACxC,2BAAwC;AACxC,+BAIO;AACP,wBAGO;AACP,0BAA4B;AAC5B,iCAAkC;AAClC,sBAA+B;AAC/B,qBAA8D;AAC9D,6BAA+B;AAC/B,sBAAyB;AACzB,IAAAA,sBAA4C;AAC5C,sBAAyB;AACzB,oBAAuB;AAkDhB,IAAM,kBACZ,mBAAAC;AAsBM,SAAS,cACf,uBACiC;AACjC,SAAO,wBAAAC,QAAgB;AAAA,IACtB;AAAA,EACD;AACD;AAaO,IAAM,gBAAgB,CAC5B,0BAGS,wBAAAA,QAAgB,cAAe,qBAAsB;AA0BxD,IAAM,YAAY,CACxB,UACA,0BAIA,wBAAAA,QAAgB,UAAW,UAAU,qBAAsB;AAUrD,IAAM,uBACZ,wBAAAA,QAAgB;AAYV,IAAM,gBAA0B,wBAAAA,QAAgB;AAShD,IAAM,MAAW,wBAAAA,QAAgB;AAoBjC,IAAM,WAAW,CACvB,UACU,wBAAAA,QAAgB,SAAU,KAAM;",
|
|
4
|
+
"sourcesContent": ["/**\n * Internal dependencies\n */\nimport defaultRegistry from './default-registry';\nimport * as plugins from './plugins';\nimport { combineReducers as combineReducersModule } from './redux-store';\n\nimport type {\n\tStoreDescriptor,\n\tReduxStoreConfig,\n\tcombineReducers as CombineReducers,\n\tAnyConfig,\n\tCurriedSelectorsResolveOf,\n} from './types';\n\nexport { default as withSelect } from './components/with-select';\nexport { default as withDispatch } from './components/with-dispatch';\nexport { default as withRegistry } from './components/with-registry';\nexport {\n\tRegistryProvider,\n\tRegistryConsumer,\n\tuseRegistry,\n} from './components/registry-provider';\nexport {\n\tdefault as useSelect,\n\tuseSuspenseSelect,\n} from './components/use-select';\nexport { useDispatch } from './components/use-dispatch';\nexport { AsyncModeProvider } from './components/async-mode-provider';\nexport { createRegistry } from './registry';\nexport { createRegistrySelector, createRegistryControl } from './factory';\nexport { createSelector } from './create-selector';\nexport { controls } from './controls';\nexport { default as createReduxStore } from './redux-store';\nexport { keyedReducer } from './redux-store/keyed-reducer';\nexport { dispatch } from './dispatch';\nexport { select } from './select';\n\nexport type * from './types';\n\n/**\n * Object of available plugins to use with a registry.\n *\n * @see [use](#use)\n */\nexport { plugins };\n\n/**\n * The combineReducers helper function turns an object whose values are different\n * reducing functions into a single reducing function you can pass to registerReducer.\n *\n * @type {import('./types').combineReducers}\n * @param {Object} reducers An object whose values correspond to different reducing\n * functions that need to be combined into one.\n *\n * @example\n * ```js\n * import { combineReducers, createReduxStore, register } from '@wordpress/data';\n *\n * const prices = ( state = {}, action ) => {\n * \treturn action.type === 'SET_PRICE' ?\n * \t\t{\n * \t\t\t...state,\n * \t\t\t[ action.item ]: action.price,\n * \t\t} :\n * \t\tstate;\n * };\n *\n * const discountPercent = ( state = 0, action ) => {\n * \treturn action.type === 'START_SALE' ?\n * \t\taction.discountPercent :\n * \t\tstate;\n * };\n *\n * const store = createReduxStore( 'my-shop', {\n * \treducer: combineReducers( {\n * \t\tprices,\n * \t\tdiscountPercent,\n * \t} ),\n * } );\n * register( store );\n * ```\n *\n * @return {Function} A reducer that invokes every reducer inside the reducers\n * object, and constructs a state object with the same shape.\n */\nexport const combineReducers =\n\tcombineReducersModule as unknown as CombineReducers;\n\n/**\n * Given a store descriptor, returns an object containing the store's selectors\n * pre-bound to state so that you only need to supply additional arguments, and\n * modified so that they return promises that resolve to their eventual values,\n * after any resolvers have ran.\n *\n * @param {StoreDescriptor|string} storeNameOrDescriptor The store descriptor. The legacy calling\n * convention of passing the store name is\n * also supported.\n *\n * @example\n * ```js\n * import { resolveSelect } from '@wordpress/data';\n * import { store as myCustomStore } from 'my-custom-store';\n *\n * resolveSelect( myCustomStore ).getPrice( 'hammer' ).then(console.log)\n * ```\n *\n * @return Object containing the store's promise-wrapped selectors.\n */\nexport function resolveSelect< T extends StoreDescriptor< AnyConfig > >(\n\tstoreNameOrDescriptor: T | string\n): CurriedSelectorsResolveOf< T > {\n\treturn defaultRegistry.resolveSelect(\n\t\tstoreNameOrDescriptor\n\t) as CurriedSelectorsResolveOf< T >;\n}\n\n/**\n * Given a store descriptor, returns an object containing the store's selectors pre-bound to state\n * so that you only need to supply additional arguments, and modified so that they throw promises\n * in case the selector is not resolved yet.\n *\n * @param {StoreDescriptor|string} storeNameOrDescriptor The store descriptor. The legacy calling\n * convention of passing the store name is\n * also supported.\n *\n * @return {Object} Object containing the store's suspense-wrapped selectors.\n */\nexport const suspendSelect = (\n\tstoreNameOrDescriptor:\n\t\t| string\n\t\t| StoreDescriptor< ReduxStoreConfig< any, any, any > >\n): any => defaultRegistry.suspendSelect( storeNameOrDescriptor );\n\n/**\n * Given a listener function, the function will be called any time the state value\n * of one of the registered stores has changed. If you specify the optional\n * `storeNameOrDescriptor` parameter, the listener function will be called only\n * on updates on that one specific registered store.\n *\n * This function returns an `unsubscribe` function used to stop the subscription.\n *\n * @param {Function} listener Callback function.\n * @param {string|StoreDescriptor?} storeNameOrDescriptor Optional store name.\n *\n * @example\n * ```js\n * import { subscribe } from '@wordpress/data';\n *\n * const unsubscribe = subscribe( () => {\n * \t// You could use this opportunity to test whether the derived result of a\n * \t// selector has subsequently changed as the result of a state update.\n * } );\n *\n * // Later, if necessary...\n * unsubscribe();\n * ```\n */\nexport const subscribe = (\n\tlistener: () => void,\n\tstoreNameOrDescriptor?:\n\t\t| string\n\t\t| StoreDescriptor< ReduxStoreConfig< any, any, any > >\n): ( () => void ) =>\n\tdefaultRegistry.subscribe( listener, storeNameOrDescriptor );\n\n/**\n * Registers a generic store instance.\n *\n * @deprecated Use `register( storeDescriptor )` instead.\n *\n * @param {string} name Store registry name.\n * @param {Object} store Store instance (`{ getSelectors, getActions, subscribe }`).\n */\nexport const registerGenericStore: Function =\n\tdefaultRegistry.registerGenericStore;\n\n/**\n * Registers a standard `@wordpress/data` store.\n *\n * @deprecated Use `register` instead.\n *\n * @param {string} storeName Unique namespace identifier for the store.\n * @param {Object} options Store description (reducer, actions, selectors, resolvers).\n *\n * @return {Object} Registered store object.\n */\nexport const registerStore: Function = defaultRegistry.registerStore;\n\n/**\n * Extends a registry to inherit functionality provided by a given plugin. A\n * plugin is an object with properties aligning to that of a registry, merged\n * to extend the default registry behavior.\n *\n * @param {Object} plugin Plugin object.\n */\nexport const use: any = defaultRegistry.use;\n\n/**\n * Registers a standard `@wordpress/data` store descriptor.\n *\n * @example\n * ```js\n * import { createReduxStore, register } from '@wordpress/data';\n *\n * const store = createReduxStore( 'demo', {\n * reducer: ( state = 'OK' ) => state,\n * selectors: {\n * getValue: ( state ) => state,\n * },\n * } );\n * register( store );\n * ```\n *\n * @param {StoreDescriptor} store Store descriptor.\n */\nexport const register = (\n\tstore: StoreDescriptor< ReduxStoreConfig< any, any, any > >\n): void => defaultRegistry.register( store );\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,8BAA4B;AAC5B,cAAyB;AACzB,yBAAyD;AAUzD,yBAAsC;AACtC,2BAAwC;AACxC,2BAAwC;AACxC,+BAIO;AACP,wBAGO;AACP,0BAA4B;AAC5B,iCAAkC;AAClC,sBAA+B;AAC/B,qBAA8D;AAC9D,6BAA+B;AAC/B,sBAAyB;AACzB,IAAAA,sBAA4C;AAC5C,2BAA6B;AAC7B,sBAAyB;AACzB,oBAAuB;AAkDhB,IAAM,kBACZ,mBAAAC;AAsBM,SAAS,cACf,uBACiC;AACjC,SAAO,wBAAAC,QAAgB;AAAA,IACtB;AAAA,EACD;AACD;AAaO,IAAM,gBAAgB,CAC5B,0BAGS,wBAAAA,QAAgB,cAAe,qBAAsB;AA0BxD,IAAM,YAAY,CACxB,UACA,0BAIA,wBAAAA,QAAgB,UAAW,UAAU,qBAAsB;AAUrD,IAAM,uBACZ,wBAAAA,QAAgB;AAYV,IAAM,gBAA0B,wBAAAA,QAAgB;AAShD,IAAM,MAAW,wBAAAA,QAAgB;AAoBjC,IAAM,WAAW,CACvB,UACU,wBAAAA,QAAgB,SAAU,KAAM;",
|
|
6
6
|
"names": ["import_redux_store", "combineReducersModule", "defaultRegistry"]
|
|
7
7
|
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// packages/data/src/redux-store/keyed-reducer.ts
|
|
21
|
+
var keyed_reducer_exports = {};
|
|
22
|
+
__export(keyed_reducer_exports, {
|
|
23
|
+
keyedReducer: () => keyedReducer
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(keyed_reducer_exports);
|
|
26
|
+
var keyedReducer = (actionProperty) => (reducer) => (state = {}, action) => {
|
|
27
|
+
const key = action[actionProperty];
|
|
28
|
+
if (key === void 0) {
|
|
29
|
+
return state;
|
|
30
|
+
}
|
|
31
|
+
const nextKeyState = reducer(state[key], action);
|
|
32
|
+
if (nextKeyState === state[key]) {
|
|
33
|
+
return state;
|
|
34
|
+
}
|
|
35
|
+
return {
|
|
36
|
+
...state,
|
|
37
|
+
[key]: nextKeyState
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
41
|
+
0 && (module.exports = {
|
|
42
|
+
keyedReducer
|
|
43
|
+
});
|
|
44
|
+
//# sourceMappingURL=keyed-reducer.cjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/redux-store/keyed-reducer.ts"],
|
|
4
|
+
"sourcesContent": ["/**\n * External dependencies\n */\nimport type { AnyAction, Reducer } from 'redux';\n\n/**\n * Higher-order reducer creator which creates a combined reducer object, keyed\n * by a property on the action object.\n *\n * @param actionProperty Action property by which to key object.\n * @return Higher-order reducer.\n *\n * @example\n * ```js\n * import { keyedReducer } from '@wordpress/data';\n *\n * const itemsByContext = keyedReducer( 'context' )( ( state = [], action ) => {\n * switch ( action.type ) {\n * case 'ADD_ITEM':\n * return [ ...state, action.item ];\n * }\n * return state;\n * } );\n * ```\n */\nexport const keyedReducer =\n\t< TState extends unknown, TAction extends AnyAction >(\n\t\tactionProperty: string\n\t) =>\n\t(\n\t\treducer: Reducer< TState, TAction >\n\t): Reducer< Record< string, TState >, TAction > =>\n\t( state: Record< string, TState > = {}, action ) => {\n\t\t// Retrieve subkey from action. Do not track if undefined; useful for cases\n\t\t// where reducer is scoped by action shape.\n\t\tconst key = action[ actionProperty ];\n\t\tif ( key === undefined ) {\n\t\t\treturn state;\n\t\t}\n\n\t\t// Avoid updating state if unchanged. Note that this also accounts for a\n\t\t// reducer which returns undefined on a key which is not yet tracked.\n\t\tconst nextKeyState = reducer( state[ key ], action );\n\t\tif ( nextKeyState === state[ key ] ) {\n\t\t\treturn state;\n\t\t}\n\n\t\treturn {\n\t\t\t...state,\n\t\t\t[ key ]: nextKeyState,\n\t\t};\n\t};\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAyBO,IAAM,eACZ,CACC,mBAED,CACC,YAED,CAAE,QAAkC,CAAC,GAAG,WAAY;AAGnD,QAAM,MAAM,OAAQ,cAAe;AACnC,MAAK,QAAQ,QAAY;AACxB,WAAO;AAAA,EACR;AAIA,QAAM,eAAe,QAAS,MAAO,GAAI,GAAG,MAAO;AACnD,MAAK,iBAAiB,MAAO,GAAI,GAAI;AACpC,WAAO;AAAA,EACR;AAEA,SAAO;AAAA,IACN,GAAG;AAAA,IACH,CAAE,GAAI,GAAG;AAAA,EACV;AACD;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -34,8 +34,9 @@ __export(reducer_exports, {
|
|
|
34
34
|
});
|
|
35
35
|
module.exports = __toCommonJS(reducer_exports);
|
|
36
36
|
var import_equivalent_key_map = __toESM(require("equivalent-key-map"));
|
|
37
|
+
var import_keyed_reducer = require("../keyed-reducer.cjs");
|
|
37
38
|
var import_utils = require("./utils.cjs");
|
|
38
|
-
var subKeysIsResolved = (0,
|
|
39
|
+
var subKeysIsResolved = (0, import_keyed_reducer.keyedReducer)("selectorName")((state = new import_equivalent_key_map.default(), action) => {
|
|
39
40
|
switch (action.type) {
|
|
40
41
|
case "START_RESOLUTION": {
|
|
41
42
|
const nextState = new import_equivalent_key_map.default(state);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/redux-store/metadata/reducer.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * External dependencies\n */\nimport EquivalentKeyMap from 'equivalent-key-map';\nimport type { Reducer } from 'redux';\n\nimport type {\n\tstartResolution,\n\tfinishResolution,\n\tfailResolution,\n\tstartResolutions,\n\tfinishResolutions,\n\tfailResolutions,\n\tinvalidateResolution,\n\tinvalidateResolutionForStore,\n\tinvalidateResolutionForStoreSelector,\n} from './actions';\n\n/**\n * Internal dependencies\n */\nimport { selectorArgsToStateKey
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,gCAA6B;AAkB7B,
|
|
4
|
+
"sourcesContent": ["/**\n * External dependencies\n */\nimport EquivalentKeyMap from 'equivalent-key-map';\nimport type { Reducer } from 'redux';\n\nimport type {\n\tstartResolution,\n\tfinishResolution,\n\tfailResolution,\n\tstartResolutions,\n\tfinishResolutions,\n\tfailResolutions,\n\tinvalidateResolution,\n\tinvalidateResolutionForStore,\n\tinvalidateResolutionForStoreSelector,\n} from './actions';\n\n/**\n * Internal dependencies\n */\nimport { keyedReducer } from '../keyed-reducer';\nimport { selectorArgsToStateKey } from './utils';\n\ntype Action =\n\t| ReturnType< typeof startResolution >\n\t| ReturnType< typeof finishResolution >\n\t| ReturnType< typeof failResolution >\n\t| ReturnType< typeof startResolutions >\n\t| ReturnType< typeof finishResolutions >\n\t| ReturnType< typeof failResolutions >\n\t| ReturnType< typeof invalidateResolution >\n\t| ReturnType< typeof invalidateResolutionForStore >\n\t| ReturnType< typeof invalidateResolutionForStoreSelector >;\n\ntype StateKey = unknown[] | unknown;\nexport type StateValue =\n\t| { status: 'resolving' | 'finished' }\n\t| { status: 'error'; error: Error | unknown };\n\nexport type Status = StateValue[ 'status' ];\nexport type State = EquivalentKeyMap< StateKey, StateValue >;\n\n/**\n * Reducer function returning next state for selector resolution of\n * subkeys, object form:\n *\n * selectorName -> EquivalentKeyMap<Array,boolean>\n */\nconst subKeysIsResolved: Reducer<\n\tRecord< string, State >,\n\tAction\n> = keyedReducer< State, Action >( 'selectorName' )( (\n\tstate = new EquivalentKeyMap(),\n\taction: Action\n) => {\n\tswitch ( action.type ) {\n\t\tcase 'START_RESOLUTION': {\n\t\t\tconst nextState = new EquivalentKeyMap( state );\n\t\t\tnextState.set( selectorArgsToStateKey( action.args ), {\n\t\t\t\tstatus: 'resolving',\n\t\t\t} );\n\t\t\treturn nextState;\n\t\t}\n\t\tcase 'FINISH_RESOLUTION': {\n\t\t\tconst nextState = new EquivalentKeyMap( state );\n\t\t\tnextState.set( selectorArgsToStateKey( action.args ), {\n\t\t\t\tstatus: 'finished',\n\t\t\t} );\n\t\t\treturn nextState;\n\t\t}\n\t\tcase 'FAIL_RESOLUTION': {\n\t\t\tconst nextState = new EquivalentKeyMap( state );\n\t\t\tnextState.set( selectorArgsToStateKey( action.args ), {\n\t\t\t\tstatus: 'error',\n\t\t\t\terror: action.error,\n\t\t\t} );\n\t\t\treturn nextState;\n\t\t}\n\t\tcase 'START_RESOLUTIONS': {\n\t\t\tconst nextState = new EquivalentKeyMap( state );\n\t\t\tfor ( const resolutionArgs of action.args ) {\n\t\t\t\tnextState.set( selectorArgsToStateKey( resolutionArgs ), {\n\t\t\t\t\tstatus: 'resolving',\n\t\t\t\t} );\n\t\t\t}\n\t\t\treturn nextState;\n\t\t}\n\t\tcase 'FINISH_RESOLUTIONS': {\n\t\t\tconst nextState = new EquivalentKeyMap( state );\n\t\t\tfor ( const resolutionArgs of action.args ) {\n\t\t\t\tnextState.set( selectorArgsToStateKey( resolutionArgs ), {\n\t\t\t\t\tstatus: 'finished',\n\t\t\t\t} );\n\t\t\t}\n\t\t\treturn nextState;\n\t\t}\n\t\tcase 'FAIL_RESOLUTIONS': {\n\t\t\tconst nextState = new EquivalentKeyMap( state );\n\t\t\taction.args.forEach( ( resolutionArgs, idx ) => {\n\t\t\t\tconst resolutionState: StateValue = {\n\t\t\t\t\tstatus: 'error',\n\t\t\t\t\terror: undefined,\n\t\t\t\t};\n\n\t\t\t\tconst error = action.errors[ idx ];\n\t\t\t\tif ( error ) {\n\t\t\t\t\tresolutionState.error = error;\n\t\t\t\t}\n\n\t\t\t\tnextState.set(\n\t\t\t\t\tselectorArgsToStateKey( resolutionArgs as unknown[] ),\n\t\t\t\t\tresolutionState\n\t\t\t\t);\n\t\t\t} );\n\t\t\treturn nextState;\n\t\t}\n\t\tcase 'INVALIDATE_RESOLUTION': {\n\t\t\tconst nextState = new EquivalentKeyMap( state );\n\t\t\tnextState.delete( selectorArgsToStateKey( action.args ) );\n\t\t\treturn nextState;\n\t\t}\n\t}\n\treturn state;\n} );\n\n/**\n * Reducer function returning next state for selector resolution, object form:\n *\n * selectorName -> EquivalentKeyMap<Array, boolean>\n *\n * @param state Current state.\n * @param action Dispatched action.\n *\n * @return Next state.\n */\nconst isResolved = ( state: Record< string, State > = {}, action: Action ) => {\n\tswitch ( action.type ) {\n\t\tcase 'INVALIDATE_RESOLUTION_FOR_STORE':\n\t\t\treturn {};\n\t\tcase 'INVALIDATE_RESOLUTION_FOR_STORE_SELECTOR': {\n\t\t\tif ( action.selectorName in state ) {\n\t\t\t\tconst {\n\t\t\t\t\t[ action.selectorName ]: removedSelector,\n\t\t\t\t\t...restState\n\t\t\t\t} = state;\n\t\t\t\treturn restState;\n\t\t\t}\n\t\t\treturn state;\n\t\t}\n\t\tcase 'START_RESOLUTION':\n\t\tcase 'FINISH_RESOLUTION':\n\t\tcase 'FAIL_RESOLUTION':\n\t\tcase 'START_RESOLUTIONS':\n\t\tcase 'FINISH_RESOLUTIONS':\n\t\tcase 'FAIL_RESOLUTIONS':\n\t\tcase 'INVALIDATE_RESOLUTION':\n\t\t\treturn subKeysIsResolved( state, action );\n\t\tdefault:\n\t\t\treturn state;\n\t}\n};\n\nexport default isResolved;\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,gCAA6B;AAkB7B,2BAA6B;AAC7B,mBAAuC;AA2BvC,IAAM,wBAGF,mCAA+B,cAAe,EAAG,CACpD,QAAQ,IAAI,0BAAAA,QAAiB,GAC7B,WACI;AACJ,UAAS,OAAO,MAAO;AAAA,IACtB,KAAK,oBAAoB;AACxB,YAAM,YAAY,IAAI,0BAAAA,QAAkB,KAAM;AAC9C,gBAAU,QAAK,qCAAwB,OAAO,IAAK,GAAG;AAAA,QACrD,QAAQ;AAAA,MACT,CAAE;AACF,aAAO;AAAA,IACR;AAAA,IACA,KAAK,qBAAqB;AACzB,YAAM,YAAY,IAAI,0BAAAA,QAAkB,KAAM;AAC9C,gBAAU,QAAK,qCAAwB,OAAO,IAAK,GAAG;AAAA,QACrD,QAAQ;AAAA,MACT,CAAE;AACF,aAAO;AAAA,IACR;AAAA,IACA,KAAK,mBAAmB;AACvB,YAAM,YAAY,IAAI,0BAAAA,QAAkB,KAAM;AAC9C,gBAAU,QAAK,qCAAwB,OAAO,IAAK,GAAG;AAAA,QACrD,QAAQ;AAAA,QACR,OAAO,OAAO;AAAA,MACf,CAAE;AACF,aAAO;AAAA,IACR;AAAA,IACA,KAAK,qBAAqB;AACzB,YAAM,YAAY,IAAI,0BAAAA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,IACA,KAAK;AAAA,IACL,KAAK;AAAA,IACL,KAAK;AAAA,IACL,KAAK;AAAA,IACL,KAAK;AAAA,IACL,KAAK;AAAA,IACL,KAAK;AACJ,aAAO,kBAAmB,OAAO,MAAO;AAAA,IACzC;AACC,aAAO;AAAA,EACT;AACD;AAEA,IAAO,kBAAQ;",
|
|
6
6
|
"names": ["EquivalentKeyMap"]
|
|
7
7
|
}
|
|
@@ -20,24 +20,9 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// packages/data/src/redux-store/metadata/utils.ts
|
|
21
21
|
var utils_exports = {};
|
|
22
22
|
__export(utils_exports, {
|
|
23
|
-
onSubKey: () => onSubKey,
|
|
24
23
|
selectorArgsToStateKey: () => selectorArgsToStateKey
|
|
25
24
|
});
|
|
26
25
|
module.exports = __toCommonJS(utils_exports);
|
|
27
|
-
var onSubKey = (actionProperty) => (reducer) => (state = {}, action) => {
|
|
28
|
-
const key = action[actionProperty];
|
|
29
|
-
if (key === void 0) {
|
|
30
|
-
return state;
|
|
31
|
-
}
|
|
32
|
-
const nextKeyState = reducer(state[key], action);
|
|
33
|
-
if (nextKeyState === state[key]) {
|
|
34
|
-
return state;
|
|
35
|
-
}
|
|
36
|
-
return {
|
|
37
|
-
...state,
|
|
38
|
-
[key]: nextKeyState
|
|
39
|
-
};
|
|
40
|
-
};
|
|
41
26
|
function selectorArgsToStateKey(args) {
|
|
42
27
|
if (args === void 0 || args === null) {
|
|
43
28
|
return [];
|
|
@@ -51,7 +36,6 @@ function selectorArgsToStateKey(args) {
|
|
|
51
36
|
}
|
|
52
37
|
// Annotate the CommonJS export names for ESM import in node:
|
|
53
38
|
0 && (module.exports = {
|
|
54
|
-
onSubKey,
|
|
55
39
|
selectorArgsToStateKey
|
|
56
40
|
});
|
|
57
41
|
//# sourceMappingURL=utils.cjs.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/redux-store/metadata/utils.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n *
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;
|
|
4
|
+
"sourcesContent": ["/**\n * Normalize selector argument array by defaulting `undefined` value to an empty array\n * and removing trailing `undefined` values.\n *\n * @param args Selector argument array\n * @return Normalized state key array\n */\nexport function selectorArgsToStateKey( args: unknown[] | null | undefined ) {\n\tif ( args === undefined || args === null ) {\n\t\treturn [];\n\t}\n\n\tconst len = args.length;\n\tlet idx = len;\n\twhile ( idx > 0 && args[ idx - 1 ] === undefined ) {\n\t\tidx--;\n\t}\n\treturn idx === len ? args : args.slice( 0, idx );\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAOO,SAAS,uBAAwB,MAAqC;AAC5E,MAAK,SAAS,UAAa,SAAS,MAAO;AAC1C,WAAO,CAAC;AAAA,EACT;AAEA,QAAM,MAAM,KAAK;AACjB,MAAI,MAAM;AACV,SAAQ,MAAM,KAAK,KAAM,MAAM,CAAE,MAAM,QAAY;AAClD;AAAA,EACD;AACA,SAAO,QAAQ,MAAM,OAAO,KAAK,MAAO,GAAG,GAAI;AAChD;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/build-module/index.mjs
CHANGED
|
@@ -21,6 +21,7 @@ import { createRegistrySelector, createRegistryControl } from "./factory.mjs";
|
|
|
21
21
|
import { createSelector } from "./create-selector.mjs";
|
|
22
22
|
import { controls } from "./controls.mjs";
|
|
23
23
|
import { default as default6 } from "./redux-store/index.mjs";
|
|
24
|
+
import { keyedReducer } from "./redux-store/keyed-reducer.mjs";
|
|
24
25
|
import { dispatch } from "./dispatch.mjs";
|
|
25
26
|
import { select } from "./select.mjs";
|
|
26
27
|
var combineReducers = combineReducersModule;
|
|
@@ -47,6 +48,7 @@ export {
|
|
|
47
48
|
createRegistrySelector,
|
|
48
49
|
createSelector,
|
|
49
50
|
dispatch,
|
|
51
|
+
keyedReducer,
|
|
50
52
|
plugins,
|
|
51
53
|
register,
|
|
52
54
|
registerGenericStore,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/index.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * Internal dependencies\n */\nimport defaultRegistry from './default-registry';\nimport * as plugins from './plugins';\nimport { combineReducers as combineReducersModule } from './redux-store';\n\nimport type {\n\tStoreDescriptor,\n\tReduxStoreConfig,\n\tcombineReducers as CombineReducers,\n\tAnyConfig,\n\tCurriedSelectorsResolveOf,\n} from './types';\n\nexport { default as withSelect } from './components/with-select';\nexport { default as withDispatch } from './components/with-dispatch';\nexport { default as withRegistry } from './components/with-registry';\nexport {\n\tRegistryProvider,\n\tRegistryConsumer,\n\tuseRegistry,\n} from './components/registry-provider';\nexport {\n\tdefault as useSelect,\n\tuseSuspenseSelect,\n} from './components/use-select';\nexport { useDispatch } from './components/use-dispatch';\nexport { AsyncModeProvider } from './components/async-mode-provider';\nexport { createRegistry } from './registry';\nexport { createRegistrySelector, createRegistryControl } from './factory';\nexport { createSelector } from './create-selector';\nexport { controls } from './controls';\nexport { default as createReduxStore } from './redux-store';\nexport { dispatch } from './dispatch';\nexport { select } from './select';\n\nexport type * from './types';\n\n/**\n * Object of available plugins to use with a registry.\n *\n * @see [use](#use)\n */\nexport { plugins };\n\n/**\n * The combineReducers helper function turns an object whose values are different\n * reducing functions into a single reducing function you can pass to registerReducer.\n *\n * @type {import('./types').combineReducers}\n * @param {Object} reducers An object whose values correspond to different reducing\n * functions that need to be combined into one.\n *\n * @example\n * ```js\n * import { combineReducers, createReduxStore, register } from '@wordpress/data';\n *\n * const prices = ( state = {}, action ) => {\n * \treturn action.type === 'SET_PRICE' ?\n * \t\t{\n * \t\t\t...state,\n * \t\t\t[ action.item ]: action.price,\n * \t\t} :\n * \t\tstate;\n * };\n *\n * const discountPercent = ( state = 0, action ) => {\n * \treturn action.type === 'START_SALE' ?\n * \t\taction.discountPercent :\n * \t\tstate;\n * };\n *\n * const store = createReduxStore( 'my-shop', {\n * \treducer: combineReducers( {\n * \t\tprices,\n * \t\tdiscountPercent,\n * \t} ),\n * } );\n * register( store );\n * ```\n *\n * @return {Function} A reducer that invokes every reducer inside the reducers\n * object, and constructs a state object with the same shape.\n */\nexport const combineReducers =\n\tcombineReducersModule as unknown as CombineReducers;\n\n/**\n * Given a store descriptor, returns an object containing the store's selectors\n * pre-bound to state so that you only need to supply additional arguments, and\n * modified so that they return promises that resolve to their eventual values,\n * after any resolvers have ran.\n *\n * @param {StoreDescriptor|string} storeNameOrDescriptor The store descriptor. The legacy calling\n * convention of passing the store name is\n * also supported.\n *\n * @example\n * ```js\n * import { resolveSelect } from '@wordpress/data';\n * import { store as myCustomStore } from 'my-custom-store';\n *\n * resolveSelect( myCustomStore ).getPrice( 'hammer' ).then(console.log)\n * ```\n *\n * @return Object containing the store's promise-wrapped selectors.\n */\nexport function resolveSelect< T extends StoreDescriptor< AnyConfig > >(\n\tstoreNameOrDescriptor: T | string\n): CurriedSelectorsResolveOf< T > {\n\treturn defaultRegistry.resolveSelect(\n\t\tstoreNameOrDescriptor\n\t) as CurriedSelectorsResolveOf< T >;\n}\n\n/**\n * Given a store descriptor, returns an object containing the store's selectors pre-bound to state\n * so that you only need to supply additional arguments, and modified so that they throw promises\n * in case the selector is not resolved yet.\n *\n * @param {StoreDescriptor|string} storeNameOrDescriptor The store descriptor. The legacy calling\n * convention of passing the store name is\n * also supported.\n *\n * @return {Object} Object containing the store's suspense-wrapped selectors.\n */\nexport const suspendSelect = (\n\tstoreNameOrDescriptor:\n\t\t| string\n\t\t| StoreDescriptor< ReduxStoreConfig< any, any, any > >\n): any => defaultRegistry.suspendSelect( storeNameOrDescriptor );\n\n/**\n * Given a listener function, the function will be called any time the state value\n * of one of the registered stores has changed. If you specify the optional\n * `storeNameOrDescriptor` parameter, the listener function will be called only\n * on updates on that one specific registered store.\n *\n * This function returns an `unsubscribe` function used to stop the subscription.\n *\n * @param {Function} listener Callback function.\n * @param {string|StoreDescriptor?} storeNameOrDescriptor Optional store name.\n *\n * @example\n * ```js\n * import { subscribe } from '@wordpress/data';\n *\n * const unsubscribe = subscribe( () => {\n * \t// You could use this opportunity to test whether the derived result of a\n * \t// selector has subsequently changed as the result of a state update.\n * } );\n *\n * // Later, if necessary...\n * unsubscribe();\n * ```\n */\nexport const subscribe = (\n\tlistener: () => void,\n\tstoreNameOrDescriptor?:\n\t\t| string\n\t\t| StoreDescriptor< ReduxStoreConfig< any, any, any > >\n): ( () => void ) =>\n\tdefaultRegistry.subscribe( listener, storeNameOrDescriptor );\n\n/**\n * Registers a generic store instance.\n *\n * @deprecated Use `register( storeDescriptor )` instead.\n *\n * @param {string} name Store registry name.\n * @param {Object} store Store instance (`{ getSelectors, getActions, subscribe }`).\n */\nexport const registerGenericStore: Function =\n\tdefaultRegistry.registerGenericStore;\n\n/**\n * Registers a standard `@wordpress/data` store.\n *\n * @deprecated Use `register` instead.\n *\n * @param {string} storeName Unique namespace identifier for the store.\n * @param {Object} options Store description (reducer, actions, selectors, resolvers).\n *\n * @return {Object} Registered store object.\n */\nexport const registerStore: Function = defaultRegistry.registerStore;\n\n/**\n * Extends a registry to inherit functionality provided by a given plugin. A\n * plugin is an object with properties aligning to that of a registry, merged\n * to extend the default registry behavior.\n *\n * @param {Object} plugin Plugin object.\n */\nexport const use: any = defaultRegistry.use;\n\n/**\n * Registers a standard `@wordpress/data` store descriptor.\n *\n * @example\n * ```js\n * import { createReduxStore, register } from '@wordpress/data';\n *\n * const store = createReduxStore( 'demo', {\n * reducer: ( state = 'OK' ) => state,\n * selectors: {\n * getValue: ( state ) => state,\n * },\n * } );\n * register( store );\n * ```\n *\n * @param {StoreDescriptor} store Store descriptor.\n */\nexport const register = (\n\tstore: StoreDescriptor< ReduxStoreConfig< any, any, any > >\n): void => defaultRegistry.register( store );\n"],
|
|
5
|
-
"mappings": ";AAGA,OAAO,qBAAqB;AAC5B,YAAY,aAAa;AACzB,SAAS,mBAAmB,6BAA6B;AAUzD,SAAoB,WAAXA,gBAA6B;AACtC,SAAoB,WAAXA,gBAA+B;AACxC,SAAoB,WAAXA,gBAA+B;AACxC;AAAA,EACC;AAAA,EACA;AAAA,EACA;AAAA,OACM;AACP;AAAA,EACY,WAAXA;AAAA,EACA;AAAA,OACM;AACP,SAAS,mBAAmB;AAC5B,SAAS,yBAAyB;AAClC,SAAS,sBAAsB;AAC/B,SAAS,wBAAwB,6BAA6B;AAC9D,SAAS,sBAAsB;AAC/B,SAAS,gBAAgB;AACzB,SAAoB,WAAXA,gBAAmC;AAC5C,SAAS,gBAAgB;AACzB,SAAS,cAAc;AAkDhB,IAAM,kBACZ;AAsBM,SAAS,cACf,uBACiC;AACjC,SAAO,gBAAgB;AAAA,IACtB;AAAA,EACD;AACD;AAaO,IAAM,gBAAgB,CAC5B,0BAGS,gBAAgB,cAAe,qBAAsB;AA0BxD,IAAM,YAAY,CACxB,UACA,0BAIA,gBAAgB,UAAW,UAAU,qBAAsB;AAUrD,IAAM,uBACZ,gBAAgB;AAYV,IAAM,gBAA0B,gBAAgB;AAShD,IAAM,MAAW,gBAAgB;AAoBjC,IAAM,WAAW,CACvB,UACU,gBAAgB,SAAU,KAAM;",
|
|
4
|
+
"sourcesContent": ["/**\n * Internal dependencies\n */\nimport defaultRegistry from './default-registry';\nimport * as plugins from './plugins';\nimport { combineReducers as combineReducersModule } from './redux-store';\n\nimport type {\n\tStoreDescriptor,\n\tReduxStoreConfig,\n\tcombineReducers as CombineReducers,\n\tAnyConfig,\n\tCurriedSelectorsResolveOf,\n} from './types';\n\nexport { default as withSelect } from './components/with-select';\nexport { default as withDispatch } from './components/with-dispatch';\nexport { default as withRegistry } from './components/with-registry';\nexport {\n\tRegistryProvider,\n\tRegistryConsumer,\n\tuseRegistry,\n} from './components/registry-provider';\nexport {\n\tdefault as useSelect,\n\tuseSuspenseSelect,\n} from './components/use-select';\nexport { useDispatch } from './components/use-dispatch';\nexport { AsyncModeProvider } from './components/async-mode-provider';\nexport { createRegistry } from './registry';\nexport { createRegistrySelector, createRegistryControl } from './factory';\nexport { createSelector } from './create-selector';\nexport { controls } from './controls';\nexport { default as createReduxStore } from './redux-store';\nexport { keyedReducer } from './redux-store/keyed-reducer';\nexport { dispatch } from './dispatch';\nexport { select } from './select';\n\nexport type * from './types';\n\n/**\n * Object of available plugins to use with a registry.\n *\n * @see [use](#use)\n */\nexport { plugins };\n\n/**\n * The combineReducers helper function turns an object whose values are different\n * reducing functions into a single reducing function you can pass to registerReducer.\n *\n * @type {import('./types').combineReducers}\n * @param {Object} reducers An object whose values correspond to different reducing\n * functions that need to be combined into one.\n *\n * @example\n * ```js\n * import { combineReducers, createReduxStore, register } from '@wordpress/data';\n *\n * const prices = ( state = {}, action ) => {\n * \treturn action.type === 'SET_PRICE' ?\n * \t\t{\n * \t\t\t...state,\n * \t\t\t[ action.item ]: action.price,\n * \t\t} :\n * \t\tstate;\n * };\n *\n * const discountPercent = ( state = 0, action ) => {\n * \treturn action.type === 'START_SALE' ?\n * \t\taction.discountPercent :\n * \t\tstate;\n * };\n *\n * const store = createReduxStore( 'my-shop', {\n * \treducer: combineReducers( {\n * \t\tprices,\n * \t\tdiscountPercent,\n * \t} ),\n * } );\n * register( store );\n * ```\n *\n * @return {Function} A reducer that invokes every reducer inside the reducers\n * object, and constructs a state object with the same shape.\n */\nexport const combineReducers =\n\tcombineReducersModule as unknown as CombineReducers;\n\n/**\n * Given a store descriptor, returns an object containing the store's selectors\n * pre-bound to state so that you only need to supply additional arguments, and\n * modified so that they return promises that resolve to their eventual values,\n * after any resolvers have ran.\n *\n * @param {StoreDescriptor|string} storeNameOrDescriptor The store descriptor. The legacy calling\n * convention of passing the store name is\n * also supported.\n *\n * @example\n * ```js\n * import { resolveSelect } from '@wordpress/data';\n * import { store as myCustomStore } from 'my-custom-store';\n *\n * resolveSelect( myCustomStore ).getPrice( 'hammer' ).then(console.log)\n * ```\n *\n * @return Object containing the store's promise-wrapped selectors.\n */\nexport function resolveSelect< T extends StoreDescriptor< AnyConfig > >(\n\tstoreNameOrDescriptor: T | string\n): CurriedSelectorsResolveOf< T > {\n\treturn defaultRegistry.resolveSelect(\n\t\tstoreNameOrDescriptor\n\t) as CurriedSelectorsResolveOf< T >;\n}\n\n/**\n * Given a store descriptor, returns an object containing the store's selectors pre-bound to state\n * so that you only need to supply additional arguments, and modified so that they throw promises\n * in case the selector is not resolved yet.\n *\n * @param {StoreDescriptor|string} storeNameOrDescriptor The store descriptor. The legacy calling\n * convention of passing the store name is\n * also supported.\n *\n * @return {Object} Object containing the store's suspense-wrapped selectors.\n */\nexport const suspendSelect = (\n\tstoreNameOrDescriptor:\n\t\t| string\n\t\t| StoreDescriptor< ReduxStoreConfig< any, any, any > >\n): any => defaultRegistry.suspendSelect( storeNameOrDescriptor );\n\n/**\n * Given a listener function, the function will be called any time the state value\n * of one of the registered stores has changed. If you specify the optional\n * `storeNameOrDescriptor` parameter, the listener function will be called only\n * on updates on that one specific registered store.\n *\n * This function returns an `unsubscribe` function used to stop the subscription.\n *\n * @param {Function} listener Callback function.\n * @param {string|StoreDescriptor?} storeNameOrDescriptor Optional store name.\n *\n * @example\n * ```js\n * import { subscribe } from '@wordpress/data';\n *\n * const unsubscribe = subscribe( () => {\n * \t// You could use this opportunity to test whether the derived result of a\n * \t// selector has subsequently changed as the result of a state update.\n * } );\n *\n * // Later, if necessary...\n * unsubscribe();\n * ```\n */\nexport const subscribe = (\n\tlistener: () => void,\n\tstoreNameOrDescriptor?:\n\t\t| string\n\t\t| StoreDescriptor< ReduxStoreConfig< any, any, any > >\n): ( () => void ) =>\n\tdefaultRegistry.subscribe( listener, storeNameOrDescriptor );\n\n/**\n * Registers a generic store instance.\n *\n * @deprecated Use `register( storeDescriptor )` instead.\n *\n * @param {string} name Store registry name.\n * @param {Object} store Store instance (`{ getSelectors, getActions, subscribe }`).\n */\nexport const registerGenericStore: Function =\n\tdefaultRegistry.registerGenericStore;\n\n/**\n * Registers a standard `@wordpress/data` store.\n *\n * @deprecated Use `register` instead.\n *\n * @param {string} storeName Unique namespace identifier for the store.\n * @param {Object} options Store description (reducer, actions, selectors, resolvers).\n *\n * @return {Object} Registered store object.\n */\nexport const registerStore: Function = defaultRegistry.registerStore;\n\n/**\n * Extends a registry to inherit functionality provided by a given plugin. A\n * plugin is an object with properties aligning to that of a registry, merged\n * to extend the default registry behavior.\n *\n * @param {Object} plugin Plugin object.\n */\nexport const use: any = defaultRegistry.use;\n\n/**\n * Registers a standard `@wordpress/data` store descriptor.\n *\n * @example\n * ```js\n * import { createReduxStore, register } from '@wordpress/data';\n *\n * const store = createReduxStore( 'demo', {\n * reducer: ( state = 'OK' ) => state,\n * selectors: {\n * getValue: ( state ) => state,\n * },\n * } );\n * register( store );\n * ```\n *\n * @param {StoreDescriptor} store Store descriptor.\n */\nexport const register = (\n\tstore: StoreDescriptor< ReduxStoreConfig< any, any, any > >\n): void => defaultRegistry.register( store );\n"],
|
|
5
|
+
"mappings": ";AAGA,OAAO,qBAAqB;AAC5B,YAAY,aAAa;AACzB,SAAS,mBAAmB,6BAA6B;AAUzD,SAAoB,WAAXA,gBAA6B;AACtC,SAAoB,WAAXA,gBAA+B;AACxC,SAAoB,WAAXA,gBAA+B;AACxC;AAAA,EACC;AAAA,EACA;AAAA,EACA;AAAA,OACM;AACP;AAAA,EACY,WAAXA;AAAA,EACA;AAAA,OACM;AACP,SAAS,mBAAmB;AAC5B,SAAS,yBAAyB;AAClC,SAAS,sBAAsB;AAC/B,SAAS,wBAAwB,6BAA6B;AAC9D,SAAS,sBAAsB;AAC/B,SAAS,gBAAgB;AACzB,SAAoB,WAAXA,gBAAmC;AAC5C,SAAS,oBAAoB;AAC7B,SAAS,gBAAgB;AACzB,SAAS,cAAc;AAkDhB,IAAM,kBACZ;AAsBM,SAAS,cACf,uBACiC;AACjC,SAAO,gBAAgB;AAAA,IACtB;AAAA,EACD;AACD;AAaO,IAAM,gBAAgB,CAC5B,0BAGS,gBAAgB,cAAe,qBAAsB;AA0BxD,IAAM,YAAY,CACxB,UACA,0BAIA,gBAAgB,UAAW,UAAU,qBAAsB;AAUrD,IAAM,uBACZ,gBAAgB;AAYV,IAAM,gBAA0B,gBAAgB;AAShD,IAAM,MAAW,gBAAgB;AAoBjC,IAAM,WAAW,CACvB,UACU,gBAAgB,SAAU,KAAM;",
|
|
6
6
|
"names": ["default"]
|
|
7
7
|
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// packages/data/src/redux-store/keyed-reducer.ts
|
|
2
|
+
var keyedReducer = (actionProperty) => (reducer) => (state = {}, action) => {
|
|
3
|
+
const key = action[actionProperty];
|
|
4
|
+
if (key === void 0) {
|
|
5
|
+
return state;
|
|
6
|
+
}
|
|
7
|
+
const nextKeyState = reducer(state[key], action);
|
|
8
|
+
if (nextKeyState === state[key]) {
|
|
9
|
+
return state;
|
|
10
|
+
}
|
|
11
|
+
return {
|
|
12
|
+
...state,
|
|
13
|
+
[key]: nextKeyState
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
export {
|
|
17
|
+
keyedReducer
|
|
18
|
+
};
|
|
19
|
+
//# sourceMappingURL=keyed-reducer.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/redux-store/keyed-reducer.ts"],
|
|
4
|
+
"sourcesContent": ["/**\n * External dependencies\n */\nimport type { AnyAction, Reducer } from 'redux';\n\n/**\n * Higher-order reducer creator which creates a combined reducer object, keyed\n * by a property on the action object.\n *\n * @param actionProperty Action property by which to key object.\n * @return Higher-order reducer.\n *\n * @example\n * ```js\n * import { keyedReducer } from '@wordpress/data';\n *\n * const itemsByContext = keyedReducer( 'context' )( ( state = [], action ) => {\n * switch ( action.type ) {\n * case 'ADD_ITEM':\n * return [ ...state, action.item ];\n * }\n * return state;\n * } );\n * ```\n */\nexport const keyedReducer =\n\t< TState extends unknown, TAction extends AnyAction >(\n\t\tactionProperty: string\n\t) =>\n\t(\n\t\treducer: Reducer< TState, TAction >\n\t): Reducer< Record< string, TState >, TAction > =>\n\t( state: Record< string, TState > = {}, action ) => {\n\t\t// Retrieve subkey from action. Do not track if undefined; useful for cases\n\t\t// where reducer is scoped by action shape.\n\t\tconst key = action[ actionProperty ];\n\t\tif ( key === undefined ) {\n\t\t\treturn state;\n\t\t}\n\n\t\t// Avoid updating state if unchanged. Note that this also accounts for a\n\t\t// reducer which returns undefined on a key which is not yet tracked.\n\t\tconst nextKeyState = reducer( state[ key ], action );\n\t\tif ( nextKeyState === state[ key ] ) {\n\t\t\treturn state;\n\t\t}\n\n\t\treturn {\n\t\t\t...state,\n\t\t\t[ key ]: nextKeyState,\n\t\t};\n\t};\n"],
|
|
5
|
+
"mappings": ";AAyBO,IAAM,eACZ,CACC,mBAED,CACC,YAED,CAAE,QAAkC,CAAC,GAAG,WAAY;AAGnD,QAAM,MAAM,OAAQ,cAAe;AACnC,MAAK,QAAQ,QAAY;AACxB,WAAO;AAAA,EACR;AAIA,QAAM,eAAe,QAAS,MAAO,GAAI,GAAG,MAAO;AACnD,MAAK,iBAAiB,MAAO,GAAI,GAAI;AACpC,WAAO;AAAA,EACR;AAEA,SAAO;AAAA,IACN,GAAG;AAAA,IACH,CAAE,GAAI,GAAG;AAAA,EACV;AACD;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
// packages/data/src/redux-store/metadata/reducer.ts
|
|
2
2
|
import EquivalentKeyMap from "equivalent-key-map";
|
|
3
|
-
import {
|
|
4
|
-
|
|
3
|
+
import { keyedReducer } from "../keyed-reducer.mjs";
|
|
4
|
+
import { selectorArgsToStateKey } from "./utils.mjs";
|
|
5
|
+
var subKeysIsResolved = keyedReducer("selectorName")((state = new EquivalentKeyMap(), action) => {
|
|
5
6
|
switch (action.type) {
|
|
6
7
|
case "START_RESOLUTION": {
|
|
7
8
|
const nextState = new EquivalentKeyMap(state);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/redux-store/metadata/reducer.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * External dependencies\n */\nimport EquivalentKeyMap from 'equivalent-key-map';\nimport type { Reducer } from 'redux';\n\nimport type {\n\tstartResolution,\n\tfinishResolution,\n\tfailResolution,\n\tstartResolutions,\n\tfinishResolutions,\n\tfailResolutions,\n\tinvalidateResolution,\n\tinvalidateResolutionForStore,\n\tinvalidateResolutionForStoreSelector,\n} from './actions';\n\n/**\n * Internal dependencies\n */\nimport { selectorArgsToStateKey
|
|
5
|
-
"mappings": ";AAGA,OAAO,sBAAsB;AAkB7B,SAAS,
|
|
4
|
+
"sourcesContent": ["/**\n * External dependencies\n */\nimport EquivalentKeyMap from 'equivalent-key-map';\nimport type { Reducer } from 'redux';\n\nimport type {\n\tstartResolution,\n\tfinishResolution,\n\tfailResolution,\n\tstartResolutions,\n\tfinishResolutions,\n\tfailResolutions,\n\tinvalidateResolution,\n\tinvalidateResolutionForStore,\n\tinvalidateResolutionForStoreSelector,\n} from './actions';\n\n/**\n * Internal dependencies\n */\nimport { keyedReducer } from '../keyed-reducer';\nimport { selectorArgsToStateKey } from './utils';\n\ntype Action =\n\t| ReturnType< typeof startResolution >\n\t| ReturnType< typeof finishResolution >\n\t| ReturnType< typeof failResolution >\n\t| ReturnType< typeof startResolutions >\n\t| ReturnType< typeof finishResolutions >\n\t| ReturnType< typeof failResolutions >\n\t| ReturnType< typeof invalidateResolution >\n\t| ReturnType< typeof invalidateResolutionForStore >\n\t| ReturnType< typeof invalidateResolutionForStoreSelector >;\n\ntype StateKey = unknown[] | unknown;\nexport type StateValue =\n\t| { status: 'resolving' | 'finished' }\n\t| { status: 'error'; error: Error | unknown };\n\nexport type Status = StateValue[ 'status' ];\nexport type State = EquivalentKeyMap< StateKey, StateValue >;\n\n/**\n * Reducer function returning next state for selector resolution of\n * subkeys, object form:\n *\n * selectorName -> EquivalentKeyMap<Array,boolean>\n */\nconst subKeysIsResolved: Reducer<\n\tRecord< string, State >,\n\tAction\n> = keyedReducer< State, Action >( 'selectorName' )( (\n\tstate = new EquivalentKeyMap(),\n\taction: Action\n) => {\n\tswitch ( action.type ) {\n\t\tcase 'START_RESOLUTION': {\n\t\t\tconst nextState = new EquivalentKeyMap( state );\n\t\t\tnextState.set( selectorArgsToStateKey( action.args ), {\n\t\t\t\tstatus: 'resolving',\n\t\t\t} );\n\t\t\treturn nextState;\n\t\t}\n\t\tcase 'FINISH_RESOLUTION': {\n\t\t\tconst nextState = new EquivalentKeyMap( state );\n\t\t\tnextState.set( selectorArgsToStateKey( action.args ), {\n\t\t\t\tstatus: 'finished',\n\t\t\t} );\n\t\t\treturn nextState;\n\t\t}\n\t\tcase 'FAIL_RESOLUTION': {\n\t\t\tconst nextState = new EquivalentKeyMap( state );\n\t\t\tnextState.set( selectorArgsToStateKey( action.args ), {\n\t\t\t\tstatus: 'error',\n\t\t\t\terror: action.error,\n\t\t\t} );\n\t\t\treturn nextState;\n\t\t}\n\t\tcase 'START_RESOLUTIONS': {\n\t\t\tconst nextState = new EquivalentKeyMap( state );\n\t\t\tfor ( const resolutionArgs of action.args ) {\n\t\t\t\tnextState.set( selectorArgsToStateKey( resolutionArgs ), {\n\t\t\t\t\tstatus: 'resolving',\n\t\t\t\t} );\n\t\t\t}\n\t\t\treturn nextState;\n\t\t}\n\t\tcase 'FINISH_RESOLUTIONS': {\n\t\t\tconst nextState = new EquivalentKeyMap( state );\n\t\t\tfor ( const resolutionArgs of action.args ) {\n\t\t\t\tnextState.set( selectorArgsToStateKey( resolutionArgs ), {\n\t\t\t\t\tstatus: 'finished',\n\t\t\t\t} );\n\t\t\t}\n\t\t\treturn nextState;\n\t\t}\n\t\tcase 'FAIL_RESOLUTIONS': {\n\t\t\tconst nextState = new EquivalentKeyMap( state );\n\t\t\taction.args.forEach( ( resolutionArgs, idx ) => {\n\t\t\t\tconst resolutionState: StateValue = {\n\t\t\t\t\tstatus: 'error',\n\t\t\t\t\terror: undefined,\n\t\t\t\t};\n\n\t\t\t\tconst error = action.errors[ idx ];\n\t\t\t\tif ( error ) {\n\t\t\t\t\tresolutionState.error = error;\n\t\t\t\t}\n\n\t\t\t\tnextState.set(\n\t\t\t\t\tselectorArgsToStateKey( resolutionArgs as unknown[] ),\n\t\t\t\t\tresolutionState\n\t\t\t\t);\n\t\t\t} );\n\t\t\treturn nextState;\n\t\t}\n\t\tcase 'INVALIDATE_RESOLUTION': {\n\t\t\tconst nextState = new EquivalentKeyMap( state );\n\t\t\tnextState.delete( selectorArgsToStateKey( action.args ) );\n\t\t\treturn nextState;\n\t\t}\n\t}\n\treturn state;\n} );\n\n/**\n * Reducer function returning next state for selector resolution, object form:\n *\n * selectorName -> EquivalentKeyMap<Array, boolean>\n *\n * @param state Current state.\n * @param action Dispatched action.\n *\n * @return Next state.\n */\nconst isResolved = ( state: Record< string, State > = {}, action: Action ) => {\n\tswitch ( action.type ) {\n\t\tcase 'INVALIDATE_RESOLUTION_FOR_STORE':\n\t\t\treturn {};\n\t\tcase 'INVALIDATE_RESOLUTION_FOR_STORE_SELECTOR': {\n\t\t\tif ( action.selectorName in state ) {\n\t\t\t\tconst {\n\t\t\t\t\t[ action.selectorName ]: removedSelector,\n\t\t\t\t\t...restState\n\t\t\t\t} = state;\n\t\t\t\treturn restState;\n\t\t\t}\n\t\t\treturn state;\n\t\t}\n\t\tcase 'START_RESOLUTION':\n\t\tcase 'FINISH_RESOLUTION':\n\t\tcase 'FAIL_RESOLUTION':\n\t\tcase 'START_RESOLUTIONS':\n\t\tcase 'FINISH_RESOLUTIONS':\n\t\tcase 'FAIL_RESOLUTIONS':\n\t\tcase 'INVALIDATE_RESOLUTION':\n\t\t\treturn subKeysIsResolved( state, action );\n\t\tdefault:\n\t\t\treturn state;\n\t}\n};\n\nexport default isResolved;\n"],
|
|
5
|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QAAS;AAC/C,cAAM,kBAA8B;AAAA,UACnC,QAAQ;AAAA,UACR,OAAO;AAAA,QACR;AAEA,cAAM,QAAQ,OAAO,OAAQ,GAAI;AACjC,YAAK,OAAQ;AACZ,0BAAgB,QAAQ;AAAA,QACzB;AAEA,kBAAU;AAAA,UACT,uBAAwB,cAA4B;AAAA,UACpD;AAAA,QACD;AAAA,MACD,CAAE;AACF,aAAO;AAAA,IACR;AAAA,IACA,KAAK,yBAAyB;AAC7B,YAAM,YAAY,IAAI,iBAAkB,KAAM;AAC9C,gBAAU,OAAQ,uBAAwB,OAAO,IAAK,CAAE;AACxD,aAAO;AAAA,IACR;AAAA,EACD;AACA,SAAO;AACR,CAAE;AAYF,IAAM,aAAa,CAAE,QAAiC,CAAC,GAAG,WAAoB;AAC7E,UAAS,OAAO,MAAO;AAAA,IACtB,KAAK;AACJ,aAAO,CAAC;AAAA,IACT,KAAK,4CAA4C;AAChD,UAAK,OAAO,gBAAgB,OAAQ;AACnC,cAAM;AAAA,UACL,CAAE,OAAO,YAAa,GAAG;AAAA,UACzB,GAAG;AAAA,QACJ,IAAI;AACJ,eAAO;AAAA,MACR;AACA,aAAO;AAAA,IACR;AAAA,IACA,KAAK;AAAA,IACL,KAAK;AAAA,IACL,KAAK;AAAA,IACL,KAAK;AAAA,IACL,KAAK;AAAA,IACL,KAAK;AAAA,IACL,KAAK;AACJ,aAAO,kBAAmB,OAAO,MAAO;AAAA,IACzC;AACC,aAAO;AAAA,EACT;AACD;AAEA,IAAO,kBAAQ;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,18 +1,4 @@
|
|
|
1
1
|
// packages/data/src/redux-store/metadata/utils.ts
|
|
2
|
-
var onSubKey = (actionProperty) => (reducer) => (state = {}, action) => {
|
|
3
|
-
const key = action[actionProperty];
|
|
4
|
-
if (key === void 0) {
|
|
5
|
-
return state;
|
|
6
|
-
}
|
|
7
|
-
const nextKeyState = reducer(state[key], action);
|
|
8
|
-
if (nextKeyState === state[key]) {
|
|
9
|
-
return state;
|
|
10
|
-
}
|
|
11
|
-
return {
|
|
12
|
-
...state,
|
|
13
|
-
[key]: nextKeyState
|
|
14
|
-
};
|
|
15
|
-
};
|
|
16
2
|
function selectorArgsToStateKey(args) {
|
|
17
3
|
if (args === void 0 || args === null) {
|
|
18
4
|
return [];
|
|
@@ -25,7 +11,6 @@ function selectorArgsToStateKey(args) {
|
|
|
25
11
|
return idx === len ? args : args.slice(0, idx);
|
|
26
12
|
}
|
|
27
13
|
export {
|
|
28
|
-
onSubKey,
|
|
29
14
|
selectorArgsToStateKey
|
|
30
15
|
};
|
|
31
16
|
//# sourceMappingURL=utils.mjs.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/redux-store/metadata/utils.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n *
|
|
5
|
-
"mappings": ";
|
|
4
|
+
"sourcesContent": ["/**\n * Normalize selector argument array by defaulting `undefined` value to an empty array\n * and removing trailing `undefined` values.\n *\n * @param args Selector argument array\n * @return Normalized state key array\n */\nexport function selectorArgsToStateKey( args: unknown[] | null | undefined ) {\n\tif ( args === undefined || args === null ) {\n\t\treturn [];\n\t}\n\n\tconst len = args.length;\n\tlet idx = len;\n\twhile ( idx > 0 && args[ idx - 1 ] === undefined ) {\n\t\tidx--;\n\t}\n\treturn idx === len ? args : args.slice( 0, idx );\n}\n"],
|
|
5
|
+
"mappings": ";AAOO,SAAS,uBAAwB,MAAqC;AAC5E,MAAK,SAAS,UAAa,SAAS,MAAO;AAC1C,WAAO,CAAC;AAAA,EACT;AAEA,QAAM,MAAM,KAAK;AACjB,MAAI,MAAM;AACV,SAAQ,MAAM,KAAK,KAAM,MAAM,CAAE,MAAM,QAAY;AAClD;AAAA,EACD;AACA,SAAO,QAAQ,MAAM,OAAO,KAAK,MAAO,GAAG,GAAI;AAChD;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/build-types/index.d.ts
CHANGED
|
@@ -12,6 +12,7 @@ export { createRegistrySelector, createRegistryControl } from './factory';
|
|
|
12
12
|
export { createSelector } from './create-selector';
|
|
13
13
|
export { controls } from './controls';
|
|
14
14
|
export { default as createReduxStore } from './redux-store';
|
|
15
|
+
export { keyedReducer } from './redux-store/keyed-reducer';
|
|
15
16
|
export { dispatch } from './dispatch';
|
|
16
17
|
export { select } from './select';
|
|
17
18
|
export type * from './types';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,OAAO,MAAM,WAAW,CAAC;AAGrC,OAAO,KAAK,EACX,eAAe,EACf,gBAAgB,EAChB,eAAe,IAAI,eAAe,EAClC,SAAS,EACT,yBAAyB,EACzB,MAAM,SAAS,CAAC;AAEjB,OAAO,EAAE,OAAO,IAAI,UAAU,EAAE,MAAM,0BAA0B,CAAC;AACjE,OAAO,EAAE,OAAO,IAAI,YAAY,EAAE,MAAM,4BAA4B,CAAC;AACrE,OAAO,EAAE,OAAO,IAAI,YAAY,EAAE,MAAM,4BAA4B,CAAC;AACrE,OAAO,EACN,gBAAgB,EAChB,gBAAgB,EAChB,WAAW,GACX,MAAM,gCAAgC,CAAC;AACxC,OAAO,EACN,OAAO,IAAI,SAAS,EACpB,iBAAiB,GACjB,MAAM,yBAAyB,CAAC;AACjC,OAAO,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAC;AACxD,OAAO,EAAE,iBAAiB,EAAE,MAAM,kCAAkC,CAAC;AACrE,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAC5C,OAAO,EAAE,sBAAsB,EAAE,qBAAqB,EAAE,MAAM,WAAW,CAAC;AAC1E,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AACnD,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AACtC,OAAO,EAAE,OAAO,IAAI,gBAAgB,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AAe,CAAC;AAErD;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,aAAa,CAAE,CAAC,SAAS,eAAe,CAAE,SAAS,CAAE,EACpE,qBAAqB,EAAE,CAAC,GAAG,MAAM,GAC/B,yBAAyB,CAAE,CAAC,CAAE,CAIhC;AAED;;;;;;;;;;GAUG;AACH,eAAO,MAAM,aAAa,GACzB,uBACG,MAAM,GACN,eAAe,CAAE,gBAAgB,CAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAE,CAAE,KACrD,GAA6D,CAAC;AAEjE;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,eAAO,MAAM,SAAS,GACrB,UAAU,MAAM,IAAI,EACpB,wBACG,MAAM,GACN,eAAe,CAAE,gBAAgB,CAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAE,CAAE,KACrD,CAAE,MAAM,IAAI,CAC8C,CAAC;AAE9D;;;;;;;GAOG;AACH,eAAO,MAAM,oBAAoB,EAAE,QACE,CAAC;AAEtC;;;;;;;;;GASG;AACH,eAAO,MAAM,aAAa,EAAE,QAAwC,CAAC;AAErE;;;;;;GAMG;AACH,eAAO,MAAM,GAAG,EAAE,GAAyB,CAAC;AAE5C;;;;;;;;;;;;;;;;;GAiBG;AACH,eAAO,MAAM,QAAQ,GACpB,OAAO,eAAe,CAAE,gBAAgB,CAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAE,CAAE,KACzD,IAAyC,CAAC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* External dependencies
|
|
3
|
+
*/
|
|
4
|
+
import type { AnyAction, Reducer } from 'redux';
|
|
5
|
+
/**
|
|
6
|
+
* Higher-order reducer creator which creates a combined reducer object, keyed
|
|
7
|
+
* by a property on the action object.
|
|
8
|
+
*
|
|
9
|
+
* @param actionProperty Action property by which to key object.
|
|
10
|
+
* @return Higher-order reducer.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```js
|
|
14
|
+
* import { keyedReducer } from '@wordpress/data';
|
|
15
|
+
*
|
|
16
|
+
* const itemsByContext = keyedReducer( 'context' )( ( state = [], action ) => {
|
|
17
|
+
* switch ( action.type ) {
|
|
18
|
+
* case 'ADD_ITEM':
|
|
19
|
+
* return [ ...state, action.item ];
|
|
20
|
+
* }
|
|
21
|
+
* return state;
|
|
22
|
+
* } );
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export declare const keyedReducer: <TState extends unknown, TAction extends AnyAction>(actionProperty: string) => (reducer: Reducer<TState, TAction>) => Reducer<Record<string, TState>, TAction>;
|
|
26
|
+
//# sourceMappingURL=keyed-reducer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"keyed-reducer.d.ts","sourceRoot":"","sources":["../../src/redux-store/keyed-reducer.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC;AAEhD;;;;;;;;;;;;;;;;;;;GAmBG;AACH,eAAO,MAAM,YAAY,GACtB,MAAM,SAAS,OAAO,EAAE,OAAO,SAAS,SAAS,EAClD,gBAAgB,MAAM,MAGtB,SAAS,OAAO,CAAE,MAAM,EAAE,OAAO,CAAE,KACjC,OAAO,CAAE,MAAM,CAAE,MAAM,EAAE,MAAM,CAAE,EAAE,OAAO,CAoB5C,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"reducer.d.ts","sourceRoot":"","sources":["../../../src/redux-store/metadata/reducer.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,gBAAgB,MAAM,oBAAoB,CAAC;AAGlD,OAAO,KAAK,EACX,eAAe,EACf,gBAAgB,EAChB,cAAc,EACd,gBAAgB,EAChB,iBAAiB,EACjB,eAAe,EACf,oBAAoB,EACpB,4BAA4B,EAC5B,oCAAoC,EACpC,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"reducer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|
3
|
-
*/
|
|
4
|
-
import type { AnyAction, Reducer } from 'redux';
|
|
5
|
-
/**
|
|
6
|
-
* Higher-order reducer creator which creates a combined reducer object, keyed
|
|
7
|
-
* by a property on the action object.
|
|
8
|
-
*
|
|
9
|
-
* @param actionProperty Action property by which to key object.
|
|
10
|
-
* @return Higher-order reducer.
|
|
11
|
-
*/
|
|
12
|
-
export declare const onSubKey: <TState extends unknown, TAction extends AnyAction>(actionProperty: string) => (reducer: Reducer<TState, TAction>) => Reducer<Record<string, TState>, TAction>;
|
|
13
1
|
/**
|
|
14
2
|
* Normalize selector argument array by defaulting `undefined` value to an empty array
|
|
15
3
|
* and removing trailing `undefined` values.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/redux-store/metadata/utils.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/redux-store/metadata/utils.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,wBAAgB,sBAAsB,CAAE,IAAI,EAAE,OAAO,EAAE,GAAG,IAAI,GAAG,SAAS,aAWzE"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wordpress/data",
|
|
3
|
-
"version": "10.
|
|
3
|
+
"version": "10.45.0",
|
|
4
4
|
"description": "Data module for WordPress.",
|
|
5
5
|
"author": "The WordPress Contributors",
|
|
6
6
|
"license": "GPL-2.0-or-later",
|
|
@@ -45,13 +45,13 @@
|
|
|
45
45
|
"types": "build-types",
|
|
46
46
|
"sideEffects": false,
|
|
47
47
|
"dependencies": {
|
|
48
|
-
"@wordpress/compose": "^7.
|
|
49
|
-
"@wordpress/deprecated": "^4.
|
|
50
|
-
"@wordpress/element": "^6.
|
|
51
|
-
"@wordpress/is-shallow-equal": "^5.
|
|
52
|
-
"@wordpress/priority-queue": "^3.
|
|
53
|
-
"@wordpress/private-apis": "^1.
|
|
54
|
-
"@wordpress/redux-routine": "^5.
|
|
48
|
+
"@wordpress/compose": "^7.45.0",
|
|
49
|
+
"@wordpress/deprecated": "^4.45.0",
|
|
50
|
+
"@wordpress/element": "^6.45.0",
|
|
51
|
+
"@wordpress/is-shallow-equal": "^5.45.0",
|
|
52
|
+
"@wordpress/priority-queue": "^3.45.0",
|
|
53
|
+
"@wordpress/private-apis": "^1.45.0",
|
|
54
|
+
"@wordpress/redux-routine": "^5.45.0",
|
|
55
55
|
"deepmerge": "^4.3.0",
|
|
56
56
|
"equivalent-key-map": "^0.2.2",
|
|
57
57
|
"is-plain-object": "^5.0.0",
|
|
@@ -69,5 +69,5 @@
|
|
|
69
69
|
"publishConfig": {
|
|
70
70
|
"access": "public"
|
|
71
71
|
},
|
|
72
|
-
"gitHead": "
|
|
72
|
+
"gitHead": "8c229eaed0e88c9827e2da3d73a78f9ddd77714b"
|
|
73
73
|
}
|
package/src/index.ts
CHANGED
|
@@ -32,6 +32,7 @@ export { createRegistrySelector, createRegistryControl } from './factory';
|
|
|
32
32
|
export { createSelector } from './create-selector';
|
|
33
33
|
export { controls } from './controls';
|
|
34
34
|
export { default as createReduxStore } from './redux-store';
|
|
35
|
+
export { keyedReducer } from './redux-store/keyed-reducer';
|
|
35
36
|
export { dispatch } from './dispatch';
|
|
36
37
|
export { select } from './select';
|
|
37
38
|
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* External dependencies
|
|
3
|
+
*/
|
|
4
|
+
import type { AnyAction, Reducer } from 'redux';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Higher-order reducer creator which creates a combined reducer object, keyed
|
|
8
|
+
* by a property on the action object.
|
|
9
|
+
*
|
|
10
|
+
* @param actionProperty Action property by which to key object.
|
|
11
|
+
* @return Higher-order reducer.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```js
|
|
15
|
+
* import { keyedReducer } from '@wordpress/data';
|
|
16
|
+
*
|
|
17
|
+
* const itemsByContext = keyedReducer( 'context' )( ( state = [], action ) => {
|
|
18
|
+
* switch ( action.type ) {
|
|
19
|
+
* case 'ADD_ITEM':
|
|
20
|
+
* return [ ...state, action.item ];
|
|
21
|
+
* }
|
|
22
|
+
* return state;
|
|
23
|
+
* } );
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export const keyedReducer =
|
|
27
|
+
< TState extends unknown, TAction extends AnyAction >(
|
|
28
|
+
actionProperty: string
|
|
29
|
+
) =>
|
|
30
|
+
(
|
|
31
|
+
reducer: Reducer< TState, TAction >
|
|
32
|
+
): Reducer< Record< string, TState >, TAction > =>
|
|
33
|
+
( state: Record< string, TState > = {}, action ) => {
|
|
34
|
+
// Retrieve subkey from action. Do not track if undefined; useful for cases
|
|
35
|
+
// where reducer is scoped by action shape.
|
|
36
|
+
const key = action[ actionProperty ];
|
|
37
|
+
if ( key === undefined ) {
|
|
38
|
+
return state;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Avoid updating state if unchanged. Note that this also accounts for a
|
|
42
|
+
// reducer which returns undefined on a key which is not yet tracked.
|
|
43
|
+
const nextKeyState = reducer( state[ key ], action );
|
|
44
|
+
if ( nextKeyState === state[ key ] ) {
|
|
45
|
+
return state;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return {
|
|
49
|
+
...state,
|
|
50
|
+
[ key ]: nextKeyState,
|
|
51
|
+
};
|
|
52
|
+
};
|
|
@@ -19,7 +19,8 @@ import type {
|
|
|
19
19
|
/**
|
|
20
20
|
* Internal dependencies
|
|
21
21
|
*/
|
|
22
|
-
import {
|
|
22
|
+
import { keyedReducer } from '../keyed-reducer';
|
|
23
|
+
import { selectorArgsToStateKey } from './utils';
|
|
23
24
|
|
|
24
25
|
type Action =
|
|
25
26
|
| ReturnType< typeof startResolution >
|
|
@@ -46,10 +47,13 @@ export type State = EquivalentKeyMap< StateKey, StateValue >;
|
|
|
46
47
|
*
|
|
47
48
|
* selectorName -> EquivalentKeyMap<Array,boolean>
|
|
48
49
|
*/
|
|
49
|
-
const subKeysIsResolved: Reducer<
|
|
50
|
-
State
|
|
50
|
+
const subKeysIsResolved: Reducer<
|
|
51
|
+
Record< string, State >,
|
|
51
52
|
Action
|
|
52
|
-
>( 'selectorName' )( (
|
|
53
|
+
> = keyedReducer< State, Action >( 'selectorName' )( (
|
|
54
|
+
state = new EquivalentKeyMap(),
|
|
55
|
+
action: Action
|
|
56
|
+
) => {
|
|
53
57
|
switch ( action.type ) {
|
|
54
58
|
case 'START_RESOLUTION': {
|
|
55
59
|
const nextState = new EquivalentKeyMap( state );
|
|
@@ -1,44 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Internal dependencies
|
|
3
3
|
*/
|
|
4
|
-
import { selectorArgsToStateKey
|
|
5
|
-
|
|
6
|
-
describe( 'onSubKey', () => {
|
|
7
|
-
function createEnhancedReducer( actionProperty ) {
|
|
8
|
-
const enhanceReducer = onSubKey( actionProperty );
|
|
9
|
-
return enhanceReducer(
|
|
10
|
-
( state, action ) => 'Called by ' + action.caller
|
|
11
|
-
);
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
it( 'should default to an empty object', () => {
|
|
15
|
-
const reducer = createEnhancedReducer( 'caller' );
|
|
16
|
-
const nextState = reducer( undefined, { type: '@@INIT' } );
|
|
17
|
-
|
|
18
|
-
expect( nextState ).toEqual( {} );
|
|
19
|
-
} );
|
|
20
|
-
|
|
21
|
-
it( 'should ignore actions where property not present', () => {
|
|
22
|
-
const state = {};
|
|
23
|
-
const reducer = createEnhancedReducer( 'caller' );
|
|
24
|
-
const nextState = reducer( state, { type: 'DO_FOO' } );
|
|
25
|
-
|
|
26
|
-
expect( nextState ).toBe( state );
|
|
27
|
-
} );
|
|
28
|
-
|
|
29
|
-
it( 'should key by action property', () => {
|
|
30
|
-
const reducer = createEnhancedReducer( 'caller' );
|
|
31
|
-
|
|
32
|
-
let state = Object.freeze( {} );
|
|
33
|
-
state = reducer( state, { type: 'DO_FOO', caller: 1 } );
|
|
34
|
-
state = reducer( state, { type: 'DO_FOO', caller: 2 } );
|
|
35
|
-
|
|
36
|
-
expect( state ).toEqual( {
|
|
37
|
-
1: 'Called by 1',
|
|
38
|
-
2: 'Called by 2',
|
|
39
|
-
} );
|
|
40
|
-
} );
|
|
41
|
-
} );
|
|
4
|
+
import { selectorArgsToStateKey } from '../utils';
|
|
42
5
|
|
|
43
6
|
describe( 'selectorArgsToStateKey', () => {
|
|
44
7
|
it( 'should default to an empty array', () => {
|
|
@@ -1,43 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* External dependencies
|
|
3
|
-
*/
|
|
4
|
-
import type { AnyAction, Reducer } from 'redux';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Higher-order reducer creator which creates a combined reducer object, keyed
|
|
8
|
-
* by a property on the action object.
|
|
9
|
-
*
|
|
10
|
-
* @param actionProperty Action property by which to key object.
|
|
11
|
-
* @return Higher-order reducer.
|
|
12
|
-
*/
|
|
13
|
-
export const onSubKey =
|
|
14
|
-
< TState extends unknown, TAction extends AnyAction >(
|
|
15
|
-
actionProperty: string
|
|
16
|
-
) =>
|
|
17
|
-
(
|
|
18
|
-
reducer: Reducer< TState, TAction >
|
|
19
|
-
): Reducer< Record< string, TState >, TAction > =>
|
|
20
|
-
( state: Record< string, TState > = {}, action ) => {
|
|
21
|
-
// Retrieve subkey from action. Do not track if undefined; useful for cases
|
|
22
|
-
// where reducer is scoped by action shape.
|
|
23
|
-
const key = action[ actionProperty ];
|
|
24
|
-
if ( key === undefined ) {
|
|
25
|
-
return state;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
// Avoid updating state if unchanged. Note that this also accounts for a
|
|
29
|
-
// reducer which returns undefined on a key which is not yet tracked.
|
|
30
|
-
const nextKeyState = reducer( state[ key ], action );
|
|
31
|
-
if ( nextKeyState === state[ key ] ) {
|
|
32
|
-
return state;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
return {
|
|
36
|
-
...state,
|
|
37
|
-
[ key ]: nextKeyState,
|
|
38
|
-
};
|
|
39
|
-
};
|
|
40
|
-
|
|
41
1
|
/**
|
|
42
2
|
* Normalize selector argument array by defaulting `undefined` value to an empty array
|
|
43
3
|
* and removing trailing `undefined` values.
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal dependencies
|
|
3
|
+
*/
|
|
4
|
+
import { keyedReducer } from '../keyed-reducer';
|
|
5
|
+
|
|
6
|
+
describe( 'keyedReducer', () => {
|
|
7
|
+
function createEnhancedReducer( actionProperty ) {
|
|
8
|
+
const enhanceReducer = keyedReducer( actionProperty );
|
|
9
|
+
return enhanceReducer(
|
|
10
|
+
( state, action ) => 'Called by ' + action.caller
|
|
11
|
+
);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
it( 'should default to an empty object', () => {
|
|
15
|
+
const reducer = createEnhancedReducer( 'caller' );
|
|
16
|
+
const nextState = reducer( undefined, { type: '@@INIT' } );
|
|
17
|
+
|
|
18
|
+
expect( nextState ).toEqual( {} );
|
|
19
|
+
} );
|
|
20
|
+
|
|
21
|
+
it( 'should ignore actions where property not present', () => {
|
|
22
|
+
const state = {};
|
|
23
|
+
const reducer = createEnhancedReducer( 'caller' );
|
|
24
|
+
const nextState = reducer( state, { type: 'DO_FOO' } );
|
|
25
|
+
|
|
26
|
+
expect( nextState ).toBe( state );
|
|
27
|
+
} );
|
|
28
|
+
|
|
29
|
+
it( 'should key by action property', () => {
|
|
30
|
+
const reducer = createEnhancedReducer( 'caller' );
|
|
31
|
+
|
|
32
|
+
let state = Object.freeze( {} );
|
|
33
|
+
state = reducer( state, { type: 'DO_FOO', caller: 1 } );
|
|
34
|
+
state = reducer( state, { type: 'DO_FOO', caller: 2 } );
|
|
35
|
+
|
|
36
|
+
expect( state ).toEqual( {
|
|
37
|
+
1: 'Called by 1',
|
|
38
|
+
2: 'Called by 2',
|
|
39
|
+
} );
|
|
40
|
+
} );
|
|
41
|
+
} );
|