···11+---
22+'@urql/exchange-refocus': minor
33+---
44+55+Add `minimumTime` to `refocusExchange` to throttle query reexecution.
+4-1
exchanges/refocus/README.md
···22222323const client = createClient({
2424 url: 'http://localhost:3000/graphql',
2525- exchanges: [refocusExchange(), cacheExchange, fetchExchange],
2525+ exchanges: [refocusExchange({
2626+ // The minimum time in milliseconds to wait before another refocus can trigger. Default value is 0.
2727+ minimumTime: 2000
2828+ }), cacheExchange, fetchExchange],
2629});
2730```
+20-5
exchanges/refocus/src/refocusExchange.ts
···11import { pipe, tap } from 'wonka';
22import type { Exchange, Operation } from '@urql/core';
3344+export interface RefocusOptions {
55+ /** The minimum time in milliseconds to wait before another refocus can trigger.
66+ * @defaultValue `0`
77+ */
88+ minimumTime?: number;
99+}
1010+411/** Exchange factory that reexecutes operations after a user returns to the tab.
512 *
1313+ * @param opts - A {@link RefocusOptions} configuration object.
1414+ *
615 * @returns a new refocus {@link Exchange}.
716 *
817 * @remarks
···1423 * The `cache-and-network` policy will refetch data in the background, but will
1524 * only refetch queries that are currently active.
1625 */
1717-export const refocusExchange = (): Exchange => {
2626+export const refocusExchange = (opts: RefocusOptions = {}): Exchange => {
2727+ const { minimumTime = 0 } = opts;
2828+1829 return ({ client, forward }) =>
1930 ops$ => {
2031 if (typeof window === 'undefined') {
···2435 const watchedOperations = new Map<number, Operation>();
2536 const observedOperations = new Map<number, number>();
26373838+ let lastHidden = 0;
3939+2740 window.addEventListener('visibilitychange', () => {
2828- if (
2929- typeof document !== 'object' ||
3030- document.visibilityState === 'visible'
3131- ) {
4141+ const state =
4242+ typeof document !== 'object' ? 'visible' : document.visibilityState;
4343+ if (state === 'visible') {
4444+ if (Date.now() - lastHidden < minimumTime) return;
3245 watchedOperations.forEach(op => {
3346 client.reexecuteOperation(
3447 client.createRequestOperation('query', op, {
···3750 })
3851 );
3952 });
5353+ } else {
5454+ lastHidden = Date.now();
4055 }
4156 });
4257