@wordpress/data 10.32.0 → 10.32.1-next.b8c8708f3.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/build/components/async-mode-provider/context.js +35 -54
- package/build/components/async-mode-provider/context.js.map +7 -1
- package/build/components/async-mode-provider/index.js +40 -25
- package/build/components/async-mode-provider/index.js.map +7 -1
- package/build/components/async-mode-provider/use-async-mode.js +25 -16
- package/build/components/async-mode-provider/use-async-mode.js.map +7 -1
- package/build/components/registry-provider/context.js +46 -64
- package/build/components/registry-provider/context.js.map +7 -1
- package/build/components/registry-provider/index.js +40 -25
- package/build/components/registry-provider/index.js.map +7 -1
- package/build/components/registry-provider/use-registry.js +25 -55
- package/build/components/registry-provider/use-registry.js.map +7 -1
- package/build/components/use-dispatch/index.js +38 -17
- package/build/components/use-dispatch/index.js.map +7 -1
- package/build/components/use-dispatch/use-dispatch-with-map.js +59 -44
- package/build/components/use-dispatch/use-dispatch-with-map.js.map +7 -1
- package/build/components/use-dispatch/use-dispatch.js +36 -71
- package/build/components/use-dispatch/use-dispatch.js.map +7 -1
- package/build/components/use-select/index.js +79 -208
- package/build/components/use-select/index.js.map +7 -1
- package/build/components/with-dispatch/index.js +34 -107
- package/build/components/with-dispatch/index.js.map +7 -1
- package/build/components/with-registry/index.js +30 -30
- package/build/components/with-registry/index.js.map +7 -1
- package/build/components/with-select/index.js +44 -67
- package/build/components/with-select/index.js.map +7 -1
- package/build/controls.js +49 -110
- package/build/controls.js.map +7 -1
- package/build/create-selector.js +36 -11
- package/build/create-selector.js.map +7 -1
- package/build/default-registry.js +24 -10
- package/build/default-registry.js.map +7 -1
- package/build/dispatch.js +38 -31
- package/build/dispatch.js.map +7 -1
- package/build/factory.js +29 -85
- package/build/factory.js.map +7 -1
- package/build/index.js +114 -301
- package/build/index.js.map +7 -1
- package/build/lock-unlock.js +31 -14
- package/build/lock-unlock.js.map +7 -1
- package/build/plugins/index.js +36 -11
- package/build/plugins/index.js.map +7 -1
- package/build/plugins/persistence/index.js +74 -139
- package/build/plugins/persistence/index.js.map +7 -1
- package/build/plugins/persistence/storage/default.js +37 -18
- package/build/plugins/persistence/storage/default.js.map +7 -1
- package/build/plugins/persistence/storage/object.js +24 -7
- package/build/plugins/persistence/storage/object.js.map +7 -1
- package/build/promise-middleware.js +37 -20
- package/build/promise-middleware.js.map +7 -1
- package/build/redux-store/combine-reducers.js +26 -5
- package/build/redux-store/combine-reducers.js.map +7 -1
- package/build/redux-store/index.js +221 -331
- package/build/redux-store/index.js.map +7 -1
- package/build/redux-store/metadata/actions.js +51 -112
- package/build/redux-store/metadata/actions.js.map +7 -1
- package/build/redux-store/metadata/equivalent-key-map.d.js +1 -1
- package/build/redux-store/metadata/equivalent-key-map.d.js.map +7 -1
- package/build/redux-store/metadata/reducer.js +111 -115
- package/build/redux-store/metadata/reducer.js.map +7 -1
- package/build/redux-store/metadata/selectors.js +109 -181
- package/build/redux-store/metadata/selectors.js.map +7 -1
- package/build/redux-store/metadata/utils.js +32 -35
- package/build/redux-store/metadata/utils.js.map +7 -1
- package/build/redux-store/thunk-middleware.js +24 -7
- package/build/redux-store/thunk-middleware.js.map +7 -1
- package/build/registry.js +94 -198
- package/build/registry.js.map +7 -1
- package/build/resolvers-cache-middleware.js +26 -27
- package/build/resolvers-cache-middleware.js.map +7 -1
- package/build/select.js +38 -31
- package/build/select.js.map +7 -1
- package/build/store/index.js +52 -16
- package/build/store/index.js.map +7 -1
- package/build/types.js +16 -5
- package/build/types.js.map +7 -1
- package/build/utils/emitter.js +33 -16
- package/build/utils/emitter.js.map +7 -1
- package/build-module/components/async-mode-provider/context.js +12 -49
- package/build-module/components/async-mode-provider/context.js.map +7 -1
- package/build-module/components/async-mode-provider/index.js +8 -3
- package/build-module/components/async-mode-provider/index.js.map +7 -1
- package/build-module/components/async-mode-provider/use-async-mode.js +7 -11
- package/build-module/components/async-mode-provider/use-async-mode.js.map +7 -1
- package/build-module/components/registry-provider/context.js +13 -58
- package/build-module/components/registry-provider/context.js.map +7 -1
- package/build-module/components/registry-provider/index.js +8 -3
- package/build-module/components/registry-provider/index.js.map +7 -1
- package/build-module/components/registry-provider/use-registry.js +7 -51
- package/build-module/components/registry-provider/use-registry.js.map +7 -1
- package/build-module/components/use-dispatch/index.js +7 -3
- package/build-module/components/use-dispatch/index.js.map +7 -1
- package/build-module/components/use-dispatch/use-dispatch-with-map.js +27 -35
- package/build-module/components/use-dispatch/use-dispatch-with-map.js.map +7 -1
- package/build-module/components/use-dispatch/use-dispatch.js +8 -66
- package/build-module/components/use-dispatch/use-dispatch.js.map +7 -1
- package/build-module/components/use-select/index.js +45 -193
- package/build-module/components/use-select/index.js.map +7 -1
- package/build-module/components/with-dispatch/index.js +16 -104
- package/build-module/components/with-dispatch/index.js.map +7 -1
- package/build-module/components/with-registry/index.js +12 -27
- package/build-module/components/with-registry/index.js.map +7 -1
- package/build-module/components/with-select/index.js +16 -63
- package/build-module/components/with-select/index.js.map +7 -1
- package/build-module/controls.js +25 -105
- package/build-module/controls.js.map +7 -1
- package/build-module/create-selector.js +5 -8
- package/build-module/create-selector.js.map +7 -1
- package/build-module/default-registry.js +6 -6
- package/build-module/default-registry.js.map +7 -1
- package/build-module/dispatch.js +6 -27
- package/build-module/dispatch.js.map +7 -1
- package/build-module/factory.js +8 -82
- package/build-module/factory.js.map +7 -1
- package/build-module/index.js +62 -185
- package/build-module/index.js.map +7 -1
- package/build-module/lock-unlock.js +8 -7
- package/build-module/lock-unlock.js.map +7 -1
- package/build-module/plugins/index.js +5 -2
- package/build-module/plugins/index.js.map +7 -1
- package/build-module/plugins/persistence/index.js +38 -129
- package/build-module/plugins/persistence/index.js.map +7 -1
- package/build-module/plugins/persistence/storage/default.js +8 -11
- package/build-module/plugins/persistence/storage/default.js.map +7 -1
- package/build-module/plugins/persistence/storage/object.js +6 -3
- package/build-module/plugins/persistence/storage/object.js.map +7 -1
- package/build-module/promise-middleware.js +8 -14
- package/build-module/promise-middleware.js.map +7 -1
- package/build-module/redux-store/combine-reducers.js +5 -2
- package/build-module/redux-store/combine-reducers.js.map +7 -1
- package/build-module/redux-store/index.js +180 -310
- package/build-module/redux-store/index.js.map +7 -1
- package/build-module/redux-store/metadata/actions.js +30 -109
- package/build-module/redux-store/metadata/actions.js.map +7 -1
- package/build-module/redux-store/metadata/equivalent-key-map.d.js +1 -2
- package/build-module/redux-store/metadata/equivalent-key-map.d.js.map +7 -1
- package/build-module/redux-store/metadata/reducer.js +83 -108
- package/build-module/redux-store/metadata/reducer.js.map +7 -1
- package/build-module/redux-store/metadata/selectors.js +76 -175
- package/build-module/redux-store/metadata/selectors.js.map +7 -1
- package/build-module/redux-store/metadata/utils.js +10 -30
- package/build-module/redux-store/metadata/utils.js.map +7 -1
- package/build-module/redux-store/thunk-middleware.js +7 -4
- package/build-module/redux-store/thunk-middleware.js.map +7 -1
- package/build-module/registry.js +56 -187
- package/build-module/registry.js.map +7 -1
- package/build-module/resolvers-cache-middleware.js +8 -23
- package/build-module/resolvers-cache-middleware.js.map +7 -1
- package/build-module/select.js +6 -27
- package/build-module/select.js.map +7 -1
- package/build-module/store/index.js +34 -12
- package/build-module/store/index.js.map +7 -1
- package/build-module/types.js +1 -2
- package/build-module/types.js.map +7 -1
- package/build-module/utils/emitter.js +12 -13
- package/build-module/utils/emitter.js.map +7 -1
- package/package.json +17 -10
|
@@ -1,63 +1,31 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import {
|
|
5
|
-
import
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
import
|
|
11
|
-
import
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
*/
|
|
16
|
-
import { combineReducers } from './combine-reducers';
|
|
17
|
-
import { builtinControls } from '../controls';
|
|
18
|
-
import { lock } from '../lock-unlock';
|
|
19
|
-
import promise from '../promise-middleware';
|
|
20
|
-
import createResolversCacheMiddleware from '../resolvers-cache-middleware';
|
|
21
|
-
import createThunkMiddleware from './thunk-middleware';
|
|
22
|
-
import metadataReducer from './metadata/reducer';
|
|
23
|
-
import * as metadataSelectors from './metadata/selectors';
|
|
24
|
-
import * as metadataActions from './metadata/actions';
|
|
25
|
-
export { combineReducers };
|
|
26
|
-
|
|
27
|
-
/** @typedef {import('../types').DataRegistry} DataRegistry */
|
|
28
|
-
/** @typedef {import('../types').ListenerFunction} ListenerFunction */
|
|
29
|
-
/**
|
|
30
|
-
* @typedef {import('../types').StoreDescriptor<C>} StoreDescriptor
|
|
31
|
-
* @template {import('../types').AnyConfig} C
|
|
32
|
-
*/
|
|
33
|
-
/**
|
|
34
|
-
* @typedef {import('../types').ReduxStoreConfig<State,Actions,Selectors>} ReduxStoreConfig
|
|
35
|
-
* @template State
|
|
36
|
-
* @template {Record<string,import('../types').ActionCreator>} Actions
|
|
37
|
-
* @template Selectors
|
|
38
|
-
*/
|
|
39
|
-
|
|
40
|
-
const trimUndefinedValues = array => {
|
|
1
|
+
import { createStore, applyMiddleware } from "redux";
|
|
2
|
+
import EquivalentKeyMap from "equivalent-key-map";
|
|
3
|
+
import createReduxRoutineMiddleware from "@wordpress/redux-routine";
|
|
4
|
+
import { compose } from "@wordpress/compose";
|
|
5
|
+
import { combineReducers } from "./combine-reducers";
|
|
6
|
+
import { builtinControls } from "../controls";
|
|
7
|
+
import { lock } from "../lock-unlock";
|
|
8
|
+
import promise from "../promise-middleware";
|
|
9
|
+
import createResolversCacheMiddleware from "../resolvers-cache-middleware";
|
|
10
|
+
import createThunkMiddleware from "./thunk-middleware";
|
|
11
|
+
import metadataReducer from "./metadata/reducer";
|
|
12
|
+
import * as metadataSelectors from "./metadata/selectors";
|
|
13
|
+
import * as metadataActions from "./metadata/actions";
|
|
14
|
+
const trimUndefinedValues = (array) => {
|
|
41
15
|
const result = [...array];
|
|
42
16
|
for (let i = result.length - 1; i >= 0; i--) {
|
|
43
|
-
if (result[i] ===
|
|
17
|
+
if (result[i] === void 0) {
|
|
44
18
|
result.splice(i, 1);
|
|
45
19
|
}
|
|
46
20
|
}
|
|
47
21
|
return result;
|
|
48
22
|
};
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
* @param {Function} callback The function to transform each object value.
|
|
56
|
-
* @return {Array} Transformed object.
|
|
57
|
-
*/
|
|
58
|
-
const mapValues = (obj, callback) => Object.fromEntries(Object.entries(obj !== null && obj !== void 0 ? obj : {}).map(([key, value]) => [key, callback(value, key)]));
|
|
59
|
-
|
|
60
|
-
// Convert non serializable types to plain objects
|
|
23
|
+
const mapValues = (obj, callback) => Object.fromEntries(
|
|
24
|
+
Object.entries(obj ?? {}).map(([key, value]) => [
|
|
25
|
+
key,
|
|
26
|
+
callback(value, key)
|
|
27
|
+
])
|
|
28
|
+
);
|
|
61
29
|
const devToolsReplacer = (key, state) => {
|
|
62
30
|
if (state instanceof Map) {
|
|
63
31
|
return Object.fromEntries(state);
|
|
@@ -67,12 +35,6 @@ const devToolsReplacer = (key, state) => {
|
|
|
67
35
|
}
|
|
68
36
|
return state;
|
|
69
37
|
};
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Create a cache to track whether resolvers started running or not.
|
|
73
|
-
*
|
|
74
|
-
* @return {Object} Resolvers Cache.
|
|
75
|
-
*/
|
|
76
38
|
function createResolversCache() {
|
|
77
39
|
const cache = {};
|
|
78
40
|
return {
|
|
@@ -93,7 +55,7 @@ function createResolversCache() {
|
|
|
93
55
|
};
|
|
94
56
|
}
|
|
95
57
|
function createBindingCache(getItem, bindItem) {
|
|
96
|
-
const cache = new WeakMap();
|
|
58
|
+
const cache = /* @__PURE__ */ new WeakMap();
|
|
97
59
|
return {
|
|
98
60
|
get(itemName) {
|
|
99
61
|
const item = getItem(itemName);
|
|
@@ -114,67 +76,24 @@ function createPrivateProxy(publicItems, privateItems) {
|
|
|
114
76
|
get: (target, itemName) => privateItems.get(itemName) || Reflect.get(target, itemName)
|
|
115
77
|
});
|
|
116
78
|
}
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* Creates a data store descriptor for the provided Redux store configuration containing
|
|
120
|
-
* properties describing reducer, actions, selectors, controls and resolvers.
|
|
121
|
-
*
|
|
122
|
-
* @example
|
|
123
|
-
* ```js
|
|
124
|
-
* import { createReduxStore } from '@wordpress/data';
|
|
125
|
-
*
|
|
126
|
-
* const store = createReduxStore( 'demo', {
|
|
127
|
-
* reducer: ( state = 'OK' ) => state,
|
|
128
|
-
* selectors: {
|
|
129
|
-
* getValue: ( state ) => state,
|
|
130
|
-
* },
|
|
131
|
-
* } );
|
|
132
|
-
* ```
|
|
133
|
-
*
|
|
134
|
-
* @template State
|
|
135
|
-
* @template {Record<string,import('../types').ActionCreator>} Actions
|
|
136
|
-
* @template Selectors
|
|
137
|
-
* @param {string} key Unique namespace identifier.
|
|
138
|
-
* @param {ReduxStoreConfig<State,Actions,Selectors>} options Registered store options, with properties
|
|
139
|
-
* describing reducer, actions, selectors,
|
|
140
|
-
* and resolvers.
|
|
141
|
-
*
|
|
142
|
-
* @return {StoreDescriptor<ReduxStoreConfig<State,Actions,Selectors>>} Store Object.
|
|
143
|
-
*/
|
|
144
|
-
export default function createReduxStore(key, options) {
|
|
79
|
+
function createReduxStore(key, options) {
|
|
145
80
|
const privateActions = {};
|
|
146
81
|
const privateSelectors = {};
|
|
147
82
|
const privateRegistrationFunctions = {
|
|
148
83
|
privateActions,
|
|
149
|
-
registerPrivateActions: actions => {
|
|
84
|
+
registerPrivateActions: (actions) => {
|
|
150
85
|
Object.assign(privateActions, actions);
|
|
151
86
|
},
|
|
152
87
|
privateSelectors,
|
|
153
|
-
registerPrivateSelectors: selectors => {
|
|
88
|
+
registerPrivateSelectors: (selectors) => {
|
|
154
89
|
Object.assign(privateSelectors, selectors);
|
|
155
90
|
}
|
|
156
91
|
};
|
|
157
92
|
const storeDescriptor = {
|
|
158
93
|
name: key,
|
|
159
|
-
instantiate: registry => {
|
|
160
|
-
|
|
161
|
-
* Stores listener functions registered with `subscribe()`.
|
|
162
|
-
*
|
|
163
|
-
* When functions register to listen to store changes with
|
|
164
|
-
* `subscribe()` they get added here. Although Redux offers
|
|
165
|
-
* its own `subscribe()` function directly, by wrapping the
|
|
166
|
-
* subscription in this store instance it's possible to
|
|
167
|
-
* optimize checking if the state has changed before calling
|
|
168
|
-
* each listener.
|
|
169
|
-
*
|
|
170
|
-
* @type {Set<ListenerFunction>}
|
|
171
|
-
*/
|
|
172
|
-
const listeners = new Set();
|
|
94
|
+
instantiate: (registry) => {
|
|
95
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
173
96
|
const reducer = options.reducer;
|
|
174
|
-
|
|
175
|
-
// Object that every thunk function receives as the first argument. It contains the
|
|
176
|
-
// `registry`, `dispatch`, `select` and `resolveSelect` fields. Some of them are
|
|
177
|
-
// constructed as getters to avoid circular dependencies.
|
|
178
97
|
const thunkArgs = {
|
|
179
98
|
registry,
|
|
180
99
|
get dispatch() {
|
|
@@ -187,52 +106,34 @@ export default function createReduxStore(key, options) {
|
|
|
187
106
|
return resolveSelectors;
|
|
188
107
|
}
|
|
189
108
|
};
|
|
190
|
-
const store = instantiateReduxStore(
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
109
|
+
const store = instantiateReduxStore(
|
|
110
|
+
key,
|
|
111
|
+
options,
|
|
112
|
+
registry,
|
|
113
|
+
thunkArgs
|
|
114
|
+
);
|
|
194
115
|
lock(store, privateRegistrationFunctions);
|
|
195
116
|
const resolversCache = createResolversCache();
|
|
196
|
-
|
|
197
|
-
// Binds an action creator (`action`) to the `store`, making it a callable function.
|
|
198
|
-
// These are the functions that are returned by `useDispatch`, for example.
|
|
199
|
-
// It always returns a `Promise`, although actions are not always async. That's an
|
|
200
|
-
// unfortunate backward compatibility measure.
|
|
201
117
|
function bindAction(action) {
|
|
202
118
|
return (...args) => Promise.resolve(store.dispatch(action(...args)));
|
|
203
119
|
}
|
|
204
|
-
|
|
205
|
-
/*
|
|
206
|
-
* Object with all public actions, both metadata and store actions.
|
|
207
|
-
*/
|
|
208
120
|
const actions = {
|
|
209
121
|
...mapValues(metadataActions, bindAction),
|
|
210
122
|
...mapValues(options.actions, bindAction)
|
|
211
123
|
};
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
const thunkDispatch = new Proxy(action => store.dispatch(action), {
|
|
224
|
-
get: (target, name) => allActions[name]
|
|
225
|
-
});
|
|
226
|
-
|
|
227
|
-
// To the public `actions` object, add the "locked" `allActions` object. When used,
|
|
228
|
-
// `unlock( actions )` will return `allActions`, implementing a way how to get at the private actions.
|
|
124
|
+
const allActions = createPrivateProxy(
|
|
125
|
+
actions,
|
|
126
|
+
createBindingCache(
|
|
127
|
+
(name) => privateActions[name],
|
|
128
|
+
bindAction
|
|
129
|
+
)
|
|
130
|
+
);
|
|
131
|
+
const thunkDispatch = new Proxy(
|
|
132
|
+
(action) => store.dispatch(action),
|
|
133
|
+
{ get: (target, name) => allActions[name] }
|
|
134
|
+
);
|
|
229
135
|
lock(actions, allActions);
|
|
230
|
-
|
|
231
|
-
// If we have selector resolvers, convert them to a normalized form.
|
|
232
136
|
const resolvers = options.resolvers ? mapValues(options.resolvers, mapResolver) : {};
|
|
233
|
-
|
|
234
|
-
// Bind a selector to the store. Call the selector with the current state, correct registry,
|
|
235
|
-
// and if there is a resolver, attach the resolver logic to the selector.
|
|
236
137
|
function bindSelector(selector, selectorName) {
|
|
237
138
|
if (selector.isRegistrySelector) {
|
|
238
139
|
selector.registry = registry;
|
|
@@ -240,114 +141,113 @@ export default function createReduxStore(key, options) {
|
|
|
240
141
|
const boundSelector = (...args) => {
|
|
241
142
|
args = normalize(selector, args);
|
|
242
143
|
const state = store.__unstableOriginalGetState();
|
|
243
|
-
// Before calling the selector, switch to the correct registry.
|
|
244
144
|
if (selector.isRegistrySelector) {
|
|
245
145
|
selector.registry = registry;
|
|
246
146
|
}
|
|
247
147
|
return selector(state.root, ...args);
|
|
248
148
|
};
|
|
249
|
-
|
|
250
|
-
// Expose normalization method on the bound selector
|
|
251
|
-
// in order that it can be called when fulfilling
|
|
252
|
-
// the resolver.
|
|
253
149
|
boundSelector.__unstableNormalizeArgs = selector.__unstableNormalizeArgs;
|
|
254
150
|
const resolver = resolvers[selectorName];
|
|
255
151
|
if (!resolver) {
|
|
256
152
|
boundSelector.hasResolver = false;
|
|
257
153
|
return boundSelector;
|
|
258
154
|
}
|
|
259
|
-
return mapSelectorWithResolver(
|
|
155
|
+
return mapSelectorWithResolver(
|
|
156
|
+
boundSelector,
|
|
157
|
+
selectorName,
|
|
158
|
+
resolver,
|
|
159
|
+
store,
|
|
160
|
+
resolversCache,
|
|
161
|
+
boundMetadataSelectors
|
|
162
|
+
);
|
|
260
163
|
}
|
|
261
|
-
|
|
262
|
-
// Metadata selectors are bound differently: different state (`state.metadata`), no resolvers,
|
|
263
|
-
// normalization depending on the target selector.
|
|
264
164
|
function bindMetadataSelector(metaDataSelector) {
|
|
265
165
|
const boundSelector = (selectorName, selectorArgs, ...args) => {
|
|
266
|
-
// Normalize the arguments passed to the target selector.
|
|
267
166
|
if (selectorName) {
|
|
268
167
|
const targetSelector = options.selectors?.[selectorName];
|
|
269
168
|
if (targetSelector) {
|
|
270
|
-
selectorArgs = normalize(
|
|
169
|
+
selectorArgs = normalize(
|
|
170
|
+
targetSelector,
|
|
171
|
+
selectorArgs
|
|
172
|
+
);
|
|
271
173
|
}
|
|
272
174
|
}
|
|
273
175
|
const state = store.__unstableOriginalGetState();
|
|
274
|
-
return metaDataSelector(
|
|
176
|
+
return metaDataSelector(
|
|
177
|
+
state.metadata,
|
|
178
|
+
selectorName,
|
|
179
|
+
selectorArgs,
|
|
180
|
+
...args
|
|
181
|
+
);
|
|
275
182
|
};
|
|
276
183
|
boundSelector.hasResolver = false;
|
|
277
184
|
return boundSelector;
|
|
278
185
|
}
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
186
|
+
const boundMetadataSelectors = mapValues(
|
|
187
|
+
metadataSelectors,
|
|
188
|
+
bindMetadataSelector
|
|
189
|
+
);
|
|
283
190
|
const boundSelectors = mapValues(options.selectors, bindSelector);
|
|
284
191
|
const selectors = {
|
|
285
192
|
...boundMetadataSelectors,
|
|
286
193
|
...boundSelectors
|
|
287
194
|
};
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
const
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
// instantiation, so that registry selectors are bound to the registry.
|
|
195
|
+
const boundPrivateSelectors = createBindingCache(
|
|
196
|
+
(name) => privateSelectors[name],
|
|
197
|
+
bindSelector
|
|
198
|
+
);
|
|
199
|
+
const allSelectors = createPrivateProxy(
|
|
200
|
+
selectors,
|
|
201
|
+
boundPrivateSelectors
|
|
202
|
+
);
|
|
297
203
|
for (const selectorName of Object.keys(privateSelectors)) {
|
|
298
204
|
boundPrivateSelectors.get(selectorName);
|
|
299
205
|
}
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
// `get` method, delegating to `allSelectors`.
|
|
305
|
-
const thunkSelect = new Proxy(selector => selector(store.__unstableOriginalGetState()), {
|
|
306
|
-
get: (target, name) => allSelectors[name]
|
|
307
|
-
});
|
|
308
|
-
|
|
309
|
-
// To the public `selectors` object, add the "locked" `allSelectors` object. When used,
|
|
310
|
-
// `unlock( selectors )` will return `allSelectors`, implementing a way how to get at the private selectors.
|
|
206
|
+
const thunkSelect = new Proxy(
|
|
207
|
+
(selector) => selector(store.__unstableOriginalGetState()),
|
|
208
|
+
{ get: (target, name) => allSelectors[name] }
|
|
209
|
+
);
|
|
311
210
|
lock(selectors, allSelectors);
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
const allResolveSelectors = createPrivateProxy(
|
|
321
|
-
|
|
322
|
-
|
|
211
|
+
const bindResolveSelector = mapResolveSelector(
|
|
212
|
+
store,
|
|
213
|
+
boundMetadataSelectors
|
|
214
|
+
);
|
|
215
|
+
const resolveSelectors = mapValues(
|
|
216
|
+
boundSelectors,
|
|
217
|
+
bindResolveSelector
|
|
218
|
+
);
|
|
219
|
+
const allResolveSelectors = createPrivateProxy(
|
|
220
|
+
resolveSelectors,
|
|
221
|
+
createBindingCache(
|
|
222
|
+
(name) => boundPrivateSelectors.get(name),
|
|
223
|
+
bindResolveSelector
|
|
224
|
+
)
|
|
225
|
+
);
|
|
323
226
|
lock(resolveSelectors, allResolveSelectors);
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
227
|
+
const bindSuspendSelector = mapSuspendSelector(
|
|
228
|
+
store,
|
|
229
|
+
boundMetadataSelectors
|
|
230
|
+
);
|
|
328
231
|
const suspendSelectors = {
|
|
329
232
|
...boundMetadataSelectors,
|
|
330
233
|
// no special suspense behavior
|
|
331
234
|
...mapValues(boundSelectors, bindSuspendSelector)
|
|
332
235
|
};
|
|
333
|
-
const allSuspendSelectors = createPrivateProxy(
|
|
334
|
-
|
|
335
|
-
|
|
236
|
+
const allSuspendSelectors = createPrivateProxy(
|
|
237
|
+
suspendSelectors,
|
|
238
|
+
createBindingCache(
|
|
239
|
+
(name) => boundPrivateSelectors.get(name),
|
|
240
|
+
bindSuspendSelector
|
|
241
|
+
)
|
|
242
|
+
);
|
|
336
243
|
lock(suspendSelectors, allSuspendSelectors);
|
|
337
244
|
const getSelectors = () => selectors;
|
|
338
245
|
const getActions = () => actions;
|
|
339
246
|
const getResolveSelectors = () => resolveSelectors;
|
|
340
247
|
const getSuspendSelectors = () => suspendSelectors;
|
|
341
|
-
|
|
342
|
-
// We have some modules monkey-patching the store object
|
|
343
|
-
// It's wrong to do so but until we refactor all of our effects to controls
|
|
344
|
-
// We need to keep the same "store" instance here.
|
|
345
248
|
store.__unstableOriginalGetState = store.getState;
|
|
346
249
|
store.getState = () => store.__unstableOriginalGetState().root;
|
|
347
|
-
|
|
348
|
-
// Customize subscribe behavior to call listeners only on effective change,
|
|
349
|
-
// not on every dispatch.
|
|
350
|
-
const subscribe = store && (listener => {
|
|
250
|
+
const subscribe = store && ((listener) => {
|
|
351
251
|
listeners.add(listener);
|
|
352
252
|
return () => listeners.delete(listener);
|
|
353
253
|
});
|
|
@@ -362,9 +262,6 @@ export default function createReduxStore(key, options) {
|
|
|
362
262
|
}
|
|
363
263
|
}
|
|
364
264
|
});
|
|
365
|
-
|
|
366
|
-
// This can be simplified to just { subscribe, getSelectors, getActions }
|
|
367
|
-
// Once we remove the use function.
|
|
368
265
|
return {
|
|
369
266
|
reducer,
|
|
370
267
|
store,
|
|
@@ -379,86 +276,75 @@ export default function createReduxStore(key, options) {
|
|
|
379
276
|
};
|
|
380
277
|
}
|
|
381
278
|
};
|
|
382
|
-
|
|
383
|
-
// Expose the private registration functions on the store
|
|
384
|
-
// descriptor. That's a natural choice since that's where the
|
|
385
|
-
// public actions and selectors are stored.
|
|
386
279
|
lock(storeDescriptor, privateRegistrationFunctions);
|
|
387
280
|
return storeDescriptor;
|
|
388
281
|
}
|
|
389
|
-
|
|
390
|
-
/**
|
|
391
|
-
* Creates a redux store for a namespace.
|
|
392
|
-
*
|
|
393
|
-
* @param {string} key Unique namespace identifier.
|
|
394
|
-
* @param {Object} options Registered store options, with properties
|
|
395
|
-
* describing reducer, actions, selectors,
|
|
396
|
-
* and resolvers.
|
|
397
|
-
* @param {DataRegistry} registry Registry reference.
|
|
398
|
-
* @param {Object} thunkArgs Argument object for the thunk middleware.
|
|
399
|
-
* @return {Object} Newly created redux store.
|
|
400
|
-
*/
|
|
401
282
|
function instantiateReduxStore(key, options, registry, thunkArgs) {
|
|
402
283
|
const controls = {
|
|
403
284
|
...options.controls,
|
|
404
285
|
...builtinControls
|
|
405
286
|
};
|
|
406
|
-
const normalizedControls = mapValues(
|
|
407
|
-
|
|
287
|
+
const normalizedControls = mapValues(
|
|
288
|
+
controls,
|
|
289
|
+
(control) => control.isRegistryControl ? control(registry) : control
|
|
290
|
+
);
|
|
291
|
+
const middlewares = [
|
|
292
|
+
createResolversCacheMiddleware(registry, key),
|
|
293
|
+
promise,
|
|
294
|
+
createReduxRoutineMiddleware(normalizedControls),
|
|
295
|
+
createThunkMiddleware(thunkArgs)
|
|
296
|
+
];
|
|
408
297
|
const enhancers = [applyMiddleware(...middlewares)];
|
|
409
|
-
if (typeof window !==
|
|
410
|
-
enhancers.push(
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
298
|
+
if (typeof window !== "undefined" && window.__REDUX_DEVTOOLS_EXTENSION__) {
|
|
299
|
+
enhancers.push(
|
|
300
|
+
window.__REDUX_DEVTOOLS_EXTENSION__({
|
|
301
|
+
name: key,
|
|
302
|
+
instanceId: key,
|
|
303
|
+
serialize: {
|
|
304
|
+
replacer: devToolsReplacer
|
|
305
|
+
}
|
|
306
|
+
})
|
|
307
|
+
);
|
|
417
308
|
}
|
|
418
|
-
const {
|
|
419
|
-
reducer,
|
|
420
|
-
initialState
|
|
421
|
-
} = options;
|
|
309
|
+
const { reducer, initialState } = options;
|
|
422
310
|
const enhancedReducer = combineReducers({
|
|
423
311
|
metadata: metadataReducer,
|
|
424
312
|
root: reducer
|
|
425
313
|
});
|
|
426
|
-
return createStore(
|
|
427
|
-
|
|
428
|
-
|
|
314
|
+
return createStore(
|
|
315
|
+
enhancedReducer,
|
|
316
|
+
{ root: initialState },
|
|
317
|
+
compose(enhancers)
|
|
318
|
+
);
|
|
429
319
|
}
|
|
430
|
-
|
|
431
|
-
/**
|
|
432
|
-
* Maps selectors to functions that return a resolution promise for them.
|
|
433
|
-
*
|
|
434
|
-
* @param {Object} store The redux store the selectors are bound to.
|
|
435
|
-
* @param {Object} boundMetadataSelectors The bound metadata selectors.
|
|
436
|
-
*
|
|
437
|
-
* @return {Function} Function that maps selectors to resolvers.
|
|
438
|
-
*/
|
|
439
320
|
function mapResolveSelector(store, boundMetadataSelectors) {
|
|
440
321
|
return (selector, selectorName) => {
|
|
441
|
-
// If the selector doesn't have a resolver, just convert the return value
|
|
442
|
-
// (including exceptions) to a Promise, no additional extra behavior is needed.
|
|
443
322
|
if (!selector.hasResolver) {
|
|
444
323
|
return async (...args) => selector.apply(null, args);
|
|
445
324
|
}
|
|
446
325
|
return (...args) => new Promise((resolve, reject) => {
|
|
447
326
|
const hasFinished = () => {
|
|
448
|
-
return boundMetadataSelectors.hasFinishedResolution(
|
|
327
|
+
return boundMetadataSelectors.hasFinishedResolution(
|
|
328
|
+
selectorName,
|
|
329
|
+
args
|
|
330
|
+
);
|
|
449
331
|
};
|
|
450
|
-
const finalize =
|
|
451
|
-
const hasFailed = boundMetadataSelectors.hasResolutionFailed(
|
|
332
|
+
const finalize = (result2) => {
|
|
333
|
+
const hasFailed = boundMetadataSelectors.hasResolutionFailed(
|
|
334
|
+
selectorName,
|
|
335
|
+
args
|
|
336
|
+
);
|
|
452
337
|
if (hasFailed) {
|
|
453
|
-
const error = boundMetadataSelectors.getResolutionError(
|
|
338
|
+
const error = boundMetadataSelectors.getResolutionError(
|
|
339
|
+
selectorName,
|
|
340
|
+
args
|
|
341
|
+
);
|
|
454
342
|
reject(error);
|
|
455
343
|
} else {
|
|
456
|
-
resolve(
|
|
344
|
+
resolve(result2);
|
|
457
345
|
}
|
|
458
346
|
};
|
|
459
347
|
const getResult = () => selector.apply(null, args);
|
|
460
|
-
|
|
461
|
-
// Trigger the selector (to trigger the resolver)
|
|
462
348
|
const result = getResult();
|
|
463
349
|
if (hasFinished()) {
|
|
464
350
|
return finalize(result);
|
|
@@ -472,32 +358,34 @@ function mapResolveSelector(store, boundMetadataSelectors) {
|
|
|
472
358
|
});
|
|
473
359
|
};
|
|
474
360
|
}
|
|
475
|
-
|
|
476
|
-
/**
|
|
477
|
-
* Maps selectors to functions that throw a suspense promise if not yet resolved.
|
|
478
|
-
*
|
|
479
|
-
* @param {Object} store The redux store the selectors select from.
|
|
480
|
-
* @param {Object} boundMetadataSelectors The bound metadata selectors.
|
|
481
|
-
*
|
|
482
|
-
* @return {Function} Function that maps selectors to their suspending versions.
|
|
483
|
-
*/
|
|
484
361
|
function mapSuspendSelector(store, boundMetadataSelectors) {
|
|
485
362
|
return (selector, selectorName) => {
|
|
486
|
-
// Selector without a resolver doesn't have any extra suspense behavior.
|
|
487
363
|
if (!selector.hasResolver) {
|
|
488
364
|
return selector;
|
|
489
365
|
}
|
|
490
366
|
return (...args) => {
|
|
491
367
|
const result = selector.apply(null, args);
|
|
492
|
-
if (boundMetadataSelectors.hasFinishedResolution(
|
|
493
|
-
|
|
494
|
-
|
|
368
|
+
if (boundMetadataSelectors.hasFinishedResolution(
|
|
369
|
+
selectorName,
|
|
370
|
+
args
|
|
371
|
+
)) {
|
|
372
|
+
if (boundMetadataSelectors.hasResolutionFailed(
|
|
373
|
+
selectorName,
|
|
374
|
+
args
|
|
375
|
+
)) {
|
|
376
|
+
throw boundMetadataSelectors.getResolutionError(
|
|
377
|
+
selectorName,
|
|
378
|
+
args
|
|
379
|
+
);
|
|
495
380
|
}
|
|
496
381
|
return result;
|
|
497
382
|
}
|
|
498
|
-
throw new Promise(resolve => {
|
|
383
|
+
throw new Promise((resolve) => {
|
|
499
384
|
const unsubscribe = store.subscribe(() => {
|
|
500
|
-
if (boundMetadataSelectors.hasFinishedResolution(
|
|
385
|
+
if (boundMetadataSelectors.hasFinishedResolution(
|
|
386
|
+
selectorName,
|
|
387
|
+
args
|
|
388
|
+
)) {
|
|
501
389
|
resolve();
|
|
502
390
|
unsubscribe();
|
|
503
391
|
}
|
|
@@ -506,13 +394,6 @@ function mapSuspendSelector(store, boundMetadataSelectors) {
|
|
|
506
394
|
};
|
|
507
395
|
};
|
|
508
396
|
}
|
|
509
|
-
|
|
510
|
-
/**
|
|
511
|
-
* Convert a resolver to a normalized form, an object with `fulfill` method and
|
|
512
|
-
* optional methods like `isFulfilled`.
|
|
513
|
-
*
|
|
514
|
-
* @param {Function} resolver Resolver to convert
|
|
515
|
-
*/
|
|
516
397
|
function mapResolver(resolver) {
|
|
517
398
|
if (resolver.fulfill) {
|
|
518
399
|
return resolver;
|
|
@@ -520,26 +401,14 @@ function mapResolver(resolver) {
|
|
|
520
401
|
return {
|
|
521
402
|
...resolver,
|
|
522
403
|
// Copy the enumerable properties of the resolver function.
|
|
523
|
-
fulfill: resolver
|
|
404
|
+
fulfill: resolver
|
|
405
|
+
// Add the fulfill method.
|
|
524
406
|
};
|
|
525
407
|
}
|
|
526
|
-
|
|
527
|
-
/**
|
|
528
|
-
* Returns a selector with a matched resolver.
|
|
529
|
-
* Resolvers are side effects invoked once per argument set of a given selector call,
|
|
530
|
-
* used in ensuring that the data needs for the selector are satisfied.
|
|
531
|
-
*
|
|
532
|
-
* @param {Object} selector The selector function to be bound.
|
|
533
|
-
* @param {string} selectorName The selector name.
|
|
534
|
-
* @param {Object} resolver Resolver to call.
|
|
535
|
-
* @param {Object} store The redux store to which the resolvers should be mapped.
|
|
536
|
-
* @param {Object} resolversCache Resolvers Cache.
|
|
537
|
-
* @param {Object} boundMetadataSelectors The bound metadata selectors.
|
|
538
|
-
*/
|
|
539
408
|
function mapSelectorWithResolver(selector, selectorName, resolver, store, resolversCache, boundMetadataSelectors) {
|
|
540
409
|
function fulfillSelector(args) {
|
|
541
410
|
const state = store.getState();
|
|
542
|
-
if (resolversCache.isRunning(selectorName, args) || typeof resolver.isFulfilled ===
|
|
411
|
+
if (resolversCache.isRunning(selectorName, args) || typeof resolver.isFulfilled === "function" && resolver.isFulfilled(state, ...args)) {
|
|
543
412
|
return;
|
|
544
413
|
}
|
|
545
414
|
if (boundMetadataSelectors.hasStartedResolution(selectorName, args)) {
|
|
@@ -548,15 +417,21 @@ function mapSelectorWithResolver(selector, selectorName, resolver, store, resolv
|
|
|
548
417
|
resolversCache.markAsRunning(selectorName, args);
|
|
549
418
|
setTimeout(async () => {
|
|
550
419
|
resolversCache.clear(selectorName, args);
|
|
551
|
-
store.dispatch(
|
|
420
|
+
store.dispatch(
|
|
421
|
+
metadataActions.startResolution(selectorName, args)
|
|
422
|
+
);
|
|
552
423
|
try {
|
|
553
424
|
const action = resolver.fulfill(...args);
|
|
554
425
|
if (action) {
|
|
555
426
|
await store.dispatch(action);
|
|
556
427
|
}
|
|
557
|
-
store.dispatch(
|
|
428
|
+
store.dispatch(
|
|
429
|
+
metadataActions.finishResolution(selectorName, args)
|
|
430
|
+
);
|
|
558
431
|
} catch (error) {
|
|
559
|
-
store.dispatch(
|
|
432
|
+
store.dispatch(
|
|
433
|
+
metadataActions.failResolution(selectorName, args, error)
|
|
434
|
+
);
|
|
560
435
|
}
|
|
561
436
|
}, 0);
|
|
562
437
|
}
|
|
@@ -568,19 +443,14 @@ function mapSelectorWithResolver(selector, selectorName, resolver, store, resolv
|
|
|
568
443
|
selectorResolver.hasResolver = true;
|
|
569
444
|
return selectorResolver;
|
|
570
445
|
}
|
|
571
|
-
|
|
572
|
-
/**
|
|
573
|
-
* Applies selector's normalization function to the given arguments
|
|
574
|
-
* if it exists.
|
|
575
|
-
*
|
|
576
|
-
* @param {Object} selector The selector potentially with a normalization method property.
|
|
577
|
-
* @param {Array} args selector arguments to normalize.
|
|
578
|
-
* @return {Array} Potentially normalized arguments.
|
|
579
|
-
*/
|
|
580
446
|
function normalize(selector, args) {
|
|
581
|
-
if (selector.__unstableNormalizeArgs && typeof selector.__unstableNormalizeArgs ===
|
|
447
|
+
if (selector.__unstableNormalizeArgs && typeof selector.__unstableNormalizeArgs === "function" && args?.length) {
|
|
582
448
|
return selector.__unstableNormalizeArgs(args);
|
|
583
449
|
}
|
|
584
450
|
return args;
|
|
585
451
|
}
|
|
586
|
-
|
|
452
|
+
export {
|
|
453
|
+
combineReducers,
|
|
454
|
+
createReduxStore as default
|
|
455
|
+
};
|
|
456
|
+
//# sourceMappingURL=index.js.map
|