@wordpress/data-controls
Version:
A set of common controls for the @wordpress/data api.
160 lines (153 loc) • 4.73 kB
JavaScript
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.__unstableAwaitPromise = void 0;
exports.apiFetch = apiFetch;
exports.controls = void 0;
exports.dispatch = dispatch;
exports.select = select;
exports.syncSelect = syncSelect;
var _apiFetch = _interopRequireDefault(require("@wordpress/api-fetch"));
var _data = require("@wordpress/data");
var _deprecated = _interopRequireDefault(require("@wordpress/deprecated"));
/**
* WordPress dependencies
*/
/**
* Dispatches a control action for triggering an api fetch call.
*
* @param {Object} request Arguments for the fetch request.
*
* @example
* ```js
* import { apiFetch } from '@wordpress/data-controls';
*
* // Action generator using apiFetch
* export function* myAction() {
* const path = '/v2/my-api/items';
* const items = yield apiFetch( { path } );
* // do something with the items.
* }
* ```
*
* @return {Object} The control descriptor.
*/
function apiFetch(request) {
return {
type: 'API_FETCH',
request
};
}
/**
* Control for resolving a selector in a registered data store.
* Alias for the `resolveSelect` built-in control in the `@wordpress/data` package.
*
* @param storeNameOrDescriptor The store object or identifier.
* @param selectorName The selector name.
* @param args Arguments passed without change to the `@wordpress/data` control.
*/
function select(storeNameOrDescriptor, selectorName, ...args) {
(0, _deprecated.default)('`select` control in `@wordpress/data-controls`', {
since: '5.7',
alternative: 'built-in `resolveSelect` control in `@wordpress/data`'
});
return _data.controls.resolveSelect(storeNameOrDescriptor, selectorName, ...args);
}
/**
* Control for calling a selector in a registered data store.
* Alias for the `select` built-in control in the `@wordpress/data` package.
*
* @param storeNameOrDescriptor The store object or identifier.
* @param selectorName The selector name.
* @param args Arguments passed without change to the `@wordpress/data` control.
*/
function syncSelect(storeNameOrDescriptor, selectorName, ...args) {
(0, _deprecated.default)('`syncSelect` control in `@wordpress/data-controls`', {
since: '5.7',
alternative: 'built-in `select` control in `@wordpress/data`'
});
return _data.controls.select(storeNameOrDescriptor, selectorName, ...args);
}
/**
* Control for dispatching an action in a registered data store.
* Alias for the `dispatch` control in the `@wordpress/data` package.
*
* @param storeNameOrDescriptor The store object or identifier.
* @param actionName The action name.
* @param args Arguments passed without change to the `@wordpress/data` control.
*/
function dispatch(storeNameOrDescriptor, actionName, ...args) {
(0, _deprecated.default)('`dispatch` control in `@wordpress/data-controls`', {
since: '5.7',
alternative: 'built-in `dispatch` control in `@wordpress/data`'
});
return _data.controls.dispatch(storeNameOrDescriptor, actionName, ...args);
}
/**
* Dispatches a control action for awaiting on a promise to be resolved.
*
* @param {Object} promise Promise to wait for.
*
* @example
* ```js
* import { __unstableAwaitPromise } from '@wordpress/data-controls';
*
* // Action generator using apiFetch
* export function* myAction() {
* const promise = getItemsAsync();
* const items = yield __unstableAwaitPromise( promise );
* // do something with the items.
* }
* ```
*
* @return {Object} The control descriptor.
*/
const __unstableAwaitPromise = function (promise) {
return {
type: 'AWAIT_PROMISE',
promise
};
};
/**
* The default export is what you use to register the controls with your custom
* store.
*
* @example
* ```js
* // WordPress dependencies
* import { controls } from '@wordpress/data-controls';
* import { registerStore } from '@wordpress/data';
*
* // Internal dependencies
* import reducer from './reducer';
* import * as selectors from './selectors';
* import * as actions from './actions';
* import * as resolvers from './resolvers';
*
* registerStore( 'my-custom-store', {
* reducer,
* controls,
* actions,
* selectors,
* resolvers,
* } );
* ```
* @return {Object} An object for registering the default controls with the
* store.
*/
exports.__unstableAwaitPromise = __unstableAwaitPromise;
const controls = exports.controls = {
AWAIT_PROMISE({
promise
}) {
return promise;
},
API_FETCH({
request
}) {
return (0, _apiFetch.default)(request);
}
};
//# sourceMappingURL=index.js.map
;