@wordpress/core-data 4.6.0 → 4.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +11 -1
- package/README.md +209 -119
- package/build/actions.js.map +1 -1
- package/build/batch/create-batch.js +1 -1
- package/build/batch/create-batch.js.map +1 -1
- package/build/entities.js +49 -35
- package/build/entities.js.map +1 -1
- package/build/entity-provider.js.map +1 -1
- package/build/entity-types/entities.js +6 -0
- package/build/entity-types/entities.js.map +1 -0
- package/build/hooks/constants.js +0 -2
- package/build/hooks/constants.js.map +1 -1
- package/build/hooks/index.js +38 -0
- package/build/hooks/index.js.map +1 -0
- package/build/hooks/use-entity-record.js +22 -8
- package/build/hooks/use-entity-record.js.map +1 -1
- package/build/hooks/use-entity-records.js +21 -8
- package/build/hooks/use-entity-records.js.map +1 -1
- package/build/hooks/use-query-select.js.map +1 -1
- package/build/index.js +17 -21
- package/build/index.js.map +1 -1
- package/build/queried-data/selectors.js.map +1 -1
- package/build/resolvers.js.map +1 -1
- package/build/selectors.js +206 -169
- package/build/selectors.js.map +1 -1
- package/build/utils/forward-resolver.js.map +1 -1
- package/build/utils/on-sub-key.js.map +1 -1
- package/build/utils/with-weak-map-cache.js +1 -7
- package/build/utils/with-weak-map-cache.js.map +1 -1
- package/build-module/actions.js.map +1 -1
- package/build-module/batch/create-batch.js +2 -2
- package/build-module/batch/create-batch.js.map +1 -1
- package/build-module/entities.js +49 -35
- package/build-module/entities.js.map +1 -1
- package/build-module/entity-provider.js.map +1 -1
- package/build-module/entity-types/entities.js +2 -0
- package/build-module/entity-types/entities.js.map +1 -0
- package/build-module/hooks/constants.js +0 -1
- package/build-module/hooks/constants.js.map +1 -1
- package/build-module/hooks/index.js +3 -0
- package/build-module/hooks/index.js.map +1 -0
- package/build-module/hooks/use-entity-record.js +18 -7
- package/build-module/hooks/use-entity-record.js.map +1 -1
- package/build-module/hooks/use-entity-records.js +17 -7
- package/build-module/hooks/use-entity-records.js.map +1 -1
- package/build-module/hooks/use-query-select.js.map +1 -1
- package/build-module/index.js +2 -3
- package/build-module/index.js.map +1 -1
- package/build-module/queried-data/selectors.js.map +1 -1
- package/build-module/resolvers.js.map +1 -1
- package/build-module/selectors.js +203 -166
- package/build-module/selectors.js.map +1 -1
- package/build-module/utils/forward-resolver.js.map +1 -1
- package/build-module/utils/on-sub-key.js.map +1 -1
- package/build-module/utils/with-weak-map-cache.js +1 -6
- package/build-module/utils/with-weak-map-cache.js.map +1 -1
- package/package.json +12 -12
- package/src/actions.js +389 -372
- package/src/batch/create-batch.js +2 -2
- package/src/entities.ts +357 -135
- package/src/entity-provider.js +4 -6
- package/src/entity-types/attachment.ts +4 -3
- package/src/entity-types/comment.ts +4 -3
- package/src/entity-types/entities.ts +130 -0
- package/src/entity-types/index.ts +115 -20
- package/src/entity-types/menu-location.ts +4 -3
- package/src/entity-types/nav-menu-item.ts +4 -3
- package/src/entity-types/nav-menu.ts +3 -3
- package/src/entity-types/page.ts +3 -3
- package/src/entity-types/plugin.ts +3 -3
- package/src/entity-types/post.ts +3 -3
- package/src/entity-types/settings.ts +3 -3
- package/src/entity-types/sidebar.ts +4 -3
- package/src/entity-types/taxonomy.ts +4 -3
- package/src/entity-types/theme.ts +3 -3
- package/src/entity-types/type.ts +3 -3
- package/src/entity-types/user.ts +3 -3
- package/src/entity-types/widget-type.ts +4 -3
- package/src/entity-types/widget.ts +3 -3
- package/src/entity-types/wp-template-part.ts +4 -3
- package/src/entity-types/wp-template.ts +4 -3
- package/src/fetch/test/__experimental-fetch-link-suggestions.js +2 -4
- package/src/hooks/constants.ts +1 -2
- package/src/hooks/index.ts +8 -0
- package/src/hooks/test/use-query-select.js +4 -2
- package/src/hooks/use-entity-record.ts +31 -9
- package/src/hooks/use-entity-records.ts +28 -30
- package/src/hooks/use-query-select.ts +26 -24
- package/src/index.js +2 -3
- package/src/locks/test/selectors.js +2 -1
- package/src/queried-data/selectors.js +2 -8
- package/src/resolvers.js +344 -325
- package/src/selectors.ts +639 -296
- package/src/test/resolvers.js +1 -3
- package/src/test/selectors.js +1 -2
- package/src/utils/forward-resolver.js +6 -5
- package/src/utils/on-sub-key.js +20 -20
- package/src/utils/with-weak-map-cache.js +1 -6
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WordPress dependencies
|
|
3
|
+
*/
|
|
4
|
+
import deprecated from '@wordpress/deprecated';
|
|
5
|
+
|
|
1
6
|
/**
|
|
2
7
|
* Internal dependencies
|
|
3
8
|
*/
|
|
@@ -5,7 +10,7 @@ import useQuerySelect from './use-query-select';
|
|
|
5
10
|
import { store as coreStore } from '../';
|
|
6
11
|
import type { Status } from './constants';
|
|
7
12
|
|
|
8
|
-
interface EntityRecordResolution< RecordType > {
|
|
13
|
+
export interface EntityRecordResolution< RecordType > {
|
|
9
14
|
/** The requested entity record */
|
|
10
15
|
record: RecordType | null;
|
|
11
16
|
|
|
@@ -23,18 +28,22 @@ interface EntityRecordResolution< RecordType > {
|
|
|
23
28
|
status: Status;
|
|
24
29
|
}
|
|
25
30
|
|
|
26
|
-
interface Options {
|
|
31
|
+
export interface Options {
|
|
32
|
+
/**
|
|
33
|
+
* Whether to run the query or short-circuit and return null.
|
|
34
|
+
*
|
|
35
|
+
* @default true
|
|
36
|
+
*/
|
|
27
37
|
enabled: boolean;
|
|
28
38
|
}
|
|
29
39
|
|
|
30
40
|
/**
|
|
31
41
|
* Resolves the specified entity record.
|
|
32
42
|
*
|
|
33
|
-
* @param kind
|
|
34
|
-
* @param name
|
|
35
|
-
* @param recordId
|
|
36
|
-
* @param options
|
|
37
|
-
* @param [options.enabled=true] Whether to run the query or short-circuit and return null. Defaults to true.
|
|
43
|
+
* @param kind Kind of the entity, e.g. `root` or a `postType`. See rootEntitiesConfig in ../entities.ts for a list of available kinds.
|
|
44
|
+
* @param name Name of the entity, e.g. `plugin` or a `post`. See rootEntitiesConfig in ../entities.ts for a list of available names.
|
|
45
|
+
* @param recordId ID of the requested entity record.
|
|
46
|
+
* @param options Optional hook options.
|
|
38
47
|
* @example
|
|
39
48
|
* ```js
|
|
40
49
|
* import { useEntityRecord } from '@wordpress/core-data';
|
|
@@ -57,10 +66,10 @@ interface Options {
|
|
|
57
66
|
* application, the page and the resolution details will be retrieved from
|
|
58
67
|
* the store state using `getEntityRecord()`, or resolved if missing.
|
|
59
68
|
*
|
|
60
|
-
* @return
|
|
69
|
+
* @return Entity record data.
|
|
61
70
|
* @template RecordType
|
|
62
71
|
*/
|
|
63
|
-
export default function
|
|
72
|
+
export default function useEntityRecord< RecordType >(
|
|
64
73
|
kind: string,
|
|
65
74
|
name: string,
|
|
66
75
|
recordId: string | number,
|
|
@@ -81,3 +90,16 @@ export default function __experimentalUseEntityRecord< RecordType >(
|
|
|
81
90
|
...rest,
|
|
82
91
|
};
|
|
83
92
|
}
|
|
93
|
+
|
|
94
|
+
export function __experimentalUseEntityRecord(
|
|
95
|
+
kind: string,
|
|
96
|
+
name: string,
|
|
97
|
+
recordId: any,
|
|
98
|
+
options: any
|
|
99
|
+
) {
|
|
100
|
+
deprecated( `wp.data.__experimentalUseEntityRecord`, {
|
|
101
|
+
alternative: 'wp.data.useEntityRecord',
|
|
102
|
+
since: '6.1',
|
|
103
|
+
} );
|
|
104
|
+
return useEntityRecord( kind, name, recordId, options );
|
|
105
|
+
}
|
|
@@ -2,48 +2,32 @@
|
|
|
2
2
|
* WordPress dependencies
|
|
3
3
|
*/
|
|
4
4
|
import { addQueryArgs } from '@wordpress/url';
|
|
5
|
+
import deprecated from '@wordpress/deprecated';
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* Internal dependencies
|
|
8
9
|
*/
|
|
9
10
|
import useQuerySelect from './use-query-select';
|
|
10
11
|
import { store as coreStore } from '../';
|
|
11
|
-
import type {
|
|
12
|
+
import type { Options, EntityRecordResolution } from './use-entity-record';
|
|
12
13
|
|
|
13
|
-
|
|
14
|
+
type EntityRecordsResolution< RecordType > = Omit<
|
|
15
|
+
EntityRecordResolution< RecordType >,
|
|
16
|
+
'record'
|
|
17
|
+
> & {
|
|
14
18
|
/** The requested entity record */
|
|
15
19
|
records: RecordType[] | null;
|
|
20
|
+
};
|
|
16
21
|
|
|
17
|
-
|
|
18
|
-
* Is the record still being resolved?
|
|
19
|
-
*/
|
|
20
|
-
isResolving: boolean;
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Is the record resolved by now?
|
|
24
|
-
*/
|
|
25
|
-
hasResolved: boolean;
|
|
26
|
-
|
|
27
|
-
/** Resolution status */
|
|
28
|
-
status: Status;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
interface Options {
|
|
32
|
-
/**
|
|
33
|
-
* Whether to run the query or short-circuit and return null.
|
|
34
|
-
*
|
|
35
|
-
* @default true
|
|
36
|
-
*/
|
|
37
|
-
enabled: boolean;
|
|
38
|
-
}
|
|
22
|
+
const EMPTY_ARRAY = [];
|
|
39
23
|
|
|
40
24
|
/**
|
|
41
25
|
* Resolves the specified entity records.
|
|
42
26
|
*
|
|
43
|
-
* @param kind Kind of the
|
|
44
|
-
* @param name Name of the
|
|
45
|
-
* @param queryArgs HTTP query for the requested
|
|
46
|
-
* @param options
|
|
27
|
+
* @param kind Kind of the entity, e.g. `root` or a `postType`. See rootEntitiesConfig in ../entities.ts for a list of available kinds.
|
|
28
|
+
* @param name Name of the entity, e.g. `plugin` or a `post`. See rootEntitiesConfig in ../entities.ts for a list of available names.
|
|
29
|
+
* @param queryArgs Optional HTTP query description for how to fetch the data, passed to the requested API endpoint.
|
|
30
|
+
* @param options Optional hook options.
|
|
47
31
|
* @example
|
|
48
32
|
* ```js
|
|
49
33
|
* import { useEntityRecord } from '@wordpress/core-data';
|
|
@@ -75,7 +59,7 @@ interface Options {
|
|
|
75
59
|
* @return Entity records data.
|
|
76
60
|
* @template RecordType
|
|
77
61
|
*/
|
|
78
|
-
export default function
|
|
62
|
+
export default function useEntityRecords< RecordType >(
|
|
79
63
|
kind: string,
|
|
80
64
|
name: string,
|
|
81
65
|
queryArgs: Record< string, unknown > = {},
|
|
@@ -91,7 +75,8 @@ export default function __experimentalUseEntityRecords< RecordType >(
|
|
|
91
75
|
( query ) => {
|
|
92
76
|
if ( ! options.enabled ) {
|
|
93
77
|
return {
|
|
94
|
-
|
|
78
|
+
// Avoiding returning a new reference on every execution.
|
|
79
|
+
data: EMPTY_ARRAY,
|
|
95
80
|
};
|
|
96
81
|
}
|
|
97
82
|
return query( coreStore ).getEntityRecords( kind, name, queryArgs );
|
|
@@ -104,3 +89,16 @@ export default function __experimentalUseEntityRecords< RecordType >(
|
|
|
104
89
|
...rest,
|
|
105
90
|
};
|
|
106
91
|
}
|
|
92
|
+
|
|
93
|
+
export function __experimentalUseEntityRecords(
|
|
94
|
+
kind: string,
|
|
95
|
+
name: string,
|
|
96
|
+
queryArgs: any,
|
|
97
|
+
options: any
|
|
98
|
+
) {
|
|
99
|
+
deprecated( `wp.data.__experimentalUseEntityRecords`, {
|
|
100
|
+
alternative: 'wp.data.useEntityRecords',
|
|
101
|
+
since: '6.1',
|
|
102
|
+
} );
|
|
103
|
+
return useEntityRecords( kind, name, queryArgs, options );
|
|
104
|
+
}
|
|
@@ -102,34 +102,36 @@ const enrichSelectors = memoize( ( ( selectors ) => {
|
|
|
102
102
|
continue;
|
|
103
103
|
}
|
|
104
104
|
Object.defineProperty( resolvers, selectorName, {
|
|
105
|
-
get:
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
105
|
+
get:
|
|
106
|
+
() =>
|
|
107
|
+
( ...args: unknown[] ) => {
|
|
108
|
+
const { getIsResolving, hasFinishedResolution } = selectors;
|
|
109
|
+
const isResolving = !! getIsResolving( selectorName, args );
|
|
110
|
+
const hasResolved =
|
|
111
|
+
! isResolving &&
|
|
112
|
+
hasFinishedResolution( selectorName, args );
|
|
113
|
+
const data = selectors[ selectorName ]( ...args );
|
|
112
114
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
115
|
+
let status;
|
|
116
|
+
if ( isResolving ) {
|
|
117
|
+
status = Status.Resolving;
|
|
118
|
+
} else if ( hasResolved ) {
|
|
119
|
+
if ( data ) {
|
|
120
|
+
status = Status.Success;
|
|
121
|
+
} else {
|
|
122
|
+
status = Status.Error;
|
|
123
|
+
}
|
|
119
124
|
} else {
|
|
120
|
-
status = Status.
|
|
125
|
+
status = Status.Idle;
|
|
121
126
|
}
|
|
122
|
-
} else {
|
|
123
|
-
status = Status.Idle;
|
|
124
|
-
}
|
|
125
127
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
128
|
+
return {
|
|
129
|
+
data,
|
|
130
|
+
status,
|
|
131
|
+
isResolving,
|
|
132
|
+
hasResolved,
|
|
133
|
+
};
|
|
134
|
+
},
|
|
133
135
|
} );
|
|
134
136
|
}
|
|
135
137
|
return resolvers;
|
package/src/index.js
CHANGED
|
@@ -68,8 +68,7 @@ export const store = createReduxStore( STORE_NAME, storeConfig() );
|
|
|
68
68
|
register( store );
|
|
69
69
|
|
|
70
70
|
export { default as EntityProvider } from './entity-provider';
|
|
71
|
-
export { default as __experimentalUseEntityRecord } from './hooks/use-entity-record';
|
|
72
|
-
export { default as __experimentalUseEntityRecords } from './hooks/use-entity-records';
|
|
73
71
|
export * from './entity-provider';
|
|
74
|
-
export * from './fetch';
|
|
75
72
|
export * from './entity-types';
|
|
73
|
+
export * from './fetch';
|
|
74
|
+
export * from './hooks';
|
|
@@ -28,14 +28,8 @@ const queriedItemsCacheByState = new WeakMap();
|
|
|
28
28
|
* @return {?Array} Query items.
|
|
29
29
|
*/
|
|
30
30
|
function getQueriedItemsUncached( state, query ) {
|
|
31
|
-
const {
|
|
32
|
-
|
|
33
|
-
page,
|
|
34
|
-
perPage,
|
|
35
|
-
include,
|
|
36
|
-
fields,
|
|
37
|
-
context,
|
|
38
|
-
} = getQueryParts( query );
|
|
31
|
+
const { stableKey, page, perPage, include, fields, context } =
|
|
32
|
+
getQueryParts( query );
|
|
39
33
|
let itemIds;
|
|
40
34
|
|
|
41
35
|
if ( state.queries?.[ context ]?.[ stableKey ] ) {
|