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

fix(react): stricter suspended qwery checks #281

Merged
merged 2 commits into from
Apr 12, 2024
Merged
Changes from all 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
26 changes: 16 additions & 10 deletions packages/react/src/use-qwery/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,19 +111,19 @@ export const useQwery = <
}, []);

React.useEffect(() => {
if (!qwery) {
if (!qwery || (isPromise(qwery) && !isSuspendedQwery(qwery))) {
return;
}

const _qwery = isSuspendedQwery(qwery) ? qwery.value : qwery;

const stopBroadcasting = qwery?.channel?.close.bind(_qwery.channel);
const stopBroadcasting = qwery.channel?.close.bind(_qwery.channel);

return stopBroadcasting;
}, [qwery]);

React.useEffect(() => {
if (!qwery) {
if (!qwery || (isPromise(qwery) && !isSuspendedQwery(qwery))) {
return;
}

Expand All @@ -140,7 +140,7 @@ export const useQwery = <
}, [qwery]);

React.useEffect(() => {
if (!qwery) {
if (!qwery || (isPromise(qwery) && !isSuspendedQwery(qwery))) {
return;
}

Expand All @@ -167,7 +167,11 @@ export const useQwery = <
}, [qwery]);

React.useEffect(() => {
if (!qwery || !refetchOnWindowFocus) {
if (
!qwery ||
!refetchOnWindowFocus ||
(isPromise(qwery) && !isSuspendedQwery(qwery))
) {
return;
}

Expand All @@ -190,7 +194,7 @@ export const useQwery = <

React.useDebugValue(renderCount);

if (suspense && isSuspendedQwery(qwery)) {
if (suspense && isPromise(qwery)) {
return use(qwery) as any;
}

Expand All @@ -207,10 +211,12 @@ export const useQwery = <

const noOpFunction = () => {};

const isPromise = (x: unknown): x is Promise<any> => x instanceof Promise;

const isSuspendedQwery = (x: unknown): x is SuspendedQwery =>
x instanceof Promise;
isPromise(x) && (x as any).value;

// From: https://codesandbox.io/p/sandbox/hopeful-voice-zldc5w?file=%2Fsrc%2FBiography.js%3A18%2C1-43%2C1
// TODO: Replace with `use` hook once the mines are safe
const use = promise => {
if (promise.status === "fulfilled") {
return {
Expand Down Expand Up @@ -238,8 +244,8 @@ const use = promise => {
}
};

interface SuspendedQwery extends Promise<any> {
interface SuspendedQwery extends Promise<ReturnType<typeof createQwery>> {
status: "fulfilled" | "rejected" | "pending";
reason?: any;
value?: any;
value?: ReturnType<typeof createQwery>;
}