react-query#focusManager TypeScript Examples

The following examples show how to use react-query#focusManager. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: index.ts    From anchor-web-app with Apache License 2.0 6 votes vote down vote up
/**
 * change the behavior of refetch data after user's inactive
 *
 * the default react-query refetch strategy uses visibilitychange and focus events.
 * it make too many data fetching.
 * this patch will decrease the data fetching.
 *
 * @see https://caniuse.com/mdn-api_document_visibilitychange_event
 */
export function patchReactQueryFocusRefetching(
  refetchInactiveTime: number = 1000 * 60,
) {
  focusManager.setEventListener((handleFocus) => {
    let lastInvisibleTime = -1;

    function onVisibilityChange() {
      if (document.hidden) {
        lastInvisibleTime = Date.now();
      } else if (lastInvisibleTime > 0) {
        const t = Date.now() - lastInvisibleTime;
        if (t > refetchInactiveTime) {
          handleFocus(true);
        }
        lastInvisibleTime = -1;
      }
    }

    if (typeof window !== 'undefined' && window.addEventListener) {
      document.addEventListener('visibilitychange', onVisibilityChange, false);
    }

    return () => {
      document.removeEventListener('visibilitychange', onVisibilityChange);
    };
  });
}