Skip to content

Commit

Permalink
feat: add option to refresh on visibilitychange event (#277)
Browse files Browse the repository at this point in the history
  • Loading branch information
maaaathis authored Nov 20, 2024
1 parent 62067b0 commit 601983d
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/lib/useOnVisibilityChange.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { DependencyList, useEffect } from "react";
import { isBrowser } from "browser-or-node";

type Callback = (isVisible: boolean) => void;

export const useOnVisibilityChange = (
cb: Callback,
deps: DependencyList,
): void => {
useEffect(() => {
if (isBrowser) {
document.addEventListener("visibilitychange", () => cb(!document.hidden));
return () => {
document.removeEventListener("visibilitychange", () =>
cb(!document.hidden),
);
};
}
}, deps);
};
1 change: 1 addition & 0 deletions src/resource/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export type UseWatchResourceOptions = {
useSuspense?: boolean;
autoRefresh?: DurationLikeObject;
refreshOnWindowFocus?: boolean;
refreshOnDocumentVisibilityChange?: boolean;
} & GetAsyncResourceOptions;

export type NoSuspenseReturnType<T> = Readonly<
Expand Down
29 changes: 29 additions & 0 deletions src/resource/useWatchResourceValue.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,35 @@ test("focus event does not trigger resource refresh, if 'refreshOnWindowFocus' i
expectValue("Foo");
});

test("visibilitychange event triggers resource refresh, if 'refreshOnDocumentVisibilityChange' is enabled", async () => {
options.refreshOnDocumentVisibilityChange = true;
render(<TestView />);
await waitToBeLoaded();
expectValue("Foo");

getName.mockReturnValue("Bar");
act(() => {
document.dispatchEvent(new Event("visibilitychange"));
});

await waitToBeLoaded();
expectValue("Bar");
});

test("visibilitychange event does not trigger resource refresh, if 'refreshOnVisibilityChange' is not enabled", async () => {
render(<TestView />);
await waitToBeLoaded();
expectValue("Foo");

getName.mockReturnValue("Bar");
act(() => {
document.dispatchEvent(new Event("visibilitychange"));
});

await waitToBeLoaded();
expectValue("Foo");
});

describe("with disabled suspense", () => {
beforeEach(() => {
options.useSuspense = false;
Expand Down
11 changes: 11 additions & 0 deletions src/resource/useWatchResourceValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useWatchObservableValue } from "../observable-value/useWatchObservableV
import { UseWatchResourceOptions, UseWatchResourceResult } from "./types.js";
import { hash } from "object-code";
import { useOnWindowFocused } from "../lib/useOnWindowFocused.js";
import { useOnVisibilityChange } from "../lib/useOnVisibilityChange.js";

export const useWatchResourceValue = <
T,
Expand All @@ -18,6 +19,7 @@ export const useWatchResourceValue = <
keepValueWhileLoading = true,
useSuspense = true,
refreshOnWindowFocus = false,
refreshOnDocumentVisibilityChange = refreshOnWindowFocus,
autoRefresh,
} = options;

Expand All @@ -37,6 +39,15 @@ export const useWatchResourceValue = <
}
}, [resource, refreshOnWindowFocus]);

useOnVisibilityChange(
(isVisible) => {
if (refreshOnDocumentVisibilityChange && isVisible) {
resource.refresh();
}
},
[resource, refreshOnDocumentVisibilityChange],
);

setTimeout(() => {
void resource.load();
}, 0);
Expand Down

0 comments on commit 601983d

Please sign in to comment.