11import { pipe , tap } from 'wonka' ;
22import type { Exchange , Operation } from '@urql/core' ;
33
4+ export interface RefocusOptions {
5+ /** The minimum time in milliseconds to wait before another refocus can trigger.
6+ * @defaultValue `0`
7+ */
8+ minimumTime ?: number ;
9+ }
10+
411/** Exchange factory that reexecutes operations after a user returns to the tab.
12+ *
13+ * @param opts - A {@link RefocusOptions} configuration object.
514 *
615 * @returns a new refocus {@link Exchange}.
716 *
@@ -14,7 +23,9 @@ import type { Exchange, Operation } from '@urql/core';
1423 * The `cache-and-network` policy will refetch data in the background, but will
1524 * only refetch queries that are currently active.
1625 */
17- export const refocusExchange = ( ) : Exchange => {
26+ export const refocusExchange = ( opts : RefocusOptions = { } ) : Exchange => {
27+ const { minimumTime = 0 } = opts ;
28+
1829 return ( { client, forward } ) =>
1930 ops$ => {
2031 if ( typeof window === 'undefined' ) {
@@ -24,11 +35,13 @@ export const refocusExchange = (): Exchange => {
2435 const watchedOperations = new Map < number , Operation > ( ) ;
2536 const observedOperations = new Map < number , number > ( ) ;
2637
38+ let lastHidden = 0 ;
39+
2740 window . addEventListener ( 'visibilitychange' , ( ) => {
28- if (
29- typeof document !== 'object' ||
30- document . visibilityState === 'visible'
31- ) {
41+ const state =
42+ typeof document !== 'object' ? 'visible' : document . visibilityState ;
43+ if ( state === 'visible' ) {
44+ if ( Date . now ( ) - lastHidden < minimumTime ) return ;
3245 watchedOperations . forEach ( op => {
3346 client . reexecuteOperation (
3447 client . createRequestOperation ( 'query' , op , {
@@ -37,6 +50,8 @@ export const refocusExchange = (): Exchange => {
3750 } )
3851 ) ;
3952 } ) ;
53+ } else {
54+ lastHidden = Date . now ( ) ;
4055 }
4156 } ) ;
4257
0 commit comments