Skip to content

Commit

Permalink
chore: clean up abort controller logic
Browse files Browse the repository at this point in the history
  • Loading branch information
wobsoriano committed Dec 7, 2023
1 parent da2f1b3 commit d2a1393
Showing 1 changed file with 17 additions and 23 deletions.
40 changes: 17 additions & 23 deletions src/client/decorationProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,29 @@ import { createRecursiveProxy } from '@trpc/server/shared'
import { getCurrentInstance, onScopeDispose, useAsyncData, unref, ref, isRef, toRaw } from '#imports'
import { getQueryKeyInternal } from './getQueryKey'

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

if (trpc?.abortOnUnmount) {
if (getCurrentInstance()) {
onScopeDispose(() => {
controller?.abort?.()
})
}
controller = typeof AbortController !== 'undefined' ? new AbortController() : {} as AbortController
}

return controller;
}

export function createNuxtProxyDecoration<TRouter extends AnyRouter> (name: string, client: inferRouterProxyClient<TRouter>) {
return createRecursiveProxy((opts) => {
const args = opts.args

const pathCopy = [name, ...opts.path]

// The last arg is for instance `.useMutation` or `.useQuery()`
const lastArg = pathCopy.pop()!

// The `path` ends up being something like `post.byId`
const path = pathCopy.join('.')

const [input, otherOptions] = args
Expand All @@ -28,16 +41,7 @@ export function createNuxtProxyDecoration<TRouter extends AnyRouter> (name: stri
if (['useQuery', 'useLazyQuery'].includes(lastArg)) {
const { trpc, queryKey: customQueryKey, ...asyncDataOptions } = otherOptions || {} as any

let controller: AbortController

if (trpc?.abortOnUnmount) {
if (getCurrentInstance()) {
onScopeDispose(() => {
controller?.abort?.()
})
}
controller = typeof AbortController !== 'undefined' ? new AbortController() : {} as AbortController
}
const controller = createAbortController(trpc);

const queryKey = customQueryKey || getQueryKeyInternal(path, unref(input))
const watch = isRef(input) ? [...(asyncDataOptions.watch || []), input] : asyncDataOptions.watch
Expand All @@ -56,19 +60,9 @@ export function createNuxtProxyDecoration<TRouter extends AnyRouter> (name: stri
if (lastArg === 'useMutation') {
const { trpc, ...asyncDataOptions } = otherOptions || {} as any

// Payload will be set by the `mutate` function and used by `useAsyncData`.
const payload = ref(null)

let controller: AbortController

if (trpc?.abortOnUnmount) {
if (getCurrentInstance()) {
onScopeDispose(() => {
controller?.abort?.()
})
}
controller = typeof AbortController !== 'undefined' ? new AbortController() : {} as AbortController
}
const controller = createAbortController(trpc);

const asyncData = useAsyncData(() => (client as any)[path].mutate(payload.value, {
signal: controller?.signal,
Expand Down

1 comment on commit d2a1393

@vercel
Copy link

@vercel vercel bot commented on d2a1393 Dec 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

trpc-nuxt – ./

trpc-nuxt-wobsoriano.vercel.app
trpc-nuxt-git-main-wobsoriano.vercel.app
trpc-nuxt.vercel.app

Please sign in to comment.