# learnpress/4.2.0/assets/src/apps/js/data-controls.js

LearnPress – WordPress LMS Plugin for Create and Sell Online Courses, version 4.2.0. 74 lines.

- Page: https://pluginprobe.com/plugins/learnpress/4.2.0/code/assets/src/apps/js/data-controls.js
- Raw: https://pluginprobe.com/plugins/learnpress/4.2.0/raw/assets/src/apps/js/data-controls.js
- Modified: 2022-11-14T06:50:54+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/learnpress/4.2.0/code/assets/src/apps/js/data-controls.js#L10-L20`.

```javascript
import triggerFetch from '@wordpress/api-fetch';

const createRegistryControl = function createRegistryControl( registryControl ) {
	registryControl.isRegistryControl = true;

	return registryControl;
};

export const apiFetch = ( request ) => {
	return {
		type: 'API_FETCH',
		request,
	};
};

export function select( storeKey, selectorName, ...args ) {
	return {
		type: 'SELECT',
		storeKey,
		selectorName,
		args,
	};
}

export function dispatch( storeKey, actionName, ...args ) {
	return {
		type: 'DISPATCH',
		storeKey,
		actionName,
		args,
	};
}

const resolveSelect = ( registry, { storeKey, selectorName, args } ) => {
	return new Promise( ( resolve ) => {
		const hasFinished = () => registry.select( '' )
			.hasFinishedResolution( storeKey, selectorName, args );

		const getResult = () => registry.select( storeKey )[ selectorName ]
			.apply( null, args );

		const result = getResult();

		if ( hasFinished() ) {
			return resolve( result );
		}

		const unsubscribe = registry.subscribe( () => {
			if ( hasFinished() ) {
				unsubscribe();
				resolve( getResult() );
			}
		} );
	} );
};

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 );
		}
	),
};

```
