UNPKG

@automattic/shopping-cart

Version:
85 lines 3.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const debug_1 = tslib_1.__importDefault(require("debug")); const react_1 = require("react"); const cart_keys_1 = require("./cart-keys"); const shopping_cart_options_context_1 = tslib_1.__importDefault(require("./shopping-cart-options-context")); const use_manager_client_1 = tslib_1.__importDefault(require("./use-manager-client")); const debug = (0, debug_1.default)('shopping-cart:use-refetch-on-focus'); // The number of seconds within that must have passed before we allow an // automatic refetch on focus. const minimumFetchInterval = 60; function convertMsToSecs(ms) { return Math.floor(ms / 1000); } function isFocused() { return [undefined, 'visible', 'prerender'].includes(document.visibilityState); } function isOffline() { try { return !window.navigator.onLine; } catch (err) { debug('failed to check onLine status; ignoring check', err); return false; } } function useRefetchOnFocus(cartKey) { const managerClient = (0, use_manager_client_1.default)('useRefetchOnFocus'); const manager = managerClient.forCartKey(cartKey); const { refetchOnWindowFocus } = (0, react_1.useContext)(shopping_cart_options_context_1.default); (0, react_1.useEffect)(() => { if (!refetchOnWindowFocus) { debug('refetchOnWindowFocus false; not listening'); return; } if (!cartKey) { debug('cartKey falsy; not listening'); return; } if (cart_keys_1.cartKeysThatDoNotAllowFetch.includes(cartKey)) { debug('cartKey not fetchable; not listening'); return; } function wasLastFetchRecent() { const nowInSeconds = convertMsToSecs(Date.now()); const { responseCart: lastCart } = manager.getState(); const lastRefreshTime = lastCart.cart_generated_at_timestamp; const secondsSinceLastFetch = nowInSeconds - lastRefreshTime; debug('last fetch was', secondsSinceLastFetch, 'seconds ago'); return secondsSinceLastFetch < minimumFetchInterval; } function handleFocusChange() { if (!isFocused()) { debug('window was made invisible; ignoring'); return; } if (wasLastFetchRecent()) { debug('last fetch was quite recent; ignoring'); return; } if (isOffline()) { debug('network is offline; ignoring'); return; } debug('window was refocused; refetching'); manager.actions.reloadFromServer().catch(() => { // No need to do anything here. Errors will be surfaced by the cart // accessors manually. }); } debug('adding focus listeners'); window.addEventListener('visibilitychange', handleFocusChange); window.addEventListener('focus', handleFocusChange); window.addEventListener('online', handleFocusChange); return () => { debug('removing focus listeners'); window.removeEventListener('visibilitychange', handleFocusChange); window.removeEventListener('focus', handleFocusChange); window.removeEventListener('online', handleFocusChange); }; }, [cartKey, refetchOnWindowFocus, manager]); } exports.default = useRefetchOnFocus; //# sourceMappingURL=use-refetch-on-focus.js.map