2020-02-25 11:36:53 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2022-10-25 13:21:11 +00:00
|
|
|
import { registerStore } from '@wordpress/data';
|
2020-03-19 11:50:51 +00:00
|
|
|
import { controls as dataControls } from '@wordpress/data-controls';
|
2021-12-07 15:47:50 +00:00
|
|
|
|
2020-02-25 11:36:53 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { STORE_KEY } from './constants';
|
|
|
|
import * as selectors from './selectors';
|
|
|
|
import * as actions from './actions';
|
|
|
|
import * as resolvers from './resolvers';
|
2021-03-01 17:47:22 +00:00
|
|
|
import reducer, { State } from './reducers';
|
2021-06-04 08:17:13 +00:00
|
|
|
import { controls as sharedControls } from '../shared-controls';
|
|
|
|
import { controls } from './controls';
|
2021-12-07 15:47:50 +00:00
|
|
|
import type { SelectFromMap, DispatchFromMap } from '../mapped-types';
|
2022-02-22 17:45:01 +00:00
|
|
|
import { pushChanges } from './push-changes';
|
2022-11-07 10:48:27 +00:00
|
|
|
import {
|
|
|
|
updatePaymentMethods,
|
|
|
|
debouncedUpdatePaymentMethods,
|
|
|
|
} from './update-payment-methods';
|
2020-02-25 11:36:53 +00:00
|
|
|
|
2022-11-10 15:37:01 +00:00
|
|
|
// Please update from deprecated "registerStore" to "createReduxStore" when this PR is merged:
|
|
|
|
// https://github.com/WordPress/gutenberg/pull/45513
|
2022-02-22 17:45:01 +00:00
|
|
|
const registeredStore = registerStore< State >( STORE_KEY, {
|
2020-02-25 11:36:53 +00:00
|
|
|
reducer,
|
|
|
|
actions,
|
2021-03-01 17:47:22 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2021-06-04 08:17:13 +00:00
|
|
|
controls: { ...dataControls, ...sharedControls, ...controls } as any,
|
2020-02-25 11:36:53 +00:00
|
|
|
selectors,
|
|
|
|
resolvers,
|
|
|
|
} );
|
|
|
|
|
2022-02-22 17:45:01 +00:00
|
|
|
registeredStore.subscribe( pushChanges );
|
2022-11-07 10:48:27 +00:00
|
|
|
|
|
|
|
// First we will run the updatePaymentMethods function without any debounce to ensure payment methods are ready as soon
|
|
|
|
// as the cart is loaded. After that, we will unsubscribe this function and instead run the
|
|
|
|
// debouncedUpdatePaymentMethods function on subsequent cart updates.
|
|
|
|
const unsubscribeUpdatePaymentMethods = registeredStore.subscribe( async () => {
|
|
|
|
const didActionDispatch = await updatePaymentMethods();
|
|
|
|
if ( didActionDispatch ) {
|
|
|
|
// The function we're currently in will unsubscribe itself. When we reach this line, this will be the last time
|
|
|
|
// this function is called.
|
|
|
|
unsubscribeUpdatePaymentMethods();
|
|
|
|
// Resubscribe, but with the debounced version of updatePaymentMethods.
|
|
|
|
registeredStore.subscribe( debouncedUpdatePaymentMethods );
|
|
|
|
}
|
|
|
|
} );
|
2022-08-22 13:56:44 +00:00
|
|
|
|
2020-02-25 11:36:53 +00:00
|
|
|
export const CART_STORE_KEY = STORE_KEY;
|
2021-03-01 17:47:22 +00:00
|
|
|
|
|
|
|
declare module '@wordpress/data' {
|
|
|
|
function dispatch(
|
|
|
|
key: typeof CART_STORE_KEY
|
|
|
|
): DispatchFromMap< typeof actions >;
|
2022-06-15 09:56:52 +00:00
|
|
|
function select( key: typeof CART_STORE_KEY ): SelectFromMap<
|
|
|
|
typeof selectors
|
|
|
|
> & {
|
2022-02-22 17:45:01 +00:00
|
|
|
hasFinishedResolution: ( selector: string ) => boolean;
|
|
|
|
};
|
2021-03-01 17:47:22 +00:00
|
|
|
}
|