Skip to content

Commit

Permalink
chore: pass getters to watch property
Browse files Browse the repository at this point in the history
  • Loading branch information
wobsoriano committed Feb 6, 2024
1 parent ab14672 commit 5a0c33d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
4 changes: 2 additions & 2 deletions playground/pages/reactive.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const id = ref(1)
// watch: [id]
// })
const { data: todo, pending, error, refresh } = await $client.todo.getTodo.useQuery(id)
const { data: todo, pending, error, refresh } = await $client.todo.getTodo.useQuery(() => id.value)
</script>

<template>
Expand All @@ -22,6 +22,6 @@ const { data: todo, pending, error, refresh } = await $client.todo.getTodo.useQu
Completed: {{ todo?.completed }}
</div>
<button @click="id++">
Next Todo
Next Todo {{ id }}
</button>
</template>
6 changes: 5 additions & 1 deletion src/client/decorationProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import { createRecursiveProxy } from '@trpc/server/shared'
import { getCurrentInstance, onScopeDispose, useAsyncData, toValue, ref, isRef, toRaw } from '#imports'
import { getQueryKeyInternal } from './getQueryKey'

function isRefOrGetter<T>(val: T): boolean {
return isRef(val) || typeof val === 'function';
}

function createAbortController(trpc: any) {
let controller: AbortController | undefined;

Expand Down Expand Up @@ -46,7 +50,7 @@ export function createNuxtProxyDecoration<TRouter extends AnyRouter> (name: stri
const controller = createAbortController(trpc);

const queryKey = customQueryKey || getQueryKeyInternal(path, toValue(input))
const watch = isRef(input) ? [...(asyncDataOptions.watch || []), input] : asyncDataOptions.watch
const watch = isRefOrGetter(input) ? [...(asyncDataOptions.watch || []), input] : asyncDataOptions.watch
const isLazy = lastArg === 'useLazyQuery' ? true : (asyncDataOptions.lazy || false)

return useAsyncData(queryKey, () => (client as any)[path].query(toValue(input), {
Expand Down

0 comments on commit 5a0c33d

Please sign in to comment.