@wordpress/core-data 6.16.0 → 6.17.1
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 +2 -0
- package/build/actions.js +69 -134
- package/build/actions.js.map +1 -1
- package/build/batch/create-batch.js +0 -22
- package/build/batch/create-batch.js.map +1 -1
- package/build/batch/default-processor.js +3 -15
- package/build/batch/default-processor.js.map +1 -1
- package/build/batch/index.js +0 -3
- package/build/batch/index.js.map +1 -1
- package/build/entities.js +109 -36
- package/build/entities.js.map +1 -1
- package/build/entity-provider.js +34 -53
- package/build/entity-provider.js.map +1 -1
- package/build/entity-types/attachment.js.map +1 -1
- package/build/entity-types/base-entity-records.js +0 -3
- package/build/entity-types/base-entity-records.js.map +1 -1
- package/build/entity-types/comment.js.map +1 -1
- package/build/entity-types/helpers.js.map +1 -1
- package/build/entity-types/index.js.map +1 -1
- package/build/entity-types/menu-location.js.map +1 -1
- package/build/entity-types/nav-menu-item.js.map +1 -1
- package/build/entity-types/nav-menu.js.map +1 -1
- package/build/entity-types/page.js.map +1 -1
- package/build/entity-types/plugin.js.map +1 -1
- package/build/entity-types/post.js.map +1 -1
- package/build/entity-types/settings.js.map +1 -1
- package/build/entity-types/sidebar.js.map +1 -1
- package/build/entity-types/taxonomy.js.map +1 -1
- package/build/entity-types/theme.js.map +1 -1
- package/build/entity-types/type.js.map +1 -1
- package/build/entity-types/user.js.map +1 -1
- package/build/entity-types/widget-type.js.map +1 -1
- package/build/entity-types/widget.js.map +1 -1
- package/build/entity-types/wp-template-part.js.map +1 -1
- package/build/entity-types/wp-template.js.map +1 -1
- package/build/fetch/__experimental-fetch-link-suggestions.js +14 -20
- package/build/fetch/__experimental-fetch-link-suggestions.js.map +1 -1
- package/build/fetch/__experimental-fetch-url-data.js +4 -13
- package/build/fetch/__experimental-fetch-url-data.js.map +1 -1
- package/build/fetch/index.js +0 -3
- package/build/fetch/index.js.map +1 -1
- package/build/hooks/constants.js +4 -5
- package/build/hooks/constants.js.map +1 -1
- package/build/hooks/index.js +0 -5
- package/build/hooks/index.js.map +1 -1
- package/build/hooks/memoize.js +0 -3
- package/build/hooks/memoize.js.map +1 -1
- package/build/hooks/use-entity-record.js +0 -9
- package/build/hooks/use-entity-record.js.map +1 -1
- package/build/hooks/use-entity-records.js +2 -9
- package/build/hooks/use-entity-records.js.map +1 -1
- package/build/hooks/use-query-select.js +1 -13
- package/build/hooks/use-query-select.js.map +1 -1
- package/build/hooks/use-resource-permissions.js +0 -15
- package/build/hooks/use-resource-permissions.js.map +1 -1
- package/build/index.js +9 -41
- package/build/index.js.map +1 -1
- package/build/locks/actions.js +1 -6
- package/build/locks/actions.js.map +1 -1
- package/build/locks/engine.js +1 -9
- package/build/locks/engine.js.map +1 -1
- package/build/locks/reducer.js +8 -9
- package/build/locks/reducer.js.map +1 -1
- package/build/locks/selectors.js +8 -11
- package/build/locks/selectors.js.map +1 -1
- package/build/locks/utils.js +4 -19
- package/build/locks/utils.js.map +1 -1
- package/build/name.js +0 -1
- package/build/name.js.map +1 -1
- package/build/private-apis.js +1 -2
- package/build/private-apis.js.map +1 -1
- package/build/private-selectors.js +2 -5
- package/build/private-selectors.js.map +1 -1
- package/build/queried-data/actions.js +4 -6
- package/build/queried-data/actions.js.map +1 -1
- package/build/queried-data/get-query-parts.js +11 -24
- package/build/queried-data/get-query-parts.js.map +1 -1
- package/build/queried-data/index.js +0 -6
- package/build/queried-data/index.js.map +1 -1
- package/build/queried-data/reducer.js +33 -51
- package/build/queried-data/reducer.js.map +1 -1
- package/build/queried-data/selectors.js +4 -25
- package/build/queried-data/selectors.js.map +1 -1
- package/build/reducer.js +76 -128
- package/build/reducer.js.map +1 -1
- package/build/resolvers.js +91 -133
- package/build/resolvers.js.map +1 -1
- package/build/selectors.js +51 -145
- package/build/selectors.js.map +1 -1
- package/build/sync.js +19 -0
- package/build/sync.js.map +1 -0
- package/build/types.js.map +1 -1
- package/build/utils/conservative-map-item.js +3 -10
- package/build/utils/conservative-map-item.js.map +1 -1
- package/build/utils/forward-resolver.js +0 -2
- package/build/utils/forward-resolver.js.map +1 -1
- package/build/utils/get-normalized-comma-separable.js +0 -3
- package/build/utils/get-normalized-comma-separable.js.map +1 -1
- package/build/utils/if-matching-action.js +0 -3
- package/build/utils/if-matching-action.js.map +1 -1
- package/build/utils/index.js +0 -10
- package/build/utils/index.js.map +1 -1
- package/build/utils/is-raw-attribute.js +0 -1
- package/build/utils/is-raw-attribute.js.map +1 -1
- package/build/utils/on-sub-key.js +5 -9
- package/build/utils/on-sub-key.js.map +1 -1
- package/build/utils/replace-action.js +0 -2
- package/build/utils/replace-action.js.map +1 -1
- package/build/utils/set-nested-value.js +0 -4
- package/build/utils/set-nested-value.js.map +1 -1
- package/build/utils/with-weak-map-cache.js +3 -6
- package/build/utils/with-weak-map-cache.js.map +1 -1
- package/build-module/actions.js +72 -95
- package/build-module/actions.js.map +1 -1
- package/build-module/batch/create-batch.js +1 -20
- package/build-module/batch/create-batch.js.map +1 -1
- package/build-module/batch/default-processor.js +4 -13
- package/build-module/batch/default-processor.js.map +1 -1
- package/build-module/batch/index.js.map +1 -1
- package/build-module/entities.js +109 -25
- package/build-module/entities.js.map +1 -1
- package/build-module/entity-provider.js +35 -45
- package/build-module/entity-provider.js.map +1 -1
- package/build-module/entity-types/attachment.js.map +1 -1
- package/build-module/entity-types/base-entity-records.js +0 -2
- package/build-module/entity-types/base-entity-records.js.map +1 -1
- package/build-module/entity-types/comment.js.map +1 -1
- package/build-module/entity-types/helpers.js.map +1 -1
- package/build-module/entity-types/index.js.map +1 -1
- package/build-module/entity-types/menu-location.js.map +1 -1
- package/build-module/entity-types/nav-menu-item.js.map +1 -1
- package/build-module/entity-types/nav-menu.js.map +1 -1
- package/build-module/entity-types/page.js.map +1 -1
- package/build-module/entity-types/plugin.js.map +1 -1
- package/build-module/entity-types/post.js.map +1 -1
- package/build-module/entity-types/settings.js.map +1 -1
- package/build-module/entity-types/sidebar.js.map +1 -1
- package/build-module/entity-types/taxonomy.js.map +1 -1
- package/build-module/entity-types/theme.js.map +1 -1
- package/build-module/entity-types/type.js.map +1 -1
- package/build-module/entity-types/user.js.map +1 -1
- package/build-module/entity-types/widget-type.js.map +1 -1
- package/build-module/entity-types/widget.js.map +1 -1
- package/build-module/entity-types/wp-template-part.js.map +1 -1
- package/build-module/entity-types/wp-template.js.map +1 -1
- package/build-module/fetch/__experimental-fetch-link-suggestions.js +15 -15
- package/build-module/fetch/__experimental-fetch-link-suggestions.js.map +1 -1
- package/build-module/fetch/__experimental-fetch-url-data.js +5 -10
- package/build-module/fetch/__experimental-fetch-url-data.js.map +1 -1
- package/build-module/fetch/index.js.map +1 -1
- package/build-module/hooks/constants.js +3 -4
- package/build-module/hooks/constants.js.map +1 -1
- package/build-module/hooks/index.js.map +1 -1
- package/build-module/hooks/memoize.js +2 -1
- package/build-module/hooks/memoize.js.map +1 -1
- package/build-module/hooks/use-entity-record.js +1 -3
- package/build-module/hooks/use-entity-record.js.map +1 -1
- package/build-module/hooks/use-entity-records.js +2 -3
- package/build-module/hooks/use-entity-records.js.map +1 -1
- package/build-module/hooks/use-query-select.js +1 -9
- package/build-module/hooks/use-query-select.js.map +1 -1
- package/build-module/hooks/use-resource-permissions.js +1 -10
- package/build-module/hooks/use-resource-permissions.js.map +1 -1
- package/build-module/index.js +11 -19
- package/build-module/index.js.map +1 -1
- package/build-module/locks/actions.js +0 -3
- package/build-module/locks/actions.js.map +1 -1
- package/build-module/locks/engine.js +0 -5
- package/build-module/locks/engine.js.map +1 -1
- package/build-module/locks/reducer.js +7 -7
- package/build-module/locks/reducer.js.map +1 -1
- package/build-module/locks/selectors.js +7 -8
- package/build-module/locks/selectors.js.map +1 -1
- package/build-module/locks/utils.js +4 -14
- package/build-module/locks/utils.js.map +1 -1
- package/build-module/name.js.map +1 -1
- package/build-module/private-apis.js.map +1 -1
- package/build-module/private-selectors.js +2 -2
- package/build-module/private-selectors.js.map +1 -1
- package/build-module/queried-data/actions.js +4 -3
- package/build-module/queried-data/actions.js.map +1 -1
- package/build-module/queried-data/get-query-parts.js +13 -21
- package/build-module/queried-data/get-query-parts.js.map +1 -1
- package/build-module/queried-data/index.js.map +1 -1
- package/build-module/queried-data/reducer.js +33 -42
- package/build-module/queried-data/reducer.js.map +1 -1
- package/build-module/queried-data/selectors.js +6 -21
- package/build-module/queried-data/selectors.js.map +1 -1
- package/build-module/reducer.js +79 -104
- package/build-module/reducer.js.map +1 -1
- package/build-module/resolvers.js +94 -96
- package/build-module/resolvers.js.map +1 -1
- package/build-module/selectors.js +56 -92
- package/build-module/selectors.js.map +1 -1
- package/build-module/sync.js +12 -0
- package/build-module/sync.js.map +1 -0
- package/build-module/types.js.map +1 -1
- package/build-module/utils/conservative-map-item.js +4 -8
- package/build-module/utils/conservative-map-item.js.map +1 -1
- package/build-module/utils/forward-resolver.js +0 -1
- package/build-module/utils/forward-resolver.js.map +1 -1
- package/build-module/utils/get-normalized-comma-separable.js +0 -2
- package/build-module/utils/get-normalized-comma-separable.js.map +1 -1
- package/build-module/utils/if-matching-action.js +0 -2
- package/build-module/utils/if-matching-action.js.map +1 -1
- package/build-module/utils/index.js.map +1 -1
- package/build-module/utils/is-raw-attribute.js.map +1 -1
- package/build-module/utils/on-sub-key.js +5 -7
- package/build-module/utils/on-sub-key.js.map +1 -1
- package/build-module/utils/replace-action.js +0 -1
- package/build-module/utils/replace-action.js.map +1 -1
- package/build-module/utils/set-nested-value.js +0 -3
- package/build-module/utils/set-nested-value.js.map +1 -1
- package/build-module/utils/with-weak-map-cache.js +3 -5
- package/build-module/utils/with-weak-map-cache.js.map +1 -1
- package/build-types/actions.d.ts.map +1 -1
- package/build-types/entities.d.ts +53 -0
- package/build-types/entities.d.ts.map +1 -1
- package/build-types/entity-provider.d.ts +1 -1
- package/build-types/entity-provider.d.ts.map +1 -1
- package/build-types/hooks/memoize.d.ts +1 -1
- package/build-types/hooks/memoize.d.ts.map +1 -1
- package/build-types/index.d.ts +3 -3
- package/build-types/index.d.ts.map +1 -1
- package/build-types/resolvers.d.ts.map +1 -1
- package/build-types/sync.d.ts +2 -0
- package/build-types/sync.d.ts.map +1 -0
- package/package.json +17 -15
- package/src/actions.js +24 -14
- package/src/entities.js +91 -0
- package/src/entity-provider.js +11 -2
- package/src/resolvers.js +89 -41
- package/src/sync.js +18 -0
- package/src/test/entity-provider.js +272 -0
- package/tsconfig.json +1 -0
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -3,26 +3,24 @@
|
|
|
3
3
|
*/
|
|
4
4
|
import { combineReducers } from '@wordpress/data';
|
|
5
5
|
import { compose } from '@wordpress/compose';
|
|
6
|
+
|
|
6
7
|
/**
|
|
7
8
|
* Internal dependencies
|
|
8
9
|
*/
|
|
9
|
-
|
|
10
10
|
import { conservativeMapItem, ifMatchingAction, replaceAction, onSubKey } from '../utils';
|
|
11
11
|
import { DEFAULT_ENTITY_KEY } from '../entities';
|
|
12
12
|
import getQueryParts from './get-query-parts';
|
|
13
|
-
|
|
14
13
|
function getContextFromAction(action) {
|
|
15
14
|
const {
|
|
16
15
|
query
|
|
17
16
|
} = action;
|
|
18
|
-
|
|
19
17
|
if (!query) {
|
|
20
18
|
return 'default';
|
|
21
19
|
}
|
|
22
|
-
|
|
23
20
|
const queryParts = getQueryParts(query);
|
|
24
21
|
return queryParts.context;
|
|
25
22
|
}
|
|
23
|
+
|
|
26
24
|
/**
|
|
27
25
|
* Returns a merged array of item IDs, given details of the received paginated
|
|
28
26
|
* items. The array is sparse-like with `undefined` entries where holes exist.
|
|
@@ -34,32 +32,28 @@ function getContextFromAction(action) {
|
|
|
34
32
|
*
|
|
35
33
|
* @return {number[]} Merged array of item IDs.
|
|
36
34
|
*/
|
|
37
|
-
|
|
38
|
-
|
|
39
35
|
export function getMergedItemIds(itemIds, nextItemIds, page, perPage) {
|
|
40
36
|
var _itemIds$length;
|
|
41
|
-
|
|
42
37
|
const receivedAllIds = page === 1 && perPage === -1;
|
|
43
|
-
|
|
44
38
|
if (receivedAllIds) {
|
|
45
39
|
return nextItemIds;
|
|
46
40
|
}
|
|
41
|
+
const nextItemIdsStartIndex = (page - 1) * perPage;
|
|
47
42
|
|
|
48
|
-
|
|
43
|
+
// If later page has already been received, default to the larger known
|
|
49
44
|
// size of the existing array, else calculate as extending the existing.
|
|
45
|
+
const size = Math.max((_itemIds$length = itemIds?.length) !== null && _itemIds$length !== void 0 ? _itemIds$length : 0, nextItemIdsStartIndex + nextItemIds.length);
|
|
50
46
|
|
|
51
|
-
|
|
52
|
-
|
|
47
|
+
// Preallocate array since size is known.
|
|
53
48
|
const mergedItemIds = new Array(size);
|
|
54
|
-
|
|
55
49
|
for (let i = 0; i < size; i++) {
|
|
56
50
|
// Preserve existing item ID except for subset of range of next items.
|
|
57
51
|
const isInNextItemsRange = i >= nextItemIdsStartIndex && i < nextItemIdsStartIndex + nextItemIds.length;
|
|
58
52
|
mergedItemIds[i] = isInNextItemsRange ? nextItemIds[i - nextItemIdsStartIndex] : itemIds?.[i];
|
|
59
53
|
}
|
|
60
|
-
|
|
61
54
|
return mergedItemIds;
|
|
62
55
|
}
|
|
56
|
+
|
|
63
57
|
/**
|
|
64
58
|
* Helper function to filter out entities with certain IDs.
|
|
65
59
|
* Entities are keyed by their ID.
|
|
@@ -69,16 +63,15 @@ export function getMergedItemIds(itemIds, nextItemIds, page, perPage) {
|
|
|
69
63
|
*
|
|
70
64
|
* @return {Object} Filtered entities.
|
|
71
65
|
*/
|
|
72
|
-
|
|
73
66
|
function removeEntitiesById(entities, ids) {
|
|
74
67
|
return Object.fromEntries(Object.entries(entities).filter(([id]) => !ids.some(itemId => {
|
|
75
68
|
if (Number.isInteger(itemId)) {
|
|
76
69
|
return itemId === +id;
|
|
77
70
|
}
|
|
78
|
-
|
|
79
71
|
return itemId === id;
|
|
80
72
|
})));
|
|
81
73
|
}
|
|
74
|
+
|
|
82
75
|
/**
|
|
83
76
|
* Reducer tracking items state, keyed by ID. Items are assumed to be normal,
|
|
84
77
|
* where identifiers are common across all queries.
|
|
@@ -88,16 +81,16 @@ function removeEntitiesById(entities, ids) {
|
|
|
88
81
|
*
|
|
89
82
|
* @return {Object} Next state.
|
|
90
83
|
*/
|
|
91
|
-
|
|
92
|
-
|
|
93
84
|
export function items(state = {}, action) {
|
|
94
85
|
switch (action.type) {
|
|
95
86
|
case 'RECEIVE_ITEMS':
|
|
96
87
|
{
|
|
97
88
|
const context = getContextFromAction(action);
|
|
98
89
|
const key = action.key || DEFAULT_ENTITY_KEY;
|
|
99
|
-
return {
|
|
100
|
-
|
|
90
|
+
return {
|
|
91
|
+
...state,
|
|
92
|
+
[context]: {
|
|
93
|
+
...state[context],
|
|
101
94
|
...action.items.reduce((accumulator, value) => {
|
|
102
95
|
const itemId = value[key];
|
|
103
96
|
accumulator[itemId] = conservativeMapItem(state?.[context]?.[itemId], value);
|
|
@@ -106,13 +99,12 @@ export function items(state = {}, action) {
|
|
|
106
99
|
}
|
|
107
100
|
};
|
|
108
101
|
}
|
|
109
|
-
|
|
110
102
|
case 'REMOVE_ITEMS':
|
|
111
103
|
return Object.fromEntries(Object.entries(state).map(([itemId, contextState]) => [itemId, removeEntitiesById(contextState, action.itemIds)]));
|
|
112
104
|
}
|
|
113
|
-
|
|
114
105
|
return state;
|
|
115
106
|
}
|
|
107
|
+
|
|
116
108
|
/**
|
|
117
109
|
* Reducer tracking item completeness, keyed by ID. A complete item is one for
|
|
118
110
|
* which all fields are known. This is used in supporting `_fields` queries,
|
|
@@ -125,7 +117,6 @@ export function items(state = {}, action) {
|
|
|
125
117
|
*
|
|
126
118
|
* @return {Object<string,Object<string,boolean>>} Next state.
|
|
127
119
|
*/
|
|
128
|
-
|
|
129
120
|
export function itemIsComplete(state = {}, action) {
|
|
130
121
|
switch (action.type) {
|
|
131
122
|
case 'RECEIVE_ITEMS':
|
|
@@ -134,34 +125,37 @@ export function itemIsComplete(state = {}, action) {
|
|
|
134
125
|
const {
|
|
135
126
|
query,
|
|
136
127
|
key = DEFAULT_ENTITY_KEY
|
|
137
|
-
} = action;
|
|
128
|
+
} = action;
|
|
129
|
+
|
|
130
|
+
// An item is considered complete if it is received without an associated
|
|
138
131
|
// fields query. Ideally, this would be implemented in such a way where the
|
|
139
132
|
// complete aggregate of all fields would satisfy completeness. Since the
|
|
140
133
|
// fields are not consistent across all entities, this would require
|
|
141
134
|
// introspection on the REST schema for each entity to know which fields
|
|
142
135
|
// compose a complete item for that entity.
|
|
143
|
-
|
|
144
136
|
const queryParts = query ? getQueryParts(query) : {};
|
|
145
137
|
const isCompleteQuery = !query || !Array.isArray(queryParts.fields);
|
|
146
|
-
return {
|
|
147
|
-
|
|
138
|
+
return {
|
|
139
|
+
...state,
|
|
140
|
+
[context]: {
|
|
141
|
+
...state[context],
|
|
148
142
|
...action.items.reduce((result, item) => {
|
|
149
|
-
const itemId = item[key];
|
|
150
|
-
// data may be outdated if receiving items for a field subset.
|
|
143
|
+
const itemId = item[key];
|
|
151
144
|
|
|
145
|
+
// Defer to completeness if already assigned. Technically the
|
|
146
|
+
// data may be outdated if receiving items for a field subset.
|
|
152
147
|
result[itemId] = state?.[context]?.[itemId] || isCompleteQuery;
|
|
153
148
|
return result;
|
|
154
149
|
}, {})
|
|
155
150
|
}
|
|
156
151
|
};
|
|
157
152
|
}
|
|
158
|
-
|
|
159
153
|
case 'REMOVE_ITEMS':
|
|
160
154
|
return Object.fromEntries(Object.entries(state).map(([itemId, contextState]) => [itemId, removeEntitiesById(contextState, action.itemIds)]));
|
|
161
155
|
}
|
|
162
|
-
|
|
163
156
|
return state;
|
|
164
157
|
}
|
|
158
|
+
|
|
165
159
|
/**
|
|
166
160
|
* Reducer tracking queries state, keyed by stable query key. Each reducer
|
|
167
161
|
* query object includes `itemIds` and `requestingPageByPerPage`.
|
|
@@ -171,22 +165,24 @@ export function itemIsComplete(state = {}, action) {
|
|
|
171
165
|
*
|
|
172
166
|
* @return {Object} Next state.
|
|
173
167
|
*/
|
|
174
|
-
|
|
175
|
-
|
|
168
|
+
const receiveQueries = compose([
|
|
169
|
+
// Limit to matching action type so we don't attempt to replace action on
|
|
176
170
|
// an unhandled action.
|
|
177
|
-
ifMatchingAction(action => 'query' in action),
|
|
171
|
+
ifMatchingAction(action => 'query' in action),
|
|
172
|
+
// Inject query parts into action for use both in `onSubKey` and reducer.
|
|
178
173
|
replaceAction(action => {
|
|
179
174
|
// `ifMatchingAction` still passes on initialization, where state is
|
|
180
175
|
// undefined and a query is not assigned. Avoid attempting to parse
|
|
181
176
|
// parts. `onSubKey` will omit by lack of `stableKey`.
|
|
182
177
|
if (action.query) {
|
|
183
|
-
return {
|
|
178
|
+
return {
|
|
179
|
+
...action,
|
|
184
180
|
...getQueryParts(action.query)
|
|
185
181
|
};
|
|
186
182
|
}
|
|
187
|
-
|
|
188
183
|
return action;
|
|
189
|
-
}), onSubKey('context'),
|
|
184
|
+
}), onSubKey('context'),
|
|
185
|
+
// Queries shape is shared, but keyed by query `stableKey` part. Original
|
|
190
186
|
// reducer tracks only a single query object.
|
|
191
187
|
onSubKey('stableKey')])((state = null, action) => {
|
|
192
188
|
const {
|
|
@@ -195,13 +191,12 @@ onSubKey('stableKey')])((state = null, action) => {
|
|
|
195
191
|
perPage,
|
|
196
192
|
key = DEFAULT_ENTITY_KEY
|
|
197
193
|
} = action;
|
|
198
|
-
|
|
199
194
|
if (type !== 'RECEIVE_ITEMS') {
|
|
200
195
|
return state;
|
|
201
196
|
}
|
|
202
|
-
|
|
203
197
|
return getMergedItemIds(state || [], action.items.map(item => item[key]), page, perPage);
|
|
204
198
|
});
|
|
199
|
+
|
|
205
200
|
/**
|
|
206
201
|
* Reducer tracking queries state.
|
|
207
202
|
*
|
|
@@ -210,24 +205,20 @@ onSubKey('stableKey')])((state = null, action) => {
|
|
|
210
205
|
*
|
|
211
206
|
* @return {Object} Next state.
|
|
212
207
|
*/
|
|
213
|
-
|
|
214
208
|
const queries = (state = {}, action) => {
|
|
215
209
|
switch (action.type) {
|
|
216
210
|
case 'RECEIVE_ITEMS':
|
|
217
211
|
return receiveQueries(state, action);
|
|
218
|
-
|
|
219
212
|
case 'REMOVE_ITEMS':
|
|
220
213
|
const removedItems = action.itemIds.reduce((result, itemId) => {
|
|
221
214
|
result[itemId] = true;
|
|
222
215
|
return result;
|
|
223
216
|
}, {});
|
|
224
217
|
return Object.fromEntries(Object.entries(state).map(([queryGroup, contextQueries]) => [queryGroup, Object.fromEntries(Object.entries(contextQueries).map(([query, queryItems]) => [query, queryItems.filter(queryId => !removedItems[queryId])]))]));
|
|
225
|
-
|
|
226
218
|
default:
|
|
227
219
|
return state;
|
|
228
220
|
}
|
|
229
221
|
};
|
|
230
|
-
|
|
231
222
|
export default combineReducers({
|
|
232
223
|
items,
|
|
233
224
|
itemIsComplete,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/core-data/src/queried-data/reducer.js"],"names":["combineReducers","compose","conservativeMapItem","ifMatchingAction","replaceAction","onSubKey","DEFAULT_ENTITY_KEY","getQueryParts","getContextFromAction","action","query","queryParts","context","getMergedItemIds","itemIds","nextItemIds","page","perPage","receivedAllIds","nextItemIdsStartIndex","size","Math","max","length","mergedItemIds","Array","i","isInNextItemsRange","removeEntitiesById","entities","ids","Object","fromEntries","entries","filter","id","some","itemId","Number","isInteger","items","state","type","key","reduce","accumulator","value","map","contextState","itemIsComplete","isCompleteQuery","isArray","fields","result","item","receiveQueries","queries","removedItems","queryGroup","contextQueries","queryItems","queryId"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,eAAT,QAAgC,iBAAhC;AACA,SAASC,OAAT,QAAwB,oBAAxB;AAEA;AACA;AACA;;AACA,SACCC,mBADD,EAECC,gBAFD,EAGCC,aAHD,EAICC,QAJD,QAKO,UALP;AAMA,SAASC,kBAAT,QAAmC,aAAnC;AACA,OAAOC,aAAP,MAA0B,mBAA1B;;AAEA,SAASC,oBAAT,CAA+BC,MAA/B,EAAwC;AACvC,QAAM;AAAEC,IAAAA;AAAF,MAAYD,MAAlB;;AACA,MAAK,CAAEC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CAAEd,KAAF,EAAShC,MAAT,CAArB;;AACD,SAAK,cAAL;AACC,YAAMgD,YAAY,GAAGhD,MAAM,CAACK,OAAP,CAAe8B,MAAf,CAAuB,CAAES,MAAF,EAAUhB,MAAV,KAAsB;AACjEgB,QAAAA,MAAM,CAAEhB,MAAF,CAAN,GAAmB,IAAnB;AACA,eAAOgB,MAAP;AACA,OAHoB,EAGlB,EAHkB,CAArB;AAKA,aAAOtB,MAAM,CAACC,WAAP,CACND,MAAM,CAACE,OAAP,CAAgBQ,KAAhB,EAAwBM,GAAxB,CACC,CAAE,CAAEW,UAAF,EAAcC,cAAd,CAAF,KAAsC,CACrCD,UADqC,EAErC3B,MAAM,CAACC,WAAP,CACCD,MAAM,CAACE,OAAP,CAAgB0B,cAAhB,EAAiCZ,GAAjC,CACC,CAAE,CAAErC,KAAF,EAASkD,UAAT,CAAF,KAA6B,CAC5BlD,KAD4B,EAE5BkD,UAAU,CAAC1B,MAAX,CACG2B,OAAF,IAAe,CAAEJ,YAAY,CAAEI,OAAF,CAD9B,CAF4B,CAD9B,CADD,CAFqC,CADvC,CADM,CAAP;;AAiBD;AACC,aAAOpB,KAAP;AA3BF;AA6BA,CA9BD;;AAgCA,eAAezC,eAAe,CAAE;AAC/BwC,EAAAA,KAD+B;AAE/BS,EAAAA,cAF+B;AAG/BO,EAAAA;AAH+B,CAAF,CAA9B","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { combineReducers } from '@wordpress/data';\nimport { compose } from '@wordpress/compose';\n\n/**\n * Internal dependencies\n */\nimport {\n\tconservativeMapItem,\n\tifMatchingAction,\n\treplaceAction,\n\tonSubKey,\n} from '../utils';\nimport { DEFAULT_ENTITY_KEY } from '../entities';\nimport getQueryParts from './get-query-parts';\n\nfunction getContextFromAction( action ) {\n\tconst { query } = action;\n\tif ( ! query ) {\n\t\treturn 'default';\n\t}\n\n\tconst queryParts = getQueryParts( query );\n\treturn queryParts.context;\n}\n\n/**\n * Returns a merged array of item IDs, given details of the received paginated\n * items. The array is sparse-like with `undefined` entries where holes exist.\n *\n * @param {?Array<number>} itemIds Original item IDs (default empty array).\n * @param {number[]} nextItemIds Item IDs to merge.\n * @param {number} page Page of items merged.\n * @param {number} perPage Number of items per page.\n *\n * @return {number[]} Merged array of item IDs.\n */\nexport function getMergedItemIds( itemIds, nextItemIds, page, perPage ) {\n\tconst receivedAllIds = page === 1 && perPage === -1;\n\tif ( receivedAllIds ) {\n\t\treturn nextItemIds;\n\t}\n\tconst nextItemIdsStartIndex = ( page - 1 ) * perPage;\n\n\t// If later page has already been received, default to the larger known\n\t// size of the existing array, else calculate as extending the existing.\n\tconst size = Math.max(\n\t\titemIds?.length ?? 0,\n\t\tnextItemIdsStartIndex + nextItemIds.length\n\t);\n\n\t// Preallocate array since size is known.\n\tconst mergedItemIds = new Array( size );\n\n\tfor ( let i = 0; i < size; i++ ) {\n\t\t// Preserve existing item ID except for subset of range of next items.\n\t\tconst isInNextItemsRange =\n\t\t\ti >= nextItemIdsStartIndex &&\n\t\t\ti < nextItemIdsStartIndex + nextItemIds.length;\n\n\t\tmergedItemIds[ i ] = isInNextItemsRange\n\t\t\t? nextItemIds[ i - nextItemIdsStartIndex ]\n\t\t\t: itemIds?.[ i ];\n\t}\n\n\treturn mergedItemIds;\n}\n\n/**\n * Helper function to filter out entities with certain IDs.\n * Entities are keyed by their ID.\n *\n * @param {Object} entities Entity objects, keyed by entity ID.\n * @param {Array} ids Entity IDs to filter out.\n *\n * @return {Object} Filtered entities.\n */\nfunction removeEntitiesById( entities, ids ) {\n\treturn Object.fromEntries(\n\t\tObject.entries( entities ).filter(\n\t\t\t( [ id ] ) =>\n\t\t\t\t! ids.some( ( itemId ) => {\n\t\t\t\t\tif ( Number.isInteger( itemId ) ) {\n\t\t\t\t\t\treturn itemId === +id;\n\t\t\t\t\t}\n\t\t\t\t\treturn itemId === id;\n\t\t\t\t} )\n\t\t)\n\t);\n}\n\n/**\n * Reducer tracking items state, keyed by ID. Items are assumed to be normal,\n * where identifiers are common across all queries.\n *\n * @param {Object} state Current state.\n * @param {Object} action Dispatched action.\n *\n * @return {Object} Next state.\n */\nexport function items( state = {}, action ) {\n\tswitch ( action.type ) {\n\t\tcase 'RECEIVE_ITEMS': {\n\t\t\tconst context = getContextFromAction( action );\n\t\t\tconst key = action.key || DEFAULT_ENTITY_KEY;\n\t\t\treturn {\n\t\t\t\t...state,\n\t\t\t\t[ context ]: {\n\t\t\t\t\t...state[ context ],\n\t\t\t\t\t...action.items.reduce( ( accumulator, value ) => {\n\t\t\t\t\t\tconst itemId = value[ key ];\n\t\t\t\t\t\taccumulator[ itemId ] = conservativeMapItem(\n\t\t\t\t\t\t\tstate?.[ context ]?.[ itemId ],\n\t\t\t\t\t\t\tvalue\n\t\t\t\t\t\t);\n\t\t\t\t\t\treturn accumulator;\n\t\t\t\t\t}, {} ),\n\t\t\t\t},\n\t\t\t};\n\t\t}\n\t\tcase 'REMOVE_ITEMS':\n\t\t\treturn Object.fromEntries(\n\t\t\t\tObject.entries( state ).map( ( [ itemId, contextState ] ) => [\n\t\t\t\t\titemId,\n\t\t\t\t\tremoveEntitiesById( contextState, action.itemIds ),\n\t\t\t\t] )\n\t\t\t);\n\t}\n\treturn state;\n}\n\n/**\n * Reducer tracking item completeness, keyed by ID. A complete item is one for\n * which all fields are known. This is used in supporting `_fields` queries,\n * where not all properties associated with an entity are necessarily returned.\n * In such cases, completeness is used as an indication of whether it would be\n * safe to use queried data for a non-`_fields`-limited request.\n *\n * @param {Object<string,Object<string,boolean>>} state Current state.\n * @param {Object} action Dispatched action.\n *\n * @return {Object<string,Object<string,boolean>>} Next state.\n */\nexport function itemIsComplete( state = {}, action ) {\n\tswitch ( action.type ) {\n\t\tcase 'RECEIVE_ITEMS': {\n\t\t\tconst context = getContextFromAction( action );\n\t\t\tconst { query, key = DEFAULT_ENTITY_KEY } = action;\n\n\t\t\t// An item is considered complete if it is received without an associated\n\t\t\t// fields query. Ideally, this would be implemented in such a way where the\n\t\t\t// complete aggregate of all fields would satisfy completeness. Since the\n\t\t\t// fields are not consistent across all entities, this would require\n\t\t\t// introspection on the REST schema for each entity to know which fields\n\t\t\t// compose a complete item for that entity.\n\t\t\tconst queryParts = query ? getQueryParts( query ) : {};\n\t\t\tconst isCompleteQuery =\n\t\t\t\t! query || ! Array.isArray( queryParts.fields );\n\n\t\t\treturn {\n\t\t\t\t...state,\n\t\t\t\t[ context ]: {\n\t\t\t\t\t...state[ context ],\n\t\t\t\t\t...action.items.reduce( ( result, item ) => {\n\t\t\t\t\t\tconst itemId = item[ key ];\n\n\t\t\t\t\t\t// Defer to completeness if already assigned. Technically the\n\t\t\t\t\t\t// data may be outdated if receiving items for a field subset.\n\t\t\t\t\t\tresult[ itemId ] =\n\t\t\t\t\t\t\tstate?.[ context ]?.[ itemId ] || isCompleteQuery;\n\n\t\t\t\t\t\treturn result;\n\t\t\t\t\t}, {} ),\n\t\t\t\t},\n\t\t\t};\n\t\t}\n\t\tcase 'REMOVE_ITEMS':\n\t\t\treturn Object.fromEntries(\n\t\t\t\tObject.entries( state ).map( ( [ itemId, contextState ] ) => [\n\t\t\t\t\titemId,\n\t\t\t\t\tremoveEntitiesById( contextState, action.itemIds ),\n\t\t\t\t] )\n\t\t\t);\n\t}\n\n\treturn state;\n}\n\n/**\n * Reducer tracking queries state, keyed by stable query key. Each reducer\n * query object includes `itemIds` and `requestingPageByPerPage`.\n *\n * @param {Object} state Current state.\n * @param {Object} action Dispatched action.\n *\n * @return {Object} Next state.\n */\nconst receiveQueries = compose( [\n\t// Limit to matching action type so we don't attempt to replace action on\n\t// an unhandled action.\n\tifMatchingAction( ( action ) => 'query' in action ),\n\n\t// Inject query parts into action for use both in `onSubKey` and reducer.\n\treplaceAction( ( action ) => {\n\t\t// `ifMatchingAction` still passes on initialization, where state is\n\t\t// undefined and a query is not assigned. Avoid attempting to parse\n\t\t// parts. `onSubKey` will omit by lack of `stableKey`.\n\t\tif ( action.query ) {\n\t\t\treturn {\n\t\t\t\t...action,\n\t\t\t\t...getQueryParts( action.query ),\n\t\t\t};\n\t\t}\n\n\t\treturn action;\n\t} ),\n\n\tonSubKey( 'context' ),\n\n\t// Queries shape is shared, but keyed by query `stableKey` part. Original\n\t// reducer tracks only a single query object.\n\tonSubKey( 'stableKey' ),\n] )( ( state = null, action ) => {\n\tconst { type, page, perPage, key = DEFAULT_ENTITY_KEY } = action;\n\n\tif ( type !== 'RECEIVE_ITEMS' ) {\n\t\treturn state;\n\t}\n\n\treturn getMergedItemIds(\n\t\tstate || [],\n\t\taction.items.map( ( item ) => item[ key ] ),\n\t\tpage,\n\t\tperPage\n\t);\n} );\n\n/**\n * Reducer tracking queries state.\n *\n * @param {Object} state Current state.\n * @param {Object} action Dispatched action.\n *\n * @return {Object} Next state.\n */\nconst queries = ( state = {}, action ) => {\n\tswitch ( action.type ) {\n\t\tcase 'RECEIVE_ITEMS':\n\t\t\treturn receiveQueries( state, action );\n\t\tcase 'REMOVE_ITEMS':\n\t\t\tconst removedItems = action.itemIds.reduce( ( result, itemId ) => {\n\t\t\t\tresult[ itemId ] = true;\n\t\t\t\treturn result;\n\t\t\t}, {} );\n\n\t\t\treturn Object.fromEntries(\n\t\t\t\tObject.entries( state ).map(\n\t\t\t\t\t( [ queryGroup, contextQueries ] ) => [\n\t\t\t\t\t\tqueryGroup,\n\t\t\t\t\t\tObject.fromEntries(\n\t\t\t\t\t\t\tObject.entries( contextQueries ).map(\n\t\t\t\t\t\t\t\t( [ query, queryItems ] ) => [\n\t\t\t\t\t\t\t\t\tquery,\n\t\t\t\t\t\t\t\t\tqueryItems.filter(\n\t\t\t\t\t\t\t\t\t\t( queryId ) => ! removedItems[ queryId ]\n\t\t\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t\t]\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t),\n\t\t\t\t\t]\n\t\t\t\t)\n\t\t\t);\n\t\tdefault:\n\t\t\treturn state;\n\t}\n};\n\nexport default combineReducers( {\n\titems,\n\titemIsComplete,\n\tqueries,\n} );\n"]}
|
|
1
|
+
{"version":3,"names":["combineReducers","compose","conservativeMapItem","ifMatchingAction","replaceAction","onSubKey","DEFAULT_ENTITY_KEY","getQueryParts","getContextFromAction","action","query","queryParts","context","getMergedItemIds","itemIds","nextItemIds","page","perPage","_itemIds$length","receivedAllIds","nextItemIdsStartIndex","size","Math","max","length","mergedItemIds","Array","i","isInNextItemsRange","removeEntitiesById","entities","ids","Object","fromEntries","entries","filter","id","some","itemId","Number","isInteger","items","state","type","key","reduce","accumulator","value","map","contextState","itemIsComplete","isCompleteQuery","isArray","fields","result","item","receiveQueries","queries","removedItems","queryGroup","contextQueries","queryItems","queryId"],"sources":["@wordpress/core-data/src/queried-data/reducer.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { combineReducers } from '@wordpress/data';\nimport { compose } from '@wordpress/compose';\n\n/**\n * Internal dependencies\n */\nimport {\n\tconservativeMapItem,\n\tifMatchingAction,\n\treplaceAction,\n\tonSubKey,\n} from '../utils';\nimport { DEFAULT_ENTITY_KEY } from '../entities';\nimport getQueryParts from './get-query-parts';\n\nfunction getContextFromAction( action ) {\n\tconst { query } = action;\n\tif ( ! query ) {\n\t\treturn 'default';\n\t}\n\n\tconst queryParts = getQueryParts( query );\n\treturn queryParts.context;\n}\n\n/**\n * Returns a merged array of item IDs, given details of the received paginated\n * items. The array is sparse-like with `undefined` entries where holes exist.\n *\n * @param {?Array<number>} itemIds Original item IDs (default empty array).\n * @param {number[]} nextItemIds Item IDs to merge.\n * @param {number} page Page of items merged.\n * @param {number} perPage Number of items per page.\n *\n * @return {number[]} Merged array of item IDs.\n */\nexport function getMergedItemIds( itemIds, nextItemIds, page, perPage ) {\n\tconst receivedAllIds = page === 1 && perPage === -1;\n\tif ( receivedAllIds ) {\n\t\treturn nextItemIds;\n\t}\n\tconst nextItemIdsStartIndex = ( page - 1 ) * perPage;\n\n\t// If later page has already been received, default to the larger known\n\t// size of the existing array, else calculate as extending the existing.\n\tconst size = Math.max(\n\t\titemIds?.length ?? 0,\n\t\tnextItemIdsStartIndex + nextItemIds.length\n\t);\n\n\t// Preallocate array since size is known.\n\tconst mergedItemIds = new Array( size );\n\n\tfor ( let i = 0; i < size; i++ ) {\n\t\t// Preserve existing item ID except for subset of range of next items.\n\t\tconst isInNextItemsRange =\n\t\t\ti >= nextItemIdsStartIndex &&\n\t\t\ti < nextItemIdsStartIndex + nextItemIds.length;\n\n\t\tmergedItemIds[ i ] = isInNextItemsRange\n\t\t\t? nextItemIds[ i - nextItemIdsStartIndex ]\n\t\t\t: itemIds?.[ i ];\n\t}\n\n\treturn mergedItemIds;\n}\n\n/**\n * Helper function to filter out entities with certain IDs.\n * Entities are keyed by their ID.\n *\n * @param {Object} entities Entity objects, keyed by entity ID.\n * @param {Array} ids Entity IDs to filter out.\n *\n * @return {Object} Filtered entities.\n */\nfunction removeEntitiesById( entities, ids ) {\n\treturn Object.fromEntries(\n\t\tObject.entries( entities ).filter(\n\t\t\t( [ id ] ) =>\n\t\t\t\t! ids.some( ( itemId ) => {\n\t\t\t\t\tif ( Number.isInteger( itemId ) ) {\n\t\t\t\t\t\treturn itemId === +id;\n\t\t\t\t\t}\n\t\t\t\t\treturn itemId === id;\n\t\t\t\t} )\n\t\t)\n\t);\n}\n\n/**\n * Reducer tracking items state, keyed by ID. Items are assumed to be normal,\n * where identifiers are common across all queries.\n *\n * @param {Object} state Current state.\n * @param {Object} action Dispatched action.\n *\n * @return {Object} Next state.\n */\nexport function items( state = {}, action ) {\n\tswitch ( action.type ) {\n\t\tcase 'RECEIVE_ITEMS': {\n\t\t\tconst context = getContextFromAction( action );\n\t\t\tconst key = action.key || DEFAULT_ENTITY_KEY;\n\t\t\treturn {\n\t\t\t\t...state,\n\t\t\t\t[ context ]: {\n\t\t\t\t\t...state[ context ],\n\t\t\t\t\t...action.items.reduce( ( accumulator, value ) => {\n\t\t\t\t\t\tconst itemId = value[ key ];\n\t\t\t\t\t\taccumulator[ itemId ] = conservativeMapItem(\n\t\t\t\t\t\t\tstate?.[ context ]?.[ itemId ],\n\t\t\t\t\t\t\tvalue\n\t\t\t\t\t\t);\n\t\t\t\t\t\treturn accumulator;\n\t\t\t\t\t}, {} ),\n\t\t\t\t},\n\t\t\t};\n\t\t}\n\t\tcase 'REMOVE_ITEMS':\n\t\t\treturn Object.fromEntries(\n\t\t\t\tObject.entries( state ).map( ( [ itemId, contextState ] ) => [\n\t\t\t\t\titemId,\n\t\t\t\t\tremoveEntitiesById( contextState, action.itemIds ),\n\t\t\t\t] )\n\t\t\t);\n\t}\n\treturn state;\n}\n\n/**\n * Reducer tracking item completeness, keyed by ID. A complete item is one for\n * which all fields are known. This is used in supporting `_fields` queries,\n * where not all properties associated with an entity are necessarily returned.\n * In such cases, completeness is used as an indication of whether it would be\n * safe to use queried data for a non-`_fields`-limited request.\n *\n * @param {Object<string,Object<string,boolean>>} state Current state.\n * @param {Object} action Dispatched action.\n *\n * @return {Object<string,Object<string,boolean>>} Next state.\n */\nexport function itemIsComplete( state = {}, action ) {\n\tswitch ( action.type ) {\n\t\tcase 'RECEIVE_ITEMS': {\n\t\t\tconst context = getContextFromAction( action );\n\t\t\tconst { query, key = DEFAULT_ENTITY_KEY } = action;\n\n\t\t\t// An item is considered complete if it is received without an associated\n\t\t\t// fields query. Ideally, this would be implemented in such a way where the\n\t\t\t// complete aggregate of all fields would satisfy completeness. Since the\n\t\t\t// fields are not consistent across all entities, this would require\n\t\t\t// introspection on the REST schema for each entity to know which fields\n\t\t\t// compose a complete item for that entity.\n\t\t\tconst queryParts = query ? getQueryParts( query ) : {};\n\t\t\tconst isCompleteQuery =\n\t\t\t\t! query || ! Array.isArray( queryParts.fields );\n\n\t\t\treturn {\n\t\t\t\t...state,\n\t\t\t\t[ context ]: {\n\t\t\t\t\t...state[ context ],\n\t\t\t\t\t...action.items.reduce( ( result, item ) => {\n\t\t\t\t\t\tconst itemId = item[ key ];\n\n\t\t\t\t\t\t// Defer to completeness if already assigned. Technically the\n\t\t\t\t\t\t// data may be outdated if receiving items for a field subset.\n\t\t\t\t\t\tresult[ itemId ] =\n\t\t\t\t\t\t\tstate?.[ context ]?.[ itemId ] || isCompleteQuery;\n\n\t\t\t\t\t\treturn result;\n\t\t\t\t\t}, {} ),\n\t\t\t\t},\n\t\t\t};\n\t\t}\n\t\tcase 'REMOVE_ITEMS':\n\t\t\treturn Object.fromEntries(\n\t\t\t\tObject.entries( state ).map( ( [ itemId, contextState ] ) => [\n\t\t\t\t\titemId,\n\t\t\t\t\tremoveEntitiesById( contextState, action.itemIds ),\n\t\t\t\t] )\n\t\t\t);\n\t}\n\n\treturn state;\n}\n\n/**\n * Reducer tracking queries state, keyed by stable query key. Each reducer\n * query object includes `itemIds` and `requestingPageByPerPage`.\n *\n * @param {Object} state Current state.\n * @param {Object} action Dispatched action.\n *\n * @return {Object} Next state.\n */\nconst receiveQueries = compose( [\n\t// Limit to matching action type so we don't attempt to replace action on\n\t// an unhandled action.\n\tifMatchingAction( ( action ) => 'query' in action ),\n\n\t// Inject query parts into action for use both in `onSubKey` and reducer.\n\treplaceAction( ( action ) => {\n\t\t// `ifMatchingAction` still passes on initialization, where state is\n\t\t// undefined and a query is not assigned. Avoid attempting to parse\n\t\t// parts. `onSubKey` will omit by lack of `stableKey`.\n\t\tif ( action.query ) {\n\t\t\treturn {\n\t\t\t\t...action,\n\t\t\t\t...getQueryParts( action.query ),\n\t\t\t};\n\t\t}\n\n\t\treturn action;\n\t} ),\n\n\tonSubKey( 'context' ),\n\n\t// Queries shape is shared, but keyed by query `stableKey` part. Original\n\t// reducer tracks only a single query object.\n\tonSubKey( 'stableKey' ),\n] )( ( state = null, action ) => {\n\tconst { type, page, perPage, key = DEFAULT_ENTITY_KEY } = action;\n\n\tif ( type !== 'RECEIVE_ITEMS' ) {\n\t\treturn state;\n\t}\n\n\treturn getMergedItemIds(\n\t\tstate || [],\n\t\taction.items.map( ( item ) => item[ key ] ),\n\t\tpage,\n\t\tperPage\n\t);\n} );\n\n/**\n * Reducer tracking queries state.\n *\n * @param {Object} state Current state.\n * @param {Object} action Dispatched action.\n *\n * @return {Object} Next state.\n */\nconst queries = ( state = {}, action ) => {\n\tswitch ( action.type ) {\n\t\tcase 'RECEIVE_ITEMS':\n\t\t\treturn receiveQueries( state, action );\n\t\tcase 'REMOVE_ITEMS':\n\t\t\tconst removedItems = action.itemIds.reduce( ( result, itemId ) => {\n\t\t\t\tresult[ itemId ] = true;\n\t\t\t\treturn result;\n\t\t\t}, {} );\n\n\t\t\treturn Object.fromEntries(\n\t\t\t\tObject.entries( state ).map(\n\t\t\t\t\t( [ queryGroup, contextQueries ] ) => [\n\t\t\t\t\t\tqueryGroup,\n\t\t\t\t\t\tObject.fromEntries(\n\t\t\t\t\t\t\tObject.entries( contextQueries ).map(\n\t\t\t\t\t\t\t\t( [ query, queryItems ] ) => [\n\t\t\t\t\t\t\t\t\tquery,\n\t\t\t\t\t\t\t\t\tqueryItems.filter(\n\t\t\t\t\t\t\t\t\t\t( queryId ) => ! removedItems[ queryId ]\n\t\t\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t\t]\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t),\n\t\t\t\t\t]\n\t\t\t\t)\n\t\t\t);\n\t\tdefault:\n\t\t\treturn state;\n\t}\n};\n\nexport default combineReducers( {\n\titems,\n\titemIsComplete,\n\tqueries,\n} );\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,eAAe,QAAQ,iBAAiB;AACjD,SAASC,OAAO,QAAQ,oBAAoB;;AAE5C;AACA;AACA;AACA,SACCC,mBAAmB,EACnBC,gBAAgB,EAChBC,aAAa,EACbC,QAAQ,QACF,UAAU;AACjB,SAASC,kBAAkB,QAAQ,aAAa;AAChD,OAAOC,aAAa,MAAM,mBAAmB;AAE7C,SAASC,oBAAoBA,CAAEC,MAAM,EAAG;EACvC,MAAM;IAAEC;EAAM,CAAC,GAAGD,MAAM;EACxB,IAAK,CAAEC,KAAK,EAAG;IACd,OAAO,SAAS;EACjB;EAEA,MAAM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KAAM;QACjEgB,MAAM,CAAEhB,MAAM,CAAE,GAAG,IAAI;QACvB,OAAOgB,MAAM;MACd,CAAC,EAAE,CAAC,CAAE,CAAC;MAEP,OAAOtB,MAAM,CAACC,WAAW,CACxBD,MAAM,CAACE,OAAO,CAAEQ,KAAM,CAAC,CAACM,GAAG,CAC1B,CAAE,CAAEW,UAAU,EAAEC,cAAc,CAAE,KAAM,CACrCD,UAAU,EACV3B,MAAM,CAACC,WAAW,CACjBD,MAAM,CAACE,OAAO,CAAE0B,cAAe,CAAC,CAACZ,GAAG,CACnC,CAAE,CAAEtC,KAAK,EAAEmD,UAAU,CAAE,KAAM,CAC5BnD,KAAK,EACLmD,UAAU,CAAC1B,MAAM,CACd2B,OAAO,IAAM,CAAEJ,YAAY,CAAEI,OAAO,CACvC,CAAC,CAEH,CACD,CAAC,CAEH,CACD,CAAC;IACF;MACC,OAAOpB,KAAK;EACd;AACD,CAAC;AAED,eAAe1C,eAAe,CAAE;EAC/ByC,KAAK;EACLS,cAAc;EACdO;AACD,CAAE,CAAC"}
|
|
@@ -3,12 +3,13 @@
|
|
|
3
3
|
*/
|
|
4
4
|
import createSelector from 'rememo';
|
|
5
5
|
import EquivalentKeyMap from 'equivalent-key-map';
|
|
6
|
+
|
|
6
7
|
/**
|
|
7
8
|
* Internal dependencies
|
|
8
9
|
*/
|
|
9
|
-
|
|
10
10
|
import getQueryParts from './get-query-parts';
|
|
11
11
|
import { setNestedValue } from '../utils';
|
|
12
|
+
|
|
12
13
|
/**
|
|
13
14
|
* Cache of state keys to EquivalentKeyMap where the inner map tracks queries
|
|
14
15
|
* to their resulting items set. WeakMap allows garbage collection on expired
|
|
@@ -16,8 +17,8 @@ import { setNestedValue } from '../utils';
|
|
|
16
17
|
*
|
|
17
18
|
* @type {WeakMap<Object,EquivalentKeyMap>}
|
|
18
19
|
*/
|
|
19
|
-
|
|
20
20
|
const queriedItemsCacheByState = new WeakMap();
|
|
21
|
+
|
|
21
22
|
/**
|
|
22
23
|
* Returns items for a given query, or null if the items are not known.
|
|
23
24
|
*
|
|
@@ -26,7 +27,6 @@ const queriedItemsCacheByState = new WeakMap();
|
|
|
26
27
|
*
|
|
27
28
|
* @return {?Array} Query items.
|
|
28
29
|
*/
|
|
29
|
-
|
|
30
30
|
function getQueriedItemsUncached(state, query) {
|
|
31
31
|
const {
|
|
32
32
|
stableKey,
|
|
@@ -37,37 +37,29 @@ function getQueriedItemsUncached(state, query) {
|
|
|
37
37
|
context
|
|
38
38
|
} = getQueryParts(query);
|
|
39
39
|
let itemIds;
|
|
40
|
-
|
|
41
40
|
if (state.queries?.[context]?.[stableKey]) {
|
|
42
41
|
itemIds = state.queries[context][stableKey];
|
|
43
42
|
}
|
|
44
|
-
|
|
45
43
|
if (!itemIds) {
|
|
46
44
|
return null;
|
|
47
45
|
}
|
|
48
|
-
|
|
49
46
|
const startOffset = perPage === -1 ? 0 : (page - 1) * perPage;
|
|
50
47
|
const endOffset = perPage === -1 ? itemIds.length : Math.min(startOffset + perPage, itemIds.length);
|
|
51
48
|
const items = [];
|
|
52
|
-
|
|
53
49
|
for (let i = startOffset; i < endOffset; i++) {
|
|
54
50
|
const itemId = itemIds[i];
|
|
55
|
-
|
|
56
51
|
if (Array.isArray(include) && !include.includes(itemId)) {
|
|
57
52
|
continue;
|
|
58
|
-
}
|
|
59
|
-
|
|
53
|
+
}
|
|
60
54
|
|
|
55
|
+
// Having a target item ID doesn't guarantee that this object has been queried.
|
|
61
56
|
if (!state.items[context]?.hasOwnProperty(itemId)) {
|
|
62
57
|
return null;
|
|
63
58
|
}
|
|
64
|
-
|
|
65
59
|
const item = state.items[context][itemId];
|
|
66
60
|
let filteredItem;
|
|
67
|
-
|
|
68
61
|
if (Array.isArray(fields)) {
|
|
69
62
|
filteredItem = {};
|
|
70
|
-
|
|
71
63
|
for (let f = 0; f < fields.length; f++) {
|
|
72
64
|
const field = fields[f].split('.');
|
|
73
65
|
let value = item;
|
|
@@ -82,15 +74,13 @@ function getQueriedItemsUncached(state, query) {
|
|
|
82
74
|
if (!state.itemIsComplete[context]?.[itemId]) {
|
|
83
75
|
return null;
|
|
84
76
|
}
|
|
85
|
-
|
|
86
77
|
filteredItem = item;
|
|
87
78
|
}
|
|
88
|
-
|
|
89
79
|
items.push(filteredItem);
|
|
90
80
|
}
|
|
91
|
-
|
|
92
81
|
return items;
|
|
93
82
|
}
|
|
83
|
+
|
|
94
84
|
/**
|
|
95
85
|
* Returns items for a given query, or null if the items are not known. Caches
|
|
96
86
|
* result both per state (by reference) and per query (by deep equality).
|
|
@@ -104,14 +94,10 @@ function getQueriedItemsUncached(state, query) {
|
|
|
104
94
|
*
|
|
105
95
|
* @return {?Array} Query items.
|
|
106
96
|
*/
|
|
107
|
-
|
|
108
|
-
|
|
109
97
|
export const getQueriedItems = createSelector((state, query = {}) => {
|
|
110
98
|
let queriedItemsCache = queriedItemsCacheByState.get(state);
|
|
111
|
-
|
|
112
99
|
if (queriedItemsCache) {
|
|
113
100
|
const queriedItems = queriedItemsCache.get(query);
|
|
114
|
-
|
|
115
101
|
if (queriedItems !== undefined) {
|
|
116
102
|
return queriedItems;
|
|
117
103
|
}
|
|
@@ -119,7 +105,6 @@ export const getQueriedItems = createSelector((state, query = {}) => {
|
|
|
119
105
|
queriedItemsCache = new EquivalentKeyMap();
|
|
120
106
|
queriedItemsCacheByState.set(state, queriedItemsCache);
|
|
121
107
|
}
|
|
122
|
-
|
|
123
108
|
const items = getQueriedItemsUncached(state, query);
|
|
124
109
|
queriedItemsCache.set(query, items);
|
|
125
110
|
return items;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"names":["createSelector","EquivalentKeyMap","getQueryParts","setNestedValue","queriedItemsCacheByState","WeakMap","getQueriedItemsUncached","state","query","stableKey","page","perPage","include","fields","context","itemIds","queries","startOffset","endOffset","length","Math","min","items","i","itemId","Array","isArray","includes","hasOwnProperty","item","filteredItem","f","field","split","value","forEach","fieldName","itemIsComplete","push","getQueriedItems","queriedItemsCache","get","queriedItems","undefined","set"],"sources":["@wordpress/core-data/src/queried-data/selectors.js"],"sourcesContent":["/**\n * External dependencies\n */\nimport createSelector from 'rememo';\nimport EquivalentKeyMap from 'equivalent-key-map';\n\n/**\n * Internal dependencies\n */\nimport getQueryParts from './get-query-parts';\nimport { setNestedValue } from '../utils';\n\n/**\n * Cache of state keys to EquivalentKeyMap where the inner map tracks queries\n * to their resulting items set. WeakMap allows garbage collection on expired\n * state references.\n *\n * @type {WeakMap<Object,EquivalentKeyMap>}\n */\nconst queriedItemsCacheByState = new WeakMap();\n\n/**\n * Returns items for a given query, or null if the items are not known.\n *\n * @param {Object} state State object.\n * @param {?Object} query Optional query.\n *\n * @return {?Array} Query items.\n */\nfunction getQueriedItemsUncached( state, query ) {\n\tconst { stableKey, page, perPage, include, fields, context } =\n\t\tgetQueryParts( query );\n\tlet itemIds;\n\n\tif ( state.queries?.[ context ]?.[ stableKey ] ) {\n\t\titemIds = state.queries[ context ][ stableKey ];\n\t}\n\n\tif ( ! itemIds ) {\n\t\treturn null;\n\t}\n\n\tconst startOffset = perPage === -1 ? 0 : ( page - 1 ) * perPage;\n\tconst endOffset =\n\t\tperPage === -1\n\t\t\t? itemIds.length\n\t\t\t: Math.min( startOffset + perPage, itemIds.length );\n\n\tconst items = [];\n\tfor ( let i = startOffset; i < endOffset; i++ ) {\n\t\tconst itemId = itemIds[ i ];\n\t\tif ( Array.isArray( include ) && ! include.includes( itemId ) ) {\n\t\t\tcontinue;\n\t\t}\n\n\t\t// Having a target item ID doesn't guarantee that this object has been queried.\n\t\tif ( ! state.items[ context ]?.hasOwnProperty( itemId ) ) {\n\t\t\treturn null;\n\t\t}\n\n\t\tconst item = state.items[ context ][ itemId ];\n\n\t\tlet filteredItem;\n\t\tif ( Array.isArray( fields ) ) {\n\t\t\tfilteredItem = {};\n\n\t\t\tfor ( let f = 0; f < fields.length; f++ ) {\n\t\t\t\tconst field = fields[ f ].split( '.' );\n\t\t\t\tlet value = item;\n\t\t\t\tfield.forEach( ( fieldName ) => {\n\t\t\t\t\tvalue = value[ fieldName ];\n\t\t\t\t} );\n\n\t\t\t\tsetNestedValue( filteredItem, field, value );\n\t\t\t}\n\t\t} else {\n\t\t\t// If expecting a complete item, validate that completeness, or\n\t\t\t// otherwise abort.\n\t\t\tif ( ! state.itemIsComplete[ context ]?.[ itemId ] ) {\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\tfilteredItem = item;\n\t\t}\n\n\t\titems.push( filteredItem );\n\t}\n\n\treturn items;\n}\n\n/**\n * Returns items for a given query, or null if the items are not known. Caches\n * result both per state (by reference) and per query (by deep equality).\n * The caching approach is intended to be durable to query objects which are\n * deeply but not referentially equal, since otherwise:\n *\n * `getQueriedItems( state, {} ) !== getQueriedItems( state, {} )`\n *\n * @param {Object} state State object.\n * @param {?Object} query Optional query.\n *\n * @return {?Array} Query items.\n */\nexport const getQueriedItems = createSelector( ( state, query = {} ) => {\n\tlet queriedItemsCache = queriedItemsCacheByState.get( state );\n\tif ( queriedItemsCache ) {\n\t\tconst queriedItems = queriedItemsCache.get( query );\n\t\tif ( queriedItems !== undefined ) {\n\t\t\treturn queriedItems;\n\t\t}\n\t} else {\n\t\tqueriedItemsCache = new EquivalentKeyMap();\n\t\tqueriedItemsCacheByState.set( state, queriedItemsCache );\n\t}\n\n\tconst items = getQueriedItemsUncached( state, query );\n\tqueriedItemsCache.set( query, items );\n\treturn items;\n} );\n"],"mappings":"AAAA;AACA;AACA;AACA,OAAOA,cAAc,MAAM,QAAQ;AACnC,OAAOC,gBAAgB,MAAM,oBAAoB;;AAEjD;AACA;AACA;AACA,OAAOC,aAAa,MAAM,mBAAmB;AAC7C,SAASC,cAAc,QAAQ,UAAU;;AAEzC;AACA;AACA;AACA;AACA;AACA;AACA;AACA,MAAMC,wBAAwB,GAAG,IAAIC,OAAO,CAAC,CAAC;;AAE9C;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAASC,uBAAuBA,CAAEC,K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