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 "Invalid hook call" issue when using @rspc/react-query #301

Open
wants to merge 1 commit into
base: v0.x
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ node_modules

.svelte-kit
.turbo
.astro
9 changes: 9 additions & 0 deletions examples/axum/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@rspc/examples-axum",
"version": "0.0.0",
"private": true,
"scripts": {
"dev": "cargo run"
},
"devDependencies": {}
}
4 changes: 1 addition & 3 deletions examples/nextjs/pages/using-use-mutation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import { useMutation } from "../src/rspc";
import styles from "../styles/Home.module.css";

const UsingUseMutation: NextPage = () => {
const { mutate, data, isPending, error } = useMutation({
mutationKey: "sendMsg",
});
const { mutate, data, isPending, error } = useMutation("sendMsg");

const handleSubmit = async (
event: React.FormEvent<HTMLFormElement>,
Expand Down
2 changes: 1 addition & 1 deletion examples/nextjs/pages/using-use-query.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useQuery } from "../src/rspc";
import styles from "../styles/Home.module.css";

const UsingUseQuery: NextPage = () => {
const { data, isLoading, error } = useQuery({ queryKey: ["echo", "Hello!"] });
const { data, isLoading, error } = useQuery(["echo", "Hello!"]);

return (
<div className={styles.container}>
Expand Down
9 changes: 7 additions & 2 deletions packages/react-query/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,18 @@ export function createReactQueryHooks<
const optsRef = react.useRef<typeof opts>(opts);
optsRef.current = opts;

const ctx = react.useContext(Context);
const helpersInner = queryCore.createQueryHookHelpers({
useContext: () => ctx,
});

// biome-ignore lint/correctness/useExhaustiveDependencies:
return react.useEffect(
() =>
helpers.handleSubscription(
helpersInner.handleSubscription(
keyAndInput,
() => optsRef.current,
helpers.useClient(),
helpersInner.useClient(),
),
[queryKey, enabled],
);
Expand Down
131 changes: 68 additions & 63 deletions packages/tauri2/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,72 +1,77 @@
import { randomId, OperationType, Transport, RSPCError } from "@rspc/client";
import { listen, UnlistenFn } from "@tauri-apps/api/event";
import { getCurrent } from "@tauri-apps/api/window";
import {
randomId,
type OperationType,
type Transport,
RSPCError,
} from "@rspc/client";
import { listen, type UnlistenFn } from "@tauri-apps/api/event";
import { getCurrentWindow } from "@tauri-apps/api/window";

export class TauriTransport implements Transport {
private requestMap = new Map<string, (data: any) => void>();
private listener?: Promise<UnlistenFn>;
clientSubscriptionCallback?: (id: string, value: any) => void;
private requestMap = new Map<string, (data: any) => void>();
private listener?: Promise<UnlistenFn>;
clientSubscriptionCallback?: (id: string, value: any) => void;

constructor() {
this.listener = listen("plugin:rspc:transport:resp", (event) => {
const { id, result } = event.payload as any;
if (result.type === "event") {
if (this.clientSubscriptionCallback)
this.clientSubscriptionCallback(id, result.data);
} else if (result.type === "response") {
if (this.requestMap.has(id)) {
this.requestMap.get(id)?.({ type: "response", result: result.data });
this.requestMap.delete(id);
}
} else if (result.type === "error") {
const { message, code } = result.data;
if (this.requestMap.has(id)) {
this.requestMap.get(id)?.({ type: "error", message, code });
this.requestMap.delete(id);
}
} else {
console.error(`Received event of unknown method '${result.type}'`);
}
});
}
constructor() {
this.listener = listen("plugin:rspc:transport:resp", (event) => {
const { id, result } = event.payload as any;
if (result.type === "event") {
if (this.clientSubscriptionCallback)
this.clientSubscriptionCallback(id, result.data);
} else if (result.type === "response") {
if (this.requestMap.has(id)) {
this.requestMap.get(id)?.({ type: "response", result: result.data });
this.requestMap.delete(id);
}
} else if (result.type === "error") {
const { message, code } = result.data;
if (this.requestMap.has(id)) {
this.requestMap.get(id)?.({ type: "error", message, code });
this.requestMap.delete(id);
}
} else {
console.error(`Received event of unknown method '${result.type}'`);
}
});
}

async doRequest(
operation: OperationType,
key: string,
input: any
): Promise<any> {
if (!this.listener) {
await this.listener;
}
async doRequest(
operation: OperationType,
key: string,
input: any,
): Promise<any> {
if (!this.listener) {
await this.listener;
}

const id = randomId();
let resolve: (data: any) => void;
const promise = new Promise((res) => {
resolve = res;
});
const id = randomId();
let resolve: (data: any) => void;
const promise = new Promise((res) => {
resolve = res;
});

// @ts-ignore
this.requestMap.set(id, resolve);
// @ts-ignore
this.requestMap.set(id, resolve);

await getCurrent().emit("plugin:rspc:transport", {
id,
method: operation,
params: {
path: key,
input,
},
});
await getCurrentWindow().emit("plugin:rspc:transport", {
id,
method: operation,
params: {
path: key,
input,
},
});

const body = (await promise) as any;
if (body.type === "error") {
const { code, message } = body;
throw new RSPCError(code, message);
} else if (body.type === "response") {
return body.result;
} else {
throw new Error(
`RSPC Tauri doRequest received invalid body type '${body?.type}'`
);
}
}
const body = (await promise) as any;
if (body.type === "error") {
const { code, message } = body;
throw new RSPCError(code, message);
} else if (body.type === "response") {
return body.result;
} else {
throw new Error(
`RSPC Tauri doRequest received invalid body type '${body?.type}'`,
);
}
}
}
18 changes: 10 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
packages:
- docs/
- packages/*
- examples/astro
- examples/nextjs
- docs/
- packages/*
- examples/astro
- examples/nextjs
- examples/axum