Add the experimental resolver to WCA data package (https://github.com/woocommerce/woocommerce-admin/pull/4862)

* Add the experimental resolver to WCA data package

* Add note about function origin and deprecation
This commit is contained in:
Joshua T Flowers 2020-07-28 03:07:39 +03:00 committed by GitHub
parent ac91a8fd60
commit fe4fcf2467
5 changed files with 66 additions and 7 deletions

View File

@ -6,18 +6,18 @@ import { compose } from '@wordpress/compose';
import { Button } from '@wordpress/components';
import { useState } from 'react';
import PropTypes from 'prop-types';
import {
withDispatch,
withSelect,
__experimentalResolveSelect,
} from '@wordpress/data';
import { withDispatch, withSelect } from '@wordpress/data';
/**
* WooCommerce dependencies
*/
import {
__experimentalResolveSelect,
PLUGINS_STORE_NAME,
useUserPreferences,
} from '@woocommerce/data';
import { H } from '@woocommerce/components';
import { getAdminLink } from '@woocommerce/wc-admin-settings';
import { PLUGINS_STORE_NAME, useUserPreferences } from '@woocommerce/data';
/**
* Internal dependencies

View File

@ -5,7 +5,7 @@ import { __ } from '@wordpress/i18n';
import { Component, createElement, Fragment } from '@wordpress/element';
import { compose } from '@wordpress/compose';
import { identity, pick } from 'lodash';
import { withDispatch, __experimentalResolveSelect } from '@wordpress/data';
import { withDispatch } from '@wordpress/data';
/**
* WooCommerce dependencies
@ -17,6 +17,7 @@ import {
updateQueryString,
} from '@woocommerce/navigation';
import {
__experimentalResolveSelect,
ONBOARDING_STORE_NAME,
OPTIONS_STORE_NAME,
PLUGINS_STORE_NAME,

View File

@ -127,6 +127,7 @@
"history": "4.10.1",
"interpolate-components": "1.1.1",
"marked": "0.8.2",
"memize": "^1.1.0",
"memoize-one": "5.1.1",
"prismjs": "1.20.0",
"qs": "6.9.3",

View File

@ -15,3 +15,5 @@ export { useUserPreferences } from './user-preferences/use-user-preferences';
export { OPTIONS_STORE_NAME } from './options';
export { withOptionsHydration } from './options/with-options-hydration';
export { __experimentalResolveSelect } from './registry';

View File

@ -0,0 +1,55 @@
/**
* External dependencies
*/
import { omit, mapValues } from 'lodash';
import memize from 'memize';
import { select, subscribe } from '@wordpress/data';
export function __experimentalResolveSelect( reducerKey ) {
return getResolveSelectors( select( reducerKey ) );
}
/**
* Returns a promise that resolves once a selector has finished resolving.
* This is directly pulled from https://github.com/WordPress/gutenberg/blob/909c9274b2440de5f6049ffddfcc8e0e6158df2d/packages/data/src/registry.js#L91-L130
* and will be removed in favor of the @wordpress/data function.
*/
const getResolveSelectors = memize(
( selectors ) => {
return mapValues(
omit( selectors, [
'getIsResolving',
'hasStartedResolution',
'hasFinishedResolution',
'isResolving',
'getCachedResolvers',
] ),
( selector, selectorName ) => {
return ( ...args ) => {
return new Promise( ( resolve ) => {
const hasFinished = () =>
selectors.hasFinishedResolution(
selectorName,
args
);
const getResult = () => selector.apply( null, args );
// trigger the selector (to trigger the resolver)
const result = getResult();
if ( hasFinished() ) {
return resolve( result );
}
const unsubscribe = subscribe( () => {
if ( hasFinished() ) {
unsubscribe();
resolve( getResult() );
}
} );
} );
};
}
);
},
{ maxSize: 1 }
);