How to use the @wordpress/data.createRegistryControl function in @wordpress/data

To help you get started, we’ve selected a few @wordpress/data examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github WordPress / gutenberg / packages / core-data / src / controls.js View on Github external
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 new Promise( ( resolve ) => {
				const hasFinished = () => registry.select( 'core/data' )
					.hasFinishedResolution( 'core', selectorName, args );
				const getResult = () => registry.select( 'core' )[ selectorName ]
					.apply( null, args );

				// trigger the selector (to trigger the resolver)
				const result = getResult();
				if ( hasFinished() ) {
					return resolve( result );
				}

				const unsubscribe = registry.subscribe( () => {
					if ( hasFinished() ) {
						unsubscribe();
github WordPress / gutenberg / packages / core-data / src / controls.js View on Github external
* @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 new Promise( ( resolve ) => {
				const hasFinished = () => registry.select( 'core/data' )
					.hasFinishedResolution( 'core', selectorName, args );
				const getResult = () => registry.select( 'core' )[ selectorName ]
					.apply( null, args );

				// trigger the selector (to trigger the resolver)
				const result = getResult();
				if ( hasFinished() ) {
					return resolve( result );
				}
github WordPress / gutenberg / packages / data-controls / src / index.js View on Github external
* 	reducer,
 * 	controls,
 * 	actions,
 * 	selectors,
 * 	resolvers,
 * } );
 * ```
 *
 * @return {Object} An object for registering the default controls with the
 *                  store.
 */
export const controls = {
	API_FETCH( { request } ) {
		return triggerFetch( request );
	},
	SELECT: createRegistryControl(
		( registry ) => ( { storeKey, selectorName, args } ) => {
			return registry.select( storeKey )[ selectorName ].hasResolver ?
				resolveSelect( registry, { storeKey, selectorName, args } ) :
				registry.select( storeKey )[ selectorName ]( ...args );
		}
	),
	DISPATCH: createRegistryControl(
		( registry ) => ( { storeKey, actionName, args } ) => {
			return registry.dispatch( storeKey )[ actionName ]( ...args );
		}
	),
};
github WordPress / gutenberg / packages / edit-post / src / store / controls.js View on Github external
* @param {string} selectorName Selector name.
 * @param  {Array} args         Selector arguments.
 *
 * @return {Object} control descriptor.
 */
export function select( storeName, selectorName, ...args ) {
	return {
		type: 'SELECT',
		storeName,
		selectorName,
		args,
	};
}

const controls = {
	SELECT: createRegistryControl(
		( registry ) => ( { storeName, selectorName, args } ) => {
			return registry.select( storeName )[ selectorName ]( ...args );
		}
	),
};

export default controls;
github WordPress / gutenberg / packages / data-controls / src / index.js View on Github external
*
 * @return {Object} An object for registering the default controls with the
 *                  store.
 */
export const controls = {
	API_FETCH( { request } ) {
		return triggerFetch( request );
	},
	SELECT: createRegistryControl(
		( registry ) => ( { storeKey, selectorName, args } ) => {
			return registry.select( storeKey )[ selectorName ].hasResolver ?
				resolveSelect( registry, { storeKey, selectorName, args } ) :
				registry.select( storeKey )[ selectorName ]( ...args );
		}
	),
	DISPATCH: createRegistryControl(
		( registry ) => ( { storeKey, actionName, args } ) => {
			return registry.dispatch( storeKey )[ actionName ]( ...args );
		}
	),
};
github WordPress / gutenberg / packages / block-directory / src / store / controls.js View on Github external
* @param {Array} assets A collection of URL for the assets.
 *
 * @return {Object} Control descriptor.
 */
export function* loadAssets( assets ) {
	return {
		type: 'LOAD_ASSETS',
		assets,
	};
}

const controls = {
	SELECT: createRegistryControl( ( registry ) => ( { storeName, selectorName, args } ) => {
		return registry.select( storeName )[ selectorName ]( ...args );
	} ),
	DISPATCH: createRegistryControl( ( registry ) => ( { storeName, dispatcherName, args } ) => {
		return registry.dispatch( storeName )[ dispatcherName ]( ...args );
	} ),
	API_FETCH( { request } ) {
		return wpApiFetch( { ... request } );
	},
	LOAD_ASSETS( { assets } ) {
		return new Promise( ( resolve, reject ) => {
			if ( Array.isArray( assets ) ) {
				let scriptsCount = 0;

				forEach( assets, ( asset ) => {
					if ( asset.match( /\.js$/ ) !== null ) {
						scriptsCount++;
						loadScript( asset, () => {
							scriptsCount--;
							if ( scriptsCount === 0 ) {
github WordPress / gutenberg / packages / block-directory / src / store / controls.js View on Github external
/**
 * Load the asset files for a block
 *
 * @param {Array} assets A collection of URL for the assets.
 *
 * @return {Object} Control descriptor.
 */
export function* loadAssets( assets ) {
	return {
		type: 'LOAD_ASSETS',
		assets,
	};
}

const controls = {
	SELECT: createRegistryControl( ( registry ) => ( { storeName, selectorName, args } ) => {
		return registry.select( storeName )[ selectorName ]( ...args );
	} ),
	DISPATCH: createRegistryControl( ( registry ) => ( { storeName, dispatcherName, args } ) => {
		return registry.dispatch( storeName )[ dispatcherName ]( ...args );
	} ),
	API_FETCH( { request } ) {
		return wpApiFetch( { ... request } );
	},
	LOAD_ASSETS( { assets } ) {
		return new Promise( ( resolve, reject ) => {
			if ( Array.isArray( assets ) ) {
				let scriptsCount = 0;

				forEach( assets, ( asset ) => {
					if ( asset.match( /\.js$/ ) !== null ) {
						scriptsCount++;