-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathcontrols.js
73 lines (66 loc) · 1.47 KB
/
controls.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
* WordPress dependencies
*/
import { default as triggerApiFetch } from '@wordpress/api-fetch';
import { createRegistryControl } from '@wordpress/data';
/**
* Trigger an API Fetch request.
*
* @param {Object} request API Fetch Request Object.
* @return {Object} control descriptor.
*/
export function apiFetch( request ) {
return {
type: 'API_FETCH',
request,
};
}
/**
* Calls a selector using the current state.
*
* @param {string} selectorName Selector name.
* @param {Array} args Selector arguments.
*
* @return {Object} control descriptor.
*/
export function select( selectorName, ...args ) {
return {
type: 'SELECT',
selectorName,
args,
};
}
/**
* Dispatches a control action for triggering a registry select that has a
* resolver.
*
* @param {string} selectorName
* @param {Array} args Arguments for the select.
*
* @return {Object} control descriptor.
*/
export function resolveSelect( selectorName, ...args ) {
return {
type: 'RESOLVE_SELECT',
selectorName,
args,
};
}
const controls = {
API_FETCH( { request } ) {
return triggerApiFetch( request );
},
SELECT: createRegistryControl(
( registry ) => ( { selectorName, args } ) => {
return registry.select( 'core' )[ selectorName ]( ...args );
}
),
RESOLVE_SELECT: createRegistryControl(
( registry ) => ( { selectorName, args } ) => {
return registry
.__experimentalResolveSelect( 'core' )
[ selectorName ]( ...args );
}
),
};
export default controls;