Skip to content

Commit

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

type Callback = () => void;

export const useOnVisibilityChange = (
cb: Callback,
deps: DependencyList,
): void => {
useEffect(() => {
if (isBrowser) {
document.addEventListener("visibilitychange", cb);
return () => {
document.removeEventListener("visibilitychange", cb);
};
}
}, 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;
refreshOnVisibilityChange?: 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 'refreshOnVisibilityChange' is enabled", async () => {
options.refreshOnVisibilityChange = 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
8 changes: 8 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,
refreshOnVisibilityChange = false,
autoRefresh,
} = options;

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

useOnVisibilityChange(() => {
if (refreshOnVisibilityChange && document.visibilityState === "visible") {
resource.refresh();
}
}, [resource, refreshOnVisibilityChange]);

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

0 comments on commit 60a89f2

Please sign in to comment.