UNPKG

@automattic/shopping-cart

Version:
41 lines 1.91 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createTakeActionsBasedOnState = exports.prepareFreshCartForInitialFetch = void 0; const tslib_1 = require("tslib"); const debug_1 = tslib_1.__importDefault(require("debug")); const debug = (0, debug_1.default)('shopping-cart:state-based-actions'); function prepareFreshCartForInitialFetch(state, dispatch, syncManager) { const { cacheStatus } = state; if (cacheStatus === 'fresh') { debug('triggering fetch of initial cart'); dispatch({ type: 'FETCH_INITIAL_RESPONSE_CART' }); syncManager.fetchInitialCartFromServer(dispatch); } } exports.prepareFreshCartForInitialFetch = prepareFreshCartForInitialFetch; function prepareInvalidCartForSync(state, dispatch, syncManager) { const { queuedActions, cacheStatus } = state; if (queuedActions.length === 0 && cacheStatus === 'invalid') { debug('triggering sync of cart to server'); dispatch({ type: 'REQUEST_UPDATED_RESPONSE_CART' }); syncManager.syncPendingCartToServer(state, dispatch); } } function createTakeActionsBasedOnState(syncManager) { // We defer the state based actions so that multiple cart changes can be // batched together during the same run of the event loop. let deferredStateCheck; return (state, dispatch) => { if (deferredStateCheck) { clearTimeout(deferredStateCheck); } deferredStateCheck = setTimeout(() => { debug('cache status before state-based-actions is', state.cacheStatus); prepareFreshCartForInitialFetch(state, dispatch, syncManager); prepareInvalidCartForSync(state, dispatch, syncManager); debug('running state-based-actions complete'); }); }; } exports.createTakeActionsBasedOnState = createTakeActionsBasedOnState; //# sourceMappingURL=state-based-actions.js.map