@wordpress/data
Version:
Data module for WordPress.
366 lines (329 loc) • 12.2 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "AsyncModeProvider", {
enumerable: true,
get: function () {
return _asyncModeProvider.AsyncModeProvider;
}
});
Object.defineProperty(exports, "RegistryConsumer", {
enumerable: true,
get: function () {
return _registryProvider.RegistryConsumer;
}
});
Object.defineProperty(exports, "RegistryProvider", {
enumerable: true,
get: function () {
return _registryProvider.RegistryProvider;
}
});
exports.combineReducers = void 0;
Object.defineProperty(exports, "controls", {
enumerable: true,
get: function () {
return _controls.controls;
}
});
Object.defineProperty(exports, "createReduxStore", {
enumerable: true,
get: function () {
return _reduxStore.default;
}
});
Object.defineProperty(exports, "createRegistry", {
enumerable: true,
get: function () {
return _registry.createRegistry;
}
});
Object.defineProperty(exports, "createRegistryControl", {
enumerable: true,
get: function () {
return _factory.createRegistryControl;
}
});
Object.defineProperty(exports, "createRegistrySelector", {
enumerable: true,
get: function () {
return _factory.createRegistrySelector;
}
});
exports.dispatch = dispatch;
exports.resolveSelect = exports.registerStore = exports.registerGenericStore = exports.register = exports.plugins = void 0;
exports.select = select;
exports.use = exports.suspendSelect = exports.subscribe = void 0;
Object.defineProperty(exports, "useDispatch", {
enumerable: true,
get: function () {
return _useDispatch.useDispatch;
}
});
Object.defineProperty(exports, "useRegistry", {
enumerable: true,
get: function () {
return _registryProvider.useRegistry;
}
});
Object.defineProperty(exports, "useSelect", {
enumerable: true,
get: function () {
return _useSelect.default;
}
});
Object.defineProperty(exports, "useSuspenseSelect", {
enumerable: true,
get: function () {
return _useSelect.useSuspenseSelect;
}
});
Object.defineProperty(exports, "withDispatch", {
enumerable: true,
get: function () {
return _withDispatch.default;
}
});
Object.defineProperty(exports, "withRegistry", {
enumerable: true,
get: function () {
return _withRegistry.default;
}
});
Object.defineProperty(exports, "withSelect", {
enumerable: true,
get: function () {
return _withSelect.default;
}
});
var _turboCombineReducers = _interopRequireDefault(require("turbo-combine-reducers"));
var _defaultRegistry = _interopRequireDefault(require("./default-registry"));
var plugins = _interopRequireWildcard(require("./plugins"));
exports.plugins = plugins;
var _withSelect = _interopRequireDefault(require("./components/with-select"));
var _withDispatch = _interopRequireDefault(require("./components/with-dispatch"));
var _withRegistry = _interopRequireDefault(require("./components/with-registry"));
var _registryProvider = require("./components/registry-provider");
var _useSelect = _interopRequireWildcard(require("./components/use-select"));
var _useDispatch = require("./components/use-dispatch");
var _asyncModeProvider = require("./components/async-mode-provider");
var _registry = require("./registry");
var _factory = require("./factory");
var _controls = require("./controls");
var _reduxStore = _interopRequireDefault(require("./redux-store"));
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
/**
* External dependencies
*/
/**
* Internal dependencies
*/
/** @typedef {import('./types').StoreDescriptor} StoreDescriptor */
/**
* Object of available plugins to use with a registry.
*
* @see [use](#use)
*
* @type {Object}
*/
/**
* The combineReducers helper function turns an object whose values are different
* reducing functions into a single reducing function you can pass to registerReducer.
*
* @type {import('./types').combineReducers}
* @param {Object} reducers An object whose values correspond to different reducing
* functions that need to be combined into one.
*
* @example
* ```js
* import { combineReducers, createReduxStore, register } from '@wordpress/data';
*
* const prices = ( state = {}, action ) => {
* return action.type === 'SET_PRICE' ?
* {
* ...state,
* [ action.item ]: action.price,
* } :
* state;
* };
*
* const discountPercent = ( state = 0, action ) => {
* return action.type === 'START_SALE' ?
* action.discountPercent :
* state;
* };
*
* const store = createReduxStore( 'my-shop', {
* reducer: combineReducers( {
* prices,
* discountPercent,
* } ),
* } );
* register( store );
* ```
*
* @return {Function} A reducer that invokes every reducer inside the reducers
* object, and constructs a state object with the same shape.
*/
const combineReducers = _turboCombineReducers.default;
/**
* Given a store descriptor, returns an object of the store's selectors.
* The selector functions are been pre-bound to pass the current state automatically.
* As a consumer, you need only pass arguments of the selector, if applicable.
*
* @param {StoreDescriptor|string} storeNameOrDescriptor The store descriptor. The legacy calling
* convention of passing the store name is
* also supported.
*
* @example
* ```js
* import { select } from '@wordpress/data';
* import { store as myCustomStore } from 'my-custom-store';
*
* select( myCustomStore ).getPrice( 'hammer' );
* ```
*
* @return {Object} Object containing the store's selectors.
*/
exports.combineReducers = combineReducers;
function select(storeNameOrDescriptor) {
return _defaultRegistry.default.select(storeNameOrDescriptor);
}
/**
* Given a store descriptor, returns an object containing the store's selectors pre-bound to state
* so that you only need to supply additional arguments, and modified so that they return promises
* that resolve to their eventual values, after any resolvers have ran.
*
* @param {StoreDescriptor|string} storeNameOrDescriptor The store descriptor. The legacy calling
* convention of passing the store name is
* also supported.
*
* @example
* ```js
* import { resolveSelect } from '@wordpress/data';
* import { store as myCustomStore } from 'my-custom-store';
*
* resolveSelect( myCustomStore ).getPrice( 'hammer' ).then(console.log)
* ```
*
* @return {Object} Object containing the store's promise-wrapped selectors.
*/
const resolveSelect = _defaultRegistry.default.resolveSelect;
/**
* Given a store descriptor, returns an object containing the store's selectors pre-bound to state
* so that you only need to supply additional arguments, and modified so that they throw promises
* in case the selector is not resolved yet.
*
* @param {StoreDescriptor|string} storeNameOrDescriptor The store descriptor. The legacy calling
* convention of passing the store name is
* also supported.
*
* @return {Object} Object containing the store's suspense-wrapped selectors.
*/
exports.resolveSelect = resolveSelect;
const suspendSelect = _defaultRegistry.default.suspendSelect;
/**
* Given a store descriptor, returns an object of the store's action creators.
* Calling an action creator will cause it to be dispatched, updating the state value accordingly.
*
* Note: Action creators returned by the dispatch will return a promise when
* they are called.
*
* @param {StoreDescriptor|string} storeNameOrDescriptor The store descriptor. The legacy calling
* convention of passing the store name is
* also supported.
*
* @example
* ```js
* import { dispatch } from '@wordpress/data';
* import { store as myCustomStore } from 'my-custom-store';
*
* dispatch( myCustomStore ).setPrice( 'hammer', 9.75 );
* ```
* @return {Object} Object containing the action creators.
*/
exports.suspendSelect = suspendSelect;
function dispatch(storeNameOrDescriptor) {
return _defaultRegistry.default.dispatch(storeNameOrDescriptor);
}
/**
* Given a listener function, the function will be called any time the state value
* of one of the registered stores has changed. If you specify the optional
* `storeNameOrDescriptor` parameter, the listener function will be called only
* on updates on that one specific registered store.
*
* This function returns an `unsubscribe` function used to stop the subscription.
*
* @param {Function} listener Callback function.
* @param {string|StoreDescriptor?} storeNameOrDescriptor Optional store name.
*
* @example
* ```js
* import { subscribe } from '@wordpress/data';
*
* const unsubscribe = subscribe( () => {
* // You could use this opportunity to test whether the derived result of a
* // selector has subsequently changed as the result of a state update.
* } );
*
* // Later, if necessary...
* unsubscribe();
* ```
*/
const subscribe = _defaultRegistry.default.subscribe;
/**
* Registers a generic store instance.
*
* @deprecated Use `register( storeDescriptor )` instead.
*
* @param {string} name Store registry name.
* @param {Object} store Store instance (`{ getSelectors, getActions, subscribe }`).
*/
exports.subscribe = subscribe;
const registerGenericStore = _defaultRegistry.default.registerGenericStore;
/**
* Registers a standard `@wordpress/data` store.
*
* @deprecated Use `register` instead.
*
* @param {string} storeName Unique namespace identifier for the store.
* @param {Object} options Store description (reducer, actions, selectors, resolvers).
*
* @return {Object} Registered store object.
*/
exports.registerGenericStore = registerGenericStore;
const registerStore = _defaultRegistry.default.registerStore;
/**
* Extends a registry to inherit functionality provided by a given plugin. A
* plugin is an object with properties aligning to that of a registry, merged
* to extend the default registry behavior.
*
* @param {Object} plugin Plugin object.
*/
exports.registerStore = registerStore;
const use = _defaultRegistry.default.use;
/**
* Registers a standard `@wordpress/data` store descriptor.
*
* @example
* ```js
* import { createReduxStore, register } from '@wordpress/data';
*
* const store = createReduxStore( 'demo', {
* reducer: ( state = 'OK' ) => state,
* selectors: {
* getValue: ( state ) => state,
* },
* } );
* register( store );
* ```
*
* @param {StoreDescriptor} store Store descriptor.
*/
exports.use = use;
const register = _defaultRegistry.default.register;
exports.register = register;
//# sourceMappingURL=index.js.map