Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add option to refresh on visibilitychange event #277

Merged
merged 4 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
refreshOnVisibilityChange?: boolean;
maaaathis marked this conversation as resolved.
Show resolved Hide resolved
} & 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
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,
refreshOnVisibilityChange = refreshOnWindowFocus,
autoRefresh,
} = options;

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

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

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