@wordpress/data 10.41.0 → 10.41.1-next.v.202603102151.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/README.md +20 -28
- package/build/components/async-mode-provider/context.cjs +1 -1
- package/build/components/async-mode-provider/context.cjs.map +3 -3
- package/build/components/async-mode-provider/index.cjs +1 -1
- package/build/components/async-mode-provider/index.cjs.map +1 -1
- package/build/components/async-mode-provider/use-async-mode.cjs +1 -1
- package/build/components/async-mode-provider/use-async-mode.cjs.map +3 -3
- package/build/components/registry-provider/context.cjs +1 -1
- package/build/components/registry-provider/context.cjs.map +1 -1
- package/build/components/registry-provider/index.cjs +1 -1
- package/build/components/registry-provider/index.cjs.map +1 -1
- package/build/components/registry-provider/use-registry.cjs +1 -1
- package/build/components/registry-provider/use-registry.cjs.map +3 -3
- package/build/components/use-dispatch/index.cjs +1 -1
- package/build/components/use-dispatch/index.cjs.map +1 -1
- package/build/components/use-dispatch/use-dispatch-with-map.cjs +1 -1
- package/build/components/use-dispatch/use-dispatch-with-map.cjs.map +3 -3
- package/build/components/use-dispatch/use-dispatch.cjs +1 -1
- package/build/components/use-dispatch/use-dispatch.cjs.map +3 -3
- package/build/components/use-select/index.cjs +2 -2
- package/build/components/use-select/index.cjs.map +3 -3
- package/build/components/with-dispatch/index.cjs +1 -1
- package/build/components/with-dispatch/index.cjs.map +3 -3
- package/build/components/with-registry/index.cjs +1 -1
- package/build/components/with-registry/index.cjs.map +3 -3
- package/build/components/with-select/index.cjs +1 -1
- package/build/components/with-select/index.cjs.map +3 -3
- package/build/controls.cjs +7 -6
- package/build/controls.cjs.map +3 -3
- package/build/default-registry.cjs +3 -2
- package/build/default-registry.cjs.map +3 -3
- package/build/dispatch.cjs +3 -1
- package/build/dispatch.cjs.map +2 -2
- package/build/factory.cjs.map +2 -2
- package/build/index.cjs +9 -8
- package/build/index.cjs.map +3 -3
- package/build/plugins/index.cjs +1 -1
- package/build/plugins/index.cjs.map +1 -1
- package/build/plugins/persistence/index.cjs +13 -8
- package/build/plugins/persistence/index.cjs.map +3 -3
- package/build/plugins/persistence/storage/default.cjs +1 -1
- package/build/plugins/persistence/storage/default.cjs.map +3 -3
- package/build/plugins/persistence/storage/object.cjs +1 -1
- package/build/plugins/persistence/storage/object.cjs.map +3 -3
- package/build/promise-middleware.cjs +2 -1
- package/build/promise-middleware.cjs.map +3 -3
- package/build/redux-store/index.cjs +32 -16
- package/build/redux-store/index.cjs.map +3 -3
- package/build/redux-store/metadata/selectors.cjs +1 -1
- package/build/redux-store/metadata/selectors.cjs.map +3 -3
- package/build/redux-store/metadata/types.cjs +19 -0
- package/build/redux-store/metadata/types.cjs.map +7 -0
- package/build/registry.cjs +11 -7
- package/build/registry.cjs.map +3 -3
- package/build/resolvers-cache-middleware.cjs +6 -3
- package/build/resolvers-cache-middleware.cjs.map +3 -3
- package/build/select.cjs +3 -1
- package/build/select.cjs.map +2 -2
- package/build/store/index.cjs +1 -1
- package/build/store/index.cjs.map +3 -3
- package/build/types.cjs.map +1 -1
- package/build-module/components/async-mode-provider/context.mjs +1 -1
- package/build-module/components/async-mode-provider/context.mjs.map +3 -3
- package/build-module/components/async-mode-provider/index.mjs +1 -1
- package/build-module/components/async-mode-provider/index.mjs.map +1 -1
- package/build-module/components/async-mode-provider/use-async-mode.mjs +1 -1
- package/build-module/components/async-mode-provider/use-async-mode.mjs.map +3 -3
- package/build-module/components/registry-provider/context.mjs +1 -1
- package/build-module/components/registry-provider/context.mjs.map +1 -1
- package/build-module/components/registry-provider/index.mjs +1 -1
- package/build-module/components/registry-provider/index.mjs.map +1 -1
- package/build-module/components/registry-provider/use-registry.mjs +1 -1
- package/build-module/components/registry-provider/use-registry.mjs.map +3 -3
- package/build-module/components/use-dispatch/index.mjs +1 -1
- package/build-module/components/use-dispatch/index.mjs.map +1 -1
- package/build-module/components/use-dispatch/use-dispatch-with-map.mjs +1 -1
- package/build-module/components/use-dispatch/use-dispatch-with-map.mjs.map +3 -3
- package/build-module/components/use-dispatch/use-dispatch.mjs +1 -1
- package/build-module/components/use-dispatch/use-dispatch.mjs.map +3 -3
- package/build-module/components/use-select/index.mjs +2 -2
- package/build-module/components/use-select/index.mjs.map +3 -3
- package/build-module/components/with-dispatch/index.mjs +1 -1
- package/build-module/components/with-dispatch/index.mjs.map +3 -3
- package/build-module/components/with-registry/index.mjs +1 -1
- package/build-module/components/with-registry/index.mjs.map +3 -3
- package/build-module/components/with-select/index.mjs +1 -1
- package/build-module/components/with-select/index.mjs.map +3 -3
- package/build-module/controls.mjs +7 -6
- package/build-module/controls.mjs.map +3 -3
- package/build-module/default-registry.mjs +3 -2
- package/build-module/default-registry.mjs.map +3 -3
- package/build-module/dispatch.mjs +3 -1
- package/build-module/dispatch.mjs.map +2 -2
- package/build-module/factory.mjs.map +2 -2
- package/build-module/index.mjs +4 -3
- package/build-module/index.mjs.map +2 -2
- package/build-module/plugins/index.mjs +1 -1
- package/build-module/plugins/index.mjs.map +1 -1
- package/build-module/plugins/persistence/index.mjs +13 -8
- package/build-module/plugins/persistence/index.mjs.map +3 -3
- package/build-module/plugins/persistence/storage/default.mjs +1 -1
- package/build-module/plugins/persistence/storage/default.mjs.map +3 -3
- package/build-module/plugins/persistence/storage/object.mjs +1 -1
- package/build-module/plugins/persistence/storage/object.mjs.map +3 -3
- package/build-module/promise-middleware.mjs +2 -1
- package/build-module/promise-middleware.mjs.map +3 -3
- package/build-module/redux-store/index.mjs +32 -16
- package/build-module/redux-store/index.mjs.map +3 -3
- package/build-module/redux-store/metadata/selectors.mjs +1 -1
- package/build-module/redux-store/metadata/selectors.mjs.map +3 -3
- package/build-module/redux-store/metadata/types.mjs +1 -0
- package/build-module/redux-store/metadata/types.mjs.map +7 -0
- package/build-module/registry.mjs +11 -7
- package/build-module/registry.mjs.map +3 -3
- package/build-module/resolvers-cache-middleware.mjs +6 -3
- package/build-module/resolvers-cache-middleware.mjs.map +3 -3
- package/build-module/select.mjs +3 -1
- package/build-module/select.mjs.map +2 -2
- package/build-module/store/index.mjs +1 -1
- package/build-module/store/index.mjs.map +3 -3
- package/build-types/components/async-mode-provider/context.d.ts +37 -3
- package/build-types/components/async-mode-provider/context.d.ts.map +1 -1
- package/build-types/components/async-mode-provider/index.d.ts +2 -2
- package/build-types/components/async-mode-provider/index.d.ts.map +1 -1
- package/build-types/components/async-mode-provider/use-async-mode.d.ts.map +1 -1
- package/build-types/components/registry-provider/context.d.ts +10 -3
- package/build-types/components/registry-provider/context.d.ts.map +1 -1
- package/build-types/components/registry-provider/index.d.ts +2 -2
- package/build-types/components/registry-provider/index.d.ts.map +1 -1
- package/build-types/components/registry-provider/use-registry.d.ts +3 -2
- package/build-types/components/registry-provider/use-registry.d.ts.map +1 -1
- package/build-types/components/use-dispatch/index.d.ts +2 -2
- package/build-types/components/use-dispatch/index.d.ts.map +1 -1
- package/build-types/components/use-dispatch/use-dispatch-with-map.d.ts +11 -9
- package/build-types/components/use-dispatch/use-dispatch-with-map.d.ts.map +1 -1
- package/build-types/components/use-dispatch/use-dispatch.d.ts +10 -19
- package/build-types/components/use-dispatch/use-dispatch.d.ts.map +1 -1
- package/build-types/components/use-select/index.d.ts +24 -28
- package/build-types/components/use-select/index.d.ts.map +1 -1
- package/build-types/components/with-dispatch/index.d.ts +10 -11
- package/build-types/components/with-dispatch/index.d.ts.map +1 -1
- package/build-types/components/with-registry/index.d.ts +2 -6
- package/build-types/components/with-registry/index.d.ts.map +1 -1
- package/build-types/components/with-select/index.d.ts +7 -8
- package/build-types/components/with-select/index.d.ts.map +1 -1
- package/build-types/controls.d.ts +57 -44
- package/build-types/controls.d.ts.map +1 -1
- package/build-types/default-registry.d.ts +2 -2
- package/build-types/default-registry.d.ts.map +1 -1
- package/build-types/dispatch.d.ts +4 -0
- package/build-types/dispatch.d.ts.map +1 -1
- package/build-types/factory.d.ts +2 -1
- package/build-types/factory.d.ts.map +1 -1
- package/build-types/index.d.ts.map +1 -1
- package/build-types/plugins/index.d.ts +1 -1
- package/build-types/plugins/index.d.ts.map +1 -1
- package/build-types/plugins/persistence/index.d.ts +38 -27
- package/build-types/plugins/persistence/index.d.ts.map +1 -1
- package/build-types/plugins/persistence/storage/default.d.ts +7 -1
- package/build-types/plugins/persistence/storage/default.d.ts.map +1 -1
- package/build-types/plugins/persistence/storage/object.d.ts +4 -5
- package/build-types/plugins/persistence/storage/object.d.ts.map +1 -1
- package/build-types/promise-middleware.d.ts +6 -4
- package/build-types/promise-middleware.d.ts.map +1 -1
- package/build-types/redux-store/index.d.ts +12 -15
- package/build-types/redux-store/index.d.ts.map +1 -1
- package/build-types/redux-store/metadata/selectors.d.ts +45 -50
- package/build-types/redux-store/metadata/selectors.d.ts.map +1 -1
- package/build-types/redux-store/metadata/types.d.ts +48 -0
- package/build-types/redux-store/metadata/types.d.ts.map +1 -0
- package/build-types/registry.d.ts +5 -49
- package/build-types/registry.d.ts.map +1 -1
- package/build-types/resolvers-cache-middleware.d.ts +13 -7
- package/build-types/resolvers-cache-middleware.d.ts.map +1 -1
- package/build-types/select.d.ts +3 -0
- package/build-types/select.d.ts.map +1 -1
- package/build-types/store/index.d.ts +5 -12
- package/build-types/store/index.d.ts.map +1 -1
- package/build-types/types.d.ts +201 -10
- package/build-types/types.d.ts.map +1 -1
- package/package.json +9 -9
- package/src/components/async-mode-provider/{context.js → context.ts} +0 -1
- package/src/components/async-mode-provider/{use-async-mode.js → use-async-mode.ts} +1 -1
- package/src/components/registry-provider/{use-registry.js → use-registry.ts} +3 -2
- package/src/components/use-dispatch/{use-dispatch-with-map.js → use-dispatch-with-map.ts} +18 -9
- package/src/components/use-dispatch/{use-dispatch.js → use-dispatch.ts} +25 -20
- package/src/components/use-select/{index.js → index.ts} +84 -70
- package/src/components/with-dispatch/{index.js → index.tsx} +20 -13
- package/src/components/with-registry/{index.js → index.tsx} +1 -5
- package/src/components/with-select/{index.js → index.tsx} +17 -10
- package/src/{controls.js → controls.ts} +53 -26
- package/src/{default-registry.js → default-registry.ts} +3 -1
- package/src/dispatch.ts +7 -1
- package/src/factory.ts +4 -3
- package/src/index.ts +4 -8
- package/src/plugins/persistence/{index.js → index.ts} +93 -54
- package/src/plugins/persistence/storage/{default.js → default.ts} +3 -2
- package/src/plugins/persistence/storage/object.ts +25 -0
- package/src/{promise-middleware.js → promise-middleware.ts} +3 -3
- package/src/redux-store/{index.js → index.ts} +218 -131
- package/src/redux-store/metadata/{selectors.js → selectors.ts} +83 -54
- package/src/redux-store/metadata/types.ts +100 -0
- package/src/{registry.js → registry.ts} +98 -88
- package/src/{resolvers-cache-middleware.js → resolvers-cache-middleware.ts} +22 -7
- package/src/select.ts +6 -1
- package/src/store/{index.js → index.ts} +13 -8
- package/src/types.ts +263 -24
- package/src/plugins/persistence/storage/object.js +0 -23
- /package/src/components/async-mode-provider/{index.js → index.ts} +0 -0
- /package/src/components/registry-provider/{context.js → context.ts} +0 -0
- /package/src/components/registry-provider/{index.js → index.ts} +0 -0
- /package/src/components/use-dispatch/{index.js → index.ts} +0 -0
- /package/src/plugins/{index.js → index.ts} +0 -0
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
* External dependencies
|
|
3
3
|
*/
|
|
4
4
|
import { createStore, applyMiddleware } from 'redux';
|
|
5
|
+
import type { Store as ReduxStore, StoreEnhancer } from 'redux';
|
|
5
6
|
import EquivalentKeyMap from 'equivalent-key-map';
|
|
6
7
|
|
|
7
8
|
/**
|
|
@@ -22,23 +23,43 @@ import createThunkMiddleware from './thunk-middleware';
|
|
|
22
23
|
import metadataReducer from './metadata/reducer';
|
|
23
24
|
import * as metadataSelectors from './metadata/selectors';
|
|
24
25
|
import * as metadataActions from './metadata/actions';
|
|
26
|
+
import type {
|
|
27
|
+
DataRegistry,
|
|
28
|
+
ListenerFunction,
|
|
29
|
+
StoreDescriptor,
|
|
30
|
+
ReduxStoreConfig,
|
|
31
|
+
ActionCreator,
|
|
32
|
+
NormalizedResolver,
|
|
33
|
+
} from '../types';
|
|
25
34
|
|
|
26
35
|
export { combineReducers };
|
|
27
36
|
|
|
28
|
-
/** @typedef {import('../types').DataRegistry} DataRegistry */
|
|
29
|
-
/** @typedef {import('../types').ListenerFunction} ListenerFunction */
|
|
30
37
|
/**
|
|
31
|
-
*
|
|
32
|
-
* @template {import('../types').AnyConfig} C
|
|
33
|
-
*/
|
|
34
|
-
/**
|
|
35
|
-
* @typedef {import('../types').ReduxStoreConfig<State,Actions,Selectors>} ReduxStoreConfig
|
|
36
|
-
* @template State
|
|
37
|
-
* @template {Record<string,import('../types').ActionCreator>} Actions
|
|
38
|
-
* @template Selectors
|
|
38
|
+
* Augment the Redux store with internal properties used by the data module.
|
|
39
39
|
*/
|
|
40
|
+
interface AugmentedReduxStore extends ReduxStore {
|
|
41
|
+
__unstableOriginalGetState: ReduxStore[ 'getState' ];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
interface ResolversCache {
|
|
45
|
+
isRunning: ( selectorName: string, args: unknown[] ) => boolean;
|
|
46
|
+
clear: ( selectorName: string, args: unknown[] ) => void;
|
|
47
|
+
markAsRunning: ( selectorName: string, args: unknown[] ) => void;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
interface BindingCache {
|
|
51
|
+
get: ( itemName: string ) => ( ( ...args: unknown[] ) => unknown ) | null;
|
|
52
|
+
}
|
|
40
53
|
|
|
41
|
-
|
|
54
|
+
interface SelectorLike {
|
|
55
|
+
( ...args: any[] ): any;
|
|
56
|
+
hasResolver?: boolean;
|
|
57
|
+
isRegistrySelector?: boolean;
|
|
58
|
+
registry?: DataRegistry;
|
|
59
|
+
__unstableNormalizeArgs?: ( args: unknown[] ) => unknown[];
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const trimUndefinedValues = ( array: unknown[] ): unknown[] => {
|
|
42
63
|
const result = [ ...array ];
|
|
43
64
|
for ( let i = result.length - 1; i >= 0; i-- ) {
|
|
44
65
|
if ( result[ i ] === undefined ) {
|
|
@@ -52,11 +73,14 @@ const trimUndefinedValues = ( array ) => {
|
|
|
52
73
|
* Creates a new object with the same keys, but with `callback()` called as
|
|
53
74
|
* a transformer function on each of the values.
|
|
54
75
|
*
|
|
55
|
-
* @param
|
|
56
|
-
* @param
|
|
57
|
-
* @return
|
|
76
|
+
* @param obj The object to transform.
|
|
77
|
+
* @param callback The function to transform each object value.
|
|
78
|
+
* @return Transformed object.
|
|
58
79
|
*/
|
|
59
|
-
const mapValues =
|
|
80
|
+
const mapValues = < T, U >(
|
|
81
|
+
obj: Record< string, T > | undefined,
|
|
82
|
+
callback: ( value: T, key: string ) => U
|
|
83
|
+
): Record< string, U > =>
|
|
60
84
|
Object.fromEntries(
|
|
61
85
|
Object.entries( obj ?? {} ).map( ( [ key, value ] ) => [
|
|
62
86
|
key,
|
|
@@ -65,12 +89,15 @@ const mapValues = ( obj, callback ) =>
|
|
|
65
89
|
);
|
|
66
90
|
|
|
67
91
|
// Convert non serializable types to plain objects
|
|
68
|
-
const devToolsReplacer = (
|
|
92
|
+
const devToolsReplacer = ( _key: string, state: unknown ): unknown => {
|
|
69
93
|
if ( state instanceof Map ) {
|
|
70
94
|
return Object.fromEntries( state );
|
|
71
95
|
}
|
|
72
96
|
|
|
73
|
-
if (
|
|
97
|
+
if (
|
|
98
|
+
typeof window !== 'undefined' &&
|
|
99
|
+
state instanceof window.HTMLElement
|
|
100
|
+
) {
|
|
74
101
|
return null;
|
|
75
102
|
}
|
|
76
103
|
|
|
@@ -80,25 +107,25 @@ const devToolsReplacer = ( key, state ) => {
|
|
|
80
107
|
/**
|
|
81
108
|
* Create a cache to track whether resolvers started running or not.
|
|
82
109
|
*
|
|
83
|
-
* @return
|
|
110
|
+
* @return Resolvers Cache.
|
|
84
111
|
*/
|
|
85
|
-
function createResolversCache() {
|
|
86
|
-
const cache = {};
|
|
112
|
+
function createResolversCache(): ResolversCache {
|
|
113
|
+
const cache: Record< string, EquivalentKeyMap< unknown[], boolean > > = {};
|
|
87
114
|
return {
|
|
88
|
-
isRunning( selectorName, args ) {
|
|
89
|
-
return (
|
|
115
|
+
isRunning( selectorName: string, args: unknown[] ): boolean {
|
|
116
|
+
return !! (
|
|
90
117
|
cache[ selectorName ] &&
|
|
91
118
|
cache[ selectorName ].get( trimUndefinedValues( args ) )
|
|
92
119
|
);
|
|
93
120
|
},
|
|
94
121
|
|
|
95
|
-
clear( selectorName, args ) {
|
|
122
|
+
clear( selectorName: string, args: unknown[] ): void {
|
|
96
123
|
if ( cache[ selectorName ] ) {
|
|
97
124
|
cache[ selectorName ].delete( trimUndefinedValues( args ) );
|
|
98
125
|
}
|
|
99
126
|
},
|
|
100
127
|
|
|
101
|
-
markAsRunning( selectorName, args ) {
|
|
128
|
+
markAsRunning( selectorName: string, args: unknown[] ): void {
|
|
102
129
|
if ( ! cache[ selectorName ] ) {
|
|
103
130
|
cache[ selectorName ] = new EquivalentKeyMap();
|
|
104
131
|
}
|
|
@@ -108,11 +135,20 @@ function createResolversCache() {
|
|
|
108
135
|
};
|
|
109
136
|
}
|
|
110
137
|
|
|
111
|
-
function createBindingCache(
|
|
112
|
-
|
|
138
|
+
function createBindingCache(
|
|
139
|
+
getItem: ( name: string ) => ( ( ...args: any[] ) => any ) | undefined,
|
|
140
|
+
bindItem: (
|
|
141
|
+
item: ( ...args: any[] ) => any,
|
|
142
|
+
name: string
|
|
143
|
+
) => ( ...args: unknown[] ) => unknown
|
|
144
|
+
): BindingCache {
|
|
145
|
+
const cache = new WeakMap<
|
|
146
|
+
( ...args: any[] ) => any,
|
|
147
|
+
( ...args: unknown[] ) => unknown
|
|
148
|
+
>();
|
|
113
149
|
|
|
114
150
|
return {
|
|
115
|
-
get( itemName ) {
|
|
151
|
+
get( itemName: string ) {
|
|
116
152
|
const item = getItem( itemName );
|
|
117
153
|
if ( ! item ) {
|
|
118
154
|
return null;
|
|
@@ -127,9 +163,12 @@ function createBindingCache( getItem, bindItem ) {
|
|
|
127
163
|
};
|
|
128
164
|
}
|
|
129
165
|
|
|
130
|
-
function createPrivateProxy
|
|
166
|
+
function createPrivateProxy< T extends Record< string, any > >(
|
|
167
|
+
publicItems: T,
|
|
168
|
+
privateItems: BindingCache
|
|
169
|
+
): T {
|
|
131
170
|
return new Proxy( publicItems, {
|
|
132
|
-
get: ( target, itemName ) =>
|
|
171
|
+
get: ( target, itemName: string ) =>
|
|
133
172
|
privateItems.get( itemName ) || Reflect.get( target, itemName ),
|
|
134
173
|
} );
|
|
135
174
|
}
|
|
@@ -150,32 +189,36 @@ function createPrivateProxy( publicItems, privateItems ) {
|
|
|
150
189
|
* } );
|
|
151
190
|
* ```
|
|
152
191
|
*
|
|
153
|
-
* @
|
|
154
|
-
* @
|
|
155
|
-
*
|
|
156
|
-
*
|
|
157
|
-
* @param {ReduxStoreConfig<State,Actions,Selectors>} options Registered store options, with properties
|
|
158
|
-
* describing reducer, actions, selectors,
|
|
159
|
-
* and resolvers.
|
|
192
|
+
* @param key Unique namespace identifier.
|
|
193
|
+
* @param options Registered store options, with properties
|
|
194
|
+
* describing reducer, actions, selectors,
|
|
195
|
+
* and resolvers.
|
|
160
196
|
*
|
|
161
|
-
* @return
|
|
197
|
+
* @return Store Object.
|
|
162
198
|
*/
|
|
163
|
-
export default function createReduxStore
|
|
164
|
-
|
|
165
|
-
|
|
199
|
+
export default function createReduxStore< State, Actions, Selectors >(
|
|
200
|
+
key: string,
|
|
201
|
+
options: ReduxStoreConfig< State, Actions, Selectors >
|
|
202
|
+
): StoreDescriptor< ReduxStoreConfig< State, Actions, Selectors > > {
|
|
203
|
+
const privateActions: Record< string, ActionCreator > = {};
|
|
204
|
+
const privateSelectors: Record< string, SelectorLike > = {};
|
|
166
205
|
const privateRegistrationFunctions = {
|
|
167
206
|
privateActions,
|
|
168
|
-
registerPrivateActions: (
|
|
207
|
+
registerPrivateActions: (
|
|
208
|
+
actions: Record< string, ActionCreator >
|
|
209
|
+
) => {
|
|
169
210
|
Object.assign( privateActions, actions );
|
|
170
211
|
},
|
|
171
212
|
privateSelectors,
|
|
172
|
-
registerPrivateSelectors: (
|
|
213
|
+
registerPrivateSelectors: (
|
|
214
|
+
selectors: Record< string, SelectorLike >
|
|
215
|
+
) => {
|
|
173
216
|
Object.assign( privateSelectors, selectors );
|
|
174
217
|
},
|
|
175
218
|
};
|
|
176
219
|
const storeDescriptor = {
|
|
177
220
|
name: key,
|
|
178
|
-
instantiate: ( registry ) => {
|
|
221
|
+
instantiate: ( registry: DataRegistry ) => {
|
|
179
222
|
/**
|
|
180
223
|
* Stores listener functions registered with `subscribe()`.
|
|
181
224
|
*
|
|
@@ -185,10 +228,8 @@ export default function createReduxStore( key, options ) {
|
|
|
185
228
|
* subscription in this store instance it's possible to
|
|
186
229
|
* optimize checking if the state has changed before calling
|
|
187
230
|
* each listener.
|
|
188
|
-
*
|
|
189
|
-
* @type {Set<ListenerFunction>}
|
|
190
231
|
*/
|
|
191
|
-
const listeners = new Set();
|
|
232
|
+
const listeners = new Set< ListenerFunction >();
|
|
192
233
|
const reducer = options.reducer;
|
|
193
234
|
|
|
194
235
|
// Object that every thunk function receives as the first argument. It contains the
|
|
@@ -212,7 +253,7 @@ export default function createReduxStore( key, options ) {
|
|
|
212
253
|
options,
|
|
213
254
|
registry,
|
|
214
255
|
thunkArgs
|
|
215
|
-
);
|
|
256
|
+
) as AugmentedReduxStore;
|
|
216
257
|
|
|
217
258
|
// Expose the private registration functions on the store
|
|
218
259
|
// so they can be copied to a sub registry in registry.js.
|
|
@@ -223,8 +264,8 @@ export default function createReduxStore( key, options ) {
|
|
|
223
264
|
// These are the functions that are returned by `useDispatch`, for example.
|
|
224
265
|
// It always returns a `Promise`, although actions are not always async. That's an
|
|
225
266
|
// unfortunate backward compatibility measure.
|
|
226
|
-
function bindAction( action ) {
|
|
227
|
-
return ( ...args ) =>
|
|
267
|
+
function bindAction( action: ( ...args: any[] ) => any ) {
|
|
268
|
+
return ( ...args: unknown[] ) =>
|
|
228
269
|
Promise.resolve( store.dispatch( action( ...args ) ) );
|
|
229
270
|
}
|
|
230
271
|
|
|
@@ -232,8 +273,16 @@ export default function createReduxStore( key, options ) {
|
|
|
232
273
|
* Object with all public actions, both metadata and store actions.
|
|
233
274
|
*/
|
|
234
275
|
const actions = {
|
|
235
|
-
...mapValues(
|
|
236
|
-
|
|
276
|
+
...mapValues(
|
|
277
|
+
metadataActions as Record< string, ActionCreator >,
|
|
278
|
+
bindAction
|
|
279
|
+
),
|
|
280
|
+
...mapValues(
|
|
281
|
+
options.actions as
|
|
282
|
+
| Record< string, ActionCreator >
|
|
283
|
+
| undefined,
|
|
284
|
+
bindAction
|
|
285
|
+
),
|
|
237
286
|
};
|
|
238
287
|
|
|
239
288
|
// Object with both public and private actions. Private actions are accessed through a proxy,
|
|
@@ -253,8 +302,10 @@ export default function createReduxStore( key, options ) {
|
|
|
253
302
|
// correspond to bound registered actions, both public and private. Implemented with the proxy
|
|
254
303
|
// `get` method, delegating to `allActions`.
|
|
255
304
|
const thunkDispatch = new Proxy(
|
|
256
|
-
( action ) => store.dispatch( action ),
|
|
257
|
-
{
|
|
305
|
+
( action: any ) => store.dispatch( action ),
|
|
306
|
+
{
|
|
307
|
+
get: ( _target, name: string ) => allActions[ name ],
|
|
308
|
+
}
|
|
258
309
|
);
|
|
259
310
|
|
|
260
311
|
// To the public `actions` object, add the "locked" `allActions` object. When used,
|
|
@@ -262,17 +313,24 @@ export default function createReduxStore( key, options ) {
|
|
|
262
313
|
lock( actions, allActions );
|
|
263
314
|
|
|
264
315
|
// If we have selector resolvers, convert them to a normalized form.
|
|
265
|
-
const resolvers =
|
|
266
|
-
|
|
267
|
-
|
|
316
|
+
const resolvers: Record< string, NormalizedResolver > =
|
|
317
|
+
options.resolvers
|
|
318
|
+
? mapValues(
|
|
319
|
+
options.resolvers as Record< string, any >,
|
|
320
|
+
mapResolver
|
|
321
|
+
)
|
|
322
|
+
: {};
|
|
268
323
|
|
|
269
324
|
// Bind a selector to the store. Call the selector with the current state, correct registry,
|
|
270
325
|
// and if there is a resolver, attach the resolver logic to the selector.
|
|
271
|
-
function bindSelector(
|
|
326
|
+
function bindSelector(
|
|
327
|
+
selector: SelectorLike,
|
|
328
|
+
selectorName: string
|
|
329
|
+
): SelectorLike {
|
|
272
330
|
if ( selector.isRegistrySelector ) {
|
|
273
331
|
selector.registry = registry;
|
|
274
332
|
}
|
|
275
|
-
const boundSelector = ( ...args ) => {
|
|
333
|
+
const boundSelector: SelectorLike = ( ...args: any[] ) => {
|
|
276
334
|
args = normalize( selector, args );
|
|
277
335
|
const state = store.__unstableOriginalGetState();
|
|
278
336
|
// Before calling the selector, switch to the correct registry.
|
|
@@ -307,16 +365,19 @@ export default function createReduxStore( key, options ) {
|
|
|
307
365
|
|
|
308
366
|
// Metadata selectors are bound differently: different state (`state.metadata`), no resolvers,
|
|
309
367
|
// normalization depending on the target selector.
|
|
310
|
-
function bindMetadataSelector(
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
368
|
+
function bindMetadataSelector(
|
|
369
|
+
metaDataSelector: ( ...args: any[] ) => any
|
|
370
|
+
): SelectorLike {
|
|
371
|
+
const boundSelector: SelectorLike = (
|
|
372
|
+
selectorName: string,
|
|
373
|
+
selectorArgs: unknown[],
|
|
374
|
+
...args: unknown[]
|
|
315
375
|
) => {
|
|
316
376
|
// Normalize the arguments passed to the target selector.
|
|
317
377
|
if ( selectorName ) {
|
|
318
|
-
const targetSelector =
|
|
319
|
-
|
|
378
|
+
const targetSelector = ( options.selectors as any )?.[
|
|
379
|
+
selectorName
|
|
380
|
+
];
|
|
320
381
|
if ( targetSelector ) {
|
|
321
382
|
selectorArgs = normalize(
|
|
322
383
|
targetSelector,
|
|
@@ -341,18 +402,24 @@ export default function createReduxStore( key, options ) {
|
|
|
341
402
|
// Perform binding of both metadata and store selectors and combine them in one
|
|
342
403
|
// `selectors` object. These are all public selectors of the store.
|
|
343
404
|
const boundMetadataSelectors = mapValues(
|
|
344
|
-
metadataSelectors
|
|
405
|
+
metadataSelectors as Record<
|
|
406
|
+
string,
|
|
407
|
+
( ...args: any[] ) => any
|
|
408
|
+
>,
|
|
345
409
|
bindMetadataSelector
|
|
346
410
|
);
|
|
347
411
|
|
|
348
|
-
const boundSelectors = mapValues(
|
|
412
|
+
const boundSelectors = mapValues(
|
|
413
|
+
options.selectors as Record< string, SelectorLike > | undefined,
|
|
414
|
+
bindSelector
|
|
415
|
+
);
|
|
349
416
|
|
|
350
417
|
const selectors = {
|
|
351
418
|
...boundMetadataSelectors,
|
|
352
419
|
...boundSelectors,
|
|
353
420
|
};
|
|
354
421
|
|
|
355
|
-
// Cache of
|
|
422
|
+
// Cache of bound private selectors. They are bound only when first accessed, because
|
|
356
423
|
// new private selectors can be registered at any time (with `registerPrivateSelectors`).
|
|
357
424
|
// Once bound, they are cached to give them a stable identity.
|
|
358
425
|
const boundPrivateSelectors = createBindingCache(
|
|
@@ -376,8 +443,11 @@ export default function createReduxStore( key, options ) {
|
|
|
376
443
|
// correspond to bound registered selectors, both public and private. Implemented with the proxy
|
|
377
444
|
// `get` method, delegating to `allSelectors`.
|
|
378
445
|
const thunkSelect = new Proxy(
|
|
379
|
-
( selector ) =>
|
|
380
|
-
|
|
446
|
+
( selector: ( state: any ) => any ) =>
|
|
447
|
+
selector( store.__unstableOriginalGetState() ),
|
|
448
|
+
{
|
|
449
|
+
get: ( _target, name: string ) => allSelectors[ name ],
|
|
450
|
+
}
|
|
381
451
|
);
|
|
382
452
|
|
|
383
453
|
// To the public `selectors` object, add the "locked" `allSelectors` object. When used,
|
|
@@ -401,7 +471,10 @@ export default function createReduxStore( key, options ) {
|
|
|
401
471
|
const allResolveSelectors = createPrivateProxy(
|
|
402
472
|
resolveSelectors,
|
|
403
473
|
createBindingCache(
|
|
404
|
-
( name ) =>
|
|
474
|
+
( name ) =>
|
|
475
|
+
boundPrivateSelectors.get( name ) as (
|
|
476
|
+
...args: any[]
|
|
477
|
+
) => any | undefined,
|
|
405
478
|
bindResolveSelector
|
|
406
479
|
)
|
|
407
480
|
);
|
|
@@ -424,7 +497,10 @@ export default function createReduxStore( key, options ) {
|
|
|
424
497
|
const allSuspendSelectors = createPrivateProxy(
|
|
425
498
|
suspendSelectors,
|
|
426
499
|
createBindingCache(
|
|
427
|
-
( name ) =>
|
|
500
|
+
( name ) =>
|
|
501
|
+
boundPrivateSelectors.get( name ) as (
|
|
502
|
+
...args: any[]
|
|
503
|
+
) => any | undefined,
|
|
428
504
|
bindSuspendSelector
|
|
429
505
|
)
|
|
430
506
|
);
|
|
@@ -445,13 +521,11 @@ export default function createReduxStore( key, options ) {
|
|
|
445
521
|
|
|
446
522
|
// Customize subscribe behavior to call listeners only on effective change,
|
|
447
523
|
// not on every dispatch.
|
|
448
|
-
const subscribe =
|
|
449
|
-
|
|
450
|
-
( ( listener ) => {
|
|
451
|
-
listeners.add( listener );
|
|
524
|
+
const subscribe = ( listener: ListenerFunction ) => {
|
|
525
|
+
listeners.add( listener );
|
|
452
526
|
|
|
453
|
-
|
|
454
|
-
|
|
527
|
+
return () => listeners.delete( listener );
|
|
528
|
+
};
|
|
455
529
|
|
|
456
530
|
let lastState = store.__unstableOriginalGetState();
|
|
457
531
|
store.subscribe( () => {
|
|
@@ -488,27 +562,34 @@ export default function createReduxStore( key, options ) {
|
|
|
488
562
|
// public actions and selectors are stored.
|
|
489
563
|
lock( storeDescriptor, privateRegistrationFunctions );
|
|
490
564
|
|
|
491
|
-
return storeDescriptor
|
|
565
|
+
return storeDescriptor as unknown as StoreDescriptor<
|
|
566
|
+
ReduxStoreConfig< State, Actions, Selectors >
|
|
567
|
+
>;
|
|
492
568
|
}
|
|
493
569
|
|
|
494
570
|
/**
|
|
495
571
|
* Creates a redux store for a namespace.
|
|
496
572
|
*
|
|
497
|
-
* @param
|
|
498
|
-
* @param
|
|
499
|
-
*
|
|
500
|
-
*
|
|
501
|
-
* @param
|
|
502
|
-
* @param
|
|
503
|
-
* @return
|
|
573
|
+
* @param key Unique namespace identifier.
|
|
574
|
+
* @param options Registered store options, with properties
|
|
575
|
+
* describing reducer, actions, selectors,
|
|
576
|
+
* and resolvers.
|
|
577
|
+
* @param registry Registry reference.
|
|
578
|
+
* @param thunkArgs Argument object for the thunk middleware.
|
|
579
|
+
* @return Newly created redux store.
|
|
504
580
|
*/
|
|
505
|
-
function instantiateReduxStore(
|
|
581
|
+
function instantiateReduxStore(
|
|
582
|
+
key: string,
|
|
583
|
+
options: ReduxStoreConfig< any, any, any >,
|
|
584
|
+
registry: DataRegistry,
|
|
585
|
+
thunkArgs: unknown
|
|
586
|
+
): ReduxStore {
|
|
506
587
|
const controls = {
|
|
507
588
|
...options.controls,
|
|
508
589
|
...builtinControls,
|
|
509
590
|
};
|
|
510
591
|
|
|
511
|
-
const normalizedControls = mapValues( controls, ( control ) =>
|
|
592
|
+
const normalizedControls = mapValues( controls, ( control: any ) =>
|
|
512
593
|
control.isRegistryControl ? control( registry ) : control
|
|
513
594
|
);
|
|
514
595
|
|
|
@@ -519,13 +600,13 @@ function instantiateReduxStore( key, options, registry, thunkArgs ) {
|
|
|
519
600
|
createThunkMiddleware( thunkArgs ),
|
|
520
601
|
];
|
|
521
602
|
|
|
522
|
-
const enhancers = [ applyMiddleware( ...middlewares ) ];
|
|
603
|
+
const enhancers: StoreEnhancer[] = [ applyMiddleware( ...middlewares ) ];
|
|
523
604
|
if (
|
|
524
605
|
typeof window !== 'undefined' &&
|
|
525
|
-
window.__REDUX_DEVTOOLS_EXTENSION__
|
|
606
|
+
( window as any ).__REDUX_DEVTOOLS_EXTENSION__
|
|
526
607
|
) {
|
|
527
608
|
enhancers.push(
|
|
528
|
-
window.__REDUX_DEVTOOLS_EXTENSION__( {
|
|
609
|
+
( window as any ).__REDUX_DEVTOOLS_EXTENSION__( {
|
|
529
610
|
name: key,
|
|
530
611
|
instanceId: key,
|
|
531
612
|
serialize: {
|
|
@@ -543,28 +624,31 @@ function instantiateReduxStore( key, options, registry, thunkArgs ) {
|
|
|
543
624
|
|
|
544
625
|
return createStore(
|
|
545
626
|
enhancedReducer,
|
|
546
|
-
{ root: initialState },
|
|
547
|
-
compose( enhancers )
|
|
627
|
+
{ root: initialState } as any,
|
|
628
|
+
compose( ...enhancers ) as unknown as StoreEnhancer
|
|
548
629
|
);
|
|
549
630
|
}
|
|
550
631
|
|
|
551
632
|
/**
|
|
552
633
|
* Maps selectors to functions that return a resolution promise for them.
|
|
553
634
|
*
|
|
554
|
-
* @param
|
|
555
|
-
* @param
|
|
635
|
+
* @param store The redux store the selectors are bound to.
|
|
636
|
+
* @param boundMetadataSelectors The bound metadata selectors.
|
|
556
637
|
*
|
|
557
|
-
* @return
|
|
638
|
+
* @return Function that maps selectors to resolvers.
|
|
558
639
|
*/
|
|
559
|
-
function mapResolveSelector(
|
|
560
|
-
|
|
640
|
+
function mapResolveSelector(
|
|
641
|
+
store: ReduxStore,
|
|
642
|
+
boundMetadataSelectors: Record< string, SelectorLike >
|
|
643
|
+
) {
|
|
644
|
+
return ( selector: SelectorLike, selectorName: string ) => {
|
|
561
645
|
// If the selector doesn't have a resolver, just convert the return value
|
|
562
646
|
// (including exceptions) to a Promise, no additional extra behavior is needed.
|
|
563
647
|
if ( ! selector.hasResolver ) {
|
|
564
|
-
return async ( ...args ) => selector
|
|
648
|
+
return async ( ...args: unknown[] ) => selector( ...args );
|
|
565
649
|
}
|
|
566
650
|
|
|
567
|
-
return ( ...args ) =>
|
|
651
|
+
return ( ...args: unknown[] ) =>
|
|
568
652
|
new Promise( ( resolve, reject ) => {
|
|
569
653
|
const hasFinished = () => {
|
|
570
654
|
return boundMetadataSelectors.hasFinishedResolution(
|
|
@@ -572,7 +656,7 @@ function mapResolveSelector( store, boundMetadataSelectors ) {
|
|
|
572
656
|
args
|
|
573
657
|
);
|
|
574
658
|
};
|
|
575
|
-
const finalize = ( result ) => {
|
|
659
|
+
const finalize = ( result: unknown ) => {
|
|
576
660
|
const hasFailed =
|
|
577
661
|
boundMetadataSelectors.hasResolutionFailed(
|
|
578
662
|
selectorName,
|
|
@@ -588,7 +672,7 @@ function mapResolveSelector( store, boundMetadataSelectors ) {
|
|
|
588
672
|
resolve( result );
|
|
589
673
|
}
|
|
590
674
|
};
|
|
591
|
-
const getResult = () => selector
|
|
675
|
+
const getResult = () => selector( ...args );
|
|
592
676
|
|
|
593
677
|
// Trigger the selector (to trigger the resolver)
|
|
594
678
|
const result = getResult();
|
|
@@ -609,20 +693,23 @@ function mapResolveSelector( store, boundMetadataSelectors ) {
|
|
|
609
693
|
/**
|
|
610
694
|
* Maps selectors to functions that throw a suspense promise if not yet resolved.
|
|
611
695
|
*
|
|
612
|
-
* @param
|
|
613
|
-
* @param
|
|
696
|
+
* @param store The redux store the selectors select from.
|
|
697
|
+
* @param boundMetadataSelectors The bound metadata selectors.
|
|
614
698
|
*
|
|
615
|
-
* @return
|
|
699
|
+
* @return Function that maps selectors to their suspending versions.
|
|
616
700
|
*/
|
|
617
|
-
function mapSuspendSelector(
|
|
618
|
-
|
|
701
|
+
function mapSuspendSelector(
|
|
702
|
+
store: ReduxStore,
|
|
703
|
+
boundMetadataSelectors: Record< string, SelectorLike >
|
|
704
|
+
) {
|
|
705
|
+
return ( selector: SelectorLike, selectorName: string ) => {
|
|
619
706
|
// Selector without a resolver doesn't have any extra suspense behavior.
|
|
620
707
|
if ( ! selector.hasResolver ) {
|
|
621
708
|
return selector;
|
|
622
709
|
}
|
|
623
710
|
|
|
624
|
-
return ( ...args ) => {
|
|
625
|
-
const result = selector
|
|
711
|
+
return ( ...args: unknown[] ) => {
|
|
712
|
+
const result = selector( ...args );
|
|
626
713
|
|
|
627
714
|
if (
|
|
628
715
|
boundMetadataSelectors.hasFinishedResolution(
|
|
@@ -645,7 +732,7 @@ function mapSuspendSelector( store, boundMetadataSelectors ) {
|
|
|
645
732
|
return result;
|
|
646
733
|
}
|
|
647
734
|
|
|
648
|
-
throw new Promise( ( resolve ) => {
|
|
735
|
+
throw new Promise< void >( ( resolve ) => {
|
|
649
736
|
const unsubscribe = store.subscribe( () => {
|
|
650
737
|
if (
|
|
651
738
|
boundMetadataSelectors.hasFinishedResolution(
|
|
@@ -666,9 +753,9 @@ function mapSuspendSelector( store, boundMetadataSelectors ) {
|
|
|
666
753
|
* Convert a resolver to a normalized form, an object with `fulfill` method and
|
|
667
754
|
* optional methods like `isFulfilled`.
|
|
668
755
|
*
|
|
669
|
-
* @param
|
|
756
|
+
* @param resolver Resolver to convert
|
|
670
757
|
*/
|
|
671
|
-
function mapResolver( resolver ) {
|
|
758
|
+
function mapResolver( resolver: any ): NormalizedResolver {
|
|
672
759
|
if ( resolver.fulfill ) {
|
|
673
760
|
return resolver;
|
|
674
761
|
}
|
|
@@ -684,22 +771,22 @@ function mapResolver( resolver ) {
|
|
|
684
771
|
* Resolvers are side effects invoked once per argument set of a given selector call,
|
|
685
772
|
* used in ensuring that the data needs for the selector are satisfied.
|
|
686
773
|
*
|
|
687
|
-
* @param
|
|
688
|
-
* @param
|
|
689
|
-
* @param
|
|
690
|
-
* @param
|
|
691
|
-
* @param
|
|
692
|
-
* @param
|
|
774
|
+
* @param selector The selector function to be bound.
|
|
775
|
+
* @param selectorName The selector name.
|
|
776
|
+
* @param resolver Resolver to call.
|
|
777
|
+
* @param store The redux store to which the resolvers should be mapped.
|
|
778
|
+
* @param resolversCache Resolvers Cache.
|
|
779
|
+
* @param boundMetadataSelectors The bound metadata selectors.
|
|
693
780
|
*/
|
|
694
781
|
function mapSelectorWithResolver(
|
|
695
|
-
selector,
|
|
696
|
-
selectorName,
|
|
697
|
-
resolver,
|
|
698
|
-
store,
|
|
699
|
-
resolversCache,
|
|
700
|
-
boundMetadataSelectors
|
|
701
|
-
) {
|
|
702
|
-
function fulfillSelector( args ) {
|
|
782
|
+
selector: SelectorLike,
|
|
783
|
+
selectorName: string,
|
|
784
|
+
resolver: NormalizedResolver,
|
|
785
|
+
store: AugmentedReduxStore,
|
|
786
|
+
resolversCache: ResolversCache,
|
|
787
|
+
boundMetadataSelectors: Record< string, SelectorLike >
|
|
788
|
+
): SelectorLike {
|
|
789
|
+
function fulfillSelector( args: unknown[] ): void {
|
|
703
790
|
if (
|
|
704
791
|
resolversCache.isRunning( selectorName, args ) ||
|
|
705
792
|
boundMetadataSelectors.hasStartedResolution( selectorName, args )
|
|
@@ -735,7 +822,7 @@ function mapSelectorWithResolver(
|
|
|
735
822
|
}, 0 );
|
|
736
823
|
}
|
|
737
824
|
|
|
738
|
-
const selectorResolver = ( ...args ) => {
|
|
825
|
+
const selectorResolver: SelectorLike = ( ...args: unknown[] ) => {
|
|
739
826
|
args = normalize( selector, args );
|
|
740
827
|
fulfillSelector( args );
|
|
741
828
|
return selector( ...args );
|
|
@@ -748,11 +835,11 @@ function mapSelectorWithResolver(
|
|
|
748
835
|
* Applies selector's normalization function to the given arguments
|
|
749
836
|
* if it exists.
|
|
750
837
|
*
|
|
751
|
-
* @param
|
|
752
|
-
* @param
|
|
753
|
-
* @return
|
|
838
|
+
* @param selector The selector potentially with a normalization method property.
|
|
839
|
+
* @param args selector arguments to normalize.
|
|
840
|
+
* @return Potentially normalized arguments.
|
|
754
841
|
*/
|
|
755
|
-
function normalize( selector, args ) {
|
|
842
|
+
function normalize( selector: SelectorLike, args: unknown[] ): unknown[] {
|
|
756
843
|
if (
|
|
757
844
|
selector.__unstableNormalizeArgs &&
|
|
758
845
|
typeof selector.__unstableNormalizeArgs === 'function' &&
|