diff --git a/.gitignore b/.gitignore index d3fa8e50..65f306d9 100644 --- a/.gitignore +++ b/.gitignore @@ -26,3 +26,4 @@ node_modules .svelte-kit .turbo +.astro \ No newline at end of file diff --git a/examples/axum/package.json b/examples/axum/package.json new file mode 100644 index 00000000..cabdbb1f --- /dev/null +++ b/examples/axum/package.json @@ -0,0 +1,9 @@ +{ + "name": "@rspc/examples-axum", + "version": "0.0.0", + "private": true, + "scripts": { + "dev": "cargo run" + }, + "devDependencies": {} +} diff --git a/examples/nextjs/pages/using-use-mutation.tsx b/examples/nextjs/pages/using-use-mutation.tsx index 3e5ae9b5..341f6763 100644 --- a/examples/nextjs/pages/using-use-mutation.tsx +++ b/examples/nextjs/pages/using-use-mutation.tsx @@ -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, diff --git a/examples/nextjs/pages/using-use-query.tsx b/examples/nextjs/pages/using-use-query.tsx index 1b2607db..bf09fbd7 100644 --- a/examples/nextjs/pages/using-use-query.tsx +++ b/examples/nextjs/pages/using-use-query.tsx @@ -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 (
diff --git a/packages/react-query/src/index.tsx b/packages/react-query/src/index.tsx index d11a0ca1..170a05d8 100644 --- a/packages/react-query/src/index.tsx +++ b/packages/react-query/src/index.tsx @@ -132,13 +132,18 @@ export function createReactQueryHooks< const optsRef = react.useRef(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], ); diff --git a/packages/tauri2/src/index.ts b/packages/tauri2/src/index.ts index f12aba6c..df7ad9d9 100644 --- a/packages/tauri2/src/index.ts +++ b/packages/tauri2/src/index.ts @@ -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 void>(); - private listener?: Promise; - clientSubscriptionCallback?: (id: string, value: any) => void; + private requestMap = new Map void>(); + private listener?: Promise; + 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 { - if (!this.listener) { - await this.listener; - } + async doRequest( + operation: OperationType, + key: string, + input: any, + ): Promise { + 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}'`, + ); + } + } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 6704e869..13759cac 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -107,6 +107,8 @@ importers: specifier: ^18.3.3 version: 18.3.3 + examples/axum: {} + examples/nextjs: dependencies: '@rspc/client': @@ -271,7 +273,7 @@ importers: devDependencies: '@tauri-apps/api': specifier: next - version: 2.0.0-beta.15 + version: 2.0.0-rc.0 tsup: specifier: ^8.1.0 version: 8.1.0(postcss@8.4.39)(typescript@5.5.3) @@ -1590,8 +1592,8 @@ packages: resolution: {integrity: sha512-rqI++FWClU5I2UBp4HXFvl+sBWkdigBkxnpJDQUWttNyG7IZP4FwQGhTNL5EOw0vI8i6eSAJ5frLqO7n7jbJdg==} engines: {node: '>= 14.6.0', npm: '>= 6.6.0', yarn: '>= 1.19.1'} - '@tauri-apps/api@2.0.0-beta.15': - resolution: {integrity: sha512-H9w6iISmR+NvH4XuyCZB4zDN10tf9RFt6i/9JHEjaRhAowdAaJ+oiXq/3kedizNClHMtbTQ5j0oqDVPkZDAI8g==} + '@tauri-apps/api@2.0.0-rc.0': + resolution: {integrity: sha512-v454Qs3REHc3Za59U+/eSmBsdmF+3NE5+76+lFDaitVqN4ZglDHENDaMARYKGJVZuxiSkzyqG0SeG7lLQjVkPA==} engines: {node: '>= 18.18', npm: '>= 6.6.0', yarn: '>= 1.19.1'} '@trysound/sax@0.2.0': @@ -1820,8 +1822,8 @@ packages: aws-sign2@0.7.0: resolution: {integrity: sha512-08kcGqnYf/YmjoRhfxyu+CLxBjUtHLXLXX/vUfx9l2LYzG3c1m61nrpyFUZI6zeS+Li/wWMMidD9KgrqtGq3mA==} - aws4@1.13.0: - resolution: {integrity: sha512-3AungXC4I8kKsS9PuS4JH2nc+0bVY/mjgrephHTIi8fpEeGsTHBUJeosp0Wc1myYMElmD0B3Oc4XL/HVJ4PV2g==} + aws4@1.13.1: + resolution: {integrity: sha512-u5w79Rd7SU4JaIlA/zFqG+gOiuq25q5VLyZ8E+ijJeILuTxVzZgp2CaGw/UTw6pXYN9XMO9yiqj/nEHmhTG5CA==} axobject-query@4.0.0: resolution: {integrity: sha512-+60uv1hiVFhHZeO+Lz0RYzsVHy5Wr1ayX0mwda9KPDVLNJgZ1T9Ny7VmFbLDzxsH0D87I86vgj3gFrjTJUYznw==} @@ -5724,7 +5726,7 @@ snapshots: '@tauri-apps/api@1.6.0': {} - '@tauri-apps/api@2.0.0-beta.15': {} + '@tauri-apps/api@2.0.0-rc.0': {} '@trysound/sax@0.2.0': {} @@ -6062,7 +6064,7 @@ snapshots: aws-sign2@0.7.0: {} - aws4@1.13.0: {} + aws4@1.13.1: {} axobject-query@4.0.0: dependencies: @@ -8078,7 +8080,7 @@ snapshots: request@2.88.2: dependencies: aws-sign2: 0.7.0 - aws4: 1.13.0 + aws4: 1.13.1 caseless: 0.12.0 combined-stream: 1.0.8 extend: 3.0.2 diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index daacc2a7..792d77fc 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -1,5 +1,6 @@ packages: - - docs/ - - packages/* - - examples/astro - - examples/nextjs + - docs/ + - packages/* + - examples/astro + - examples/nextjs + - examples/axum