diff --git a/plugins/woocommerce-admin/client/homescreen/stats-overview/install-jetpack-cta.js b/plugins/woocommerce-admin/client/homescreen/stats-overview/install-jetpack-cta.js index 911388da116..6802d460536 100644 --- a/plugins/woocommerce-admin/client/homescreen/stats-overview/install-jetpack-cta.js +++ b/plugins/woocommerce-admin/client/homescreen/stats-overview/install-jetpack-cta.js @@ -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 diff --git a/plugins/woocommerce-admin/client/profile-wizard/index.js b/plugins/woocommerce-admin/client/profile-wizard/index.js index 82a172a625b..e3c5a472d63 100644 --- a/plugins/woocommerce-admin/client/profile-wizard/index.js +++ b/plugins/woocommerce-admin/client/profile-wizard/index.js @@ -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, diff --git a/plugins/woocommerce-admin/package.json b/plugins/woocommerce-admin/package.json index 40f9c51c4ea..5a7593d89e7 100644 --- a/plugins/woocommerce-admin/package.json +++ b/plugins/woocommerce-admin/package.json @@ -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", diff --git a/plugins/woocommerce-admin/packages/data/src/index.js b/plugins/woocommerce-admin/packages/data/src/index.js index 6ca9ab3c11e..fe347a06d9a 100644 --- a/plugins/woocommerce-admin/packages/data/src/index.js +++ b/plugins/woocommerce-admin/packages/data/src/index.js @@ -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'; diff --git a/plugins/woocommerce-admin/packages/data/src/registry.js b/plugins/woocommerce-admin/packages/data/src/registry.js new file mode 100644 index 00000000000..a9b3ca8f7fd --- /dev/null +++ b/plugins/woocommerce-admin/packages/data/src/registry.js @@ -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 } +);