mirror of
https://github.com/ArthurDanjou/trpc-nuxt.git
synced 2026-01-29 03:10:40 +01:00
feat: add abortOnUnmount option
This commit is contained in:
@@ -24,7 +24,12 @@ const addTodo = async () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const { data: todos, pending, error, refresh } = await $client.todo.getTodos.query()
|
const { data: todos, pending, error, refresh } = await $client.todo.getTodos.query(undefined, {
|
||||||
|
trpc: {
|
||||||
|
abortOnUnmount: true,
|
||||||
|
},
|
||||||
|
server: false,
|
||||||
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import type {
|
|||||||
} from '@trpc/server'
|
} from '@trpc/server'
|
||||||
import { createFlatProxy, createRecursiveProxy } from '@trpc/server/shared'
|
import { createFlatProxy, createRecursiveProxy } from '@trpc/server/shared'
|
||||||
import { hash } from 'ohash'
|
import { hash } from 'ohash'
|
||||||
|
import { getCurrentInstance, onScopeDispose } from 'vue'
|
||||||
import type { DecoratedProcedureRecord } from './types'
|
import type { DecoratedProcedureRecord } from './types'
|
||||||
// @ts-expect-error: Nuxt auto-imports
|
// @ts-expect-error: Nuxt auto-imports
|
||||||
import { useAsyncData, useState } from '#imports'
|
import { useAsyncData, useState } from '#imports'
|
||||||
@@ -36,7 +37,21 @@ function createNuxtProxyDecoration<TRouter extends AnyRouter>(name: string, clie
|
|||||||
|
|
||||||
const { trpc, ...asyncDataOptions } = otherOptions || {} as any
|
const { trpc, ...asyncDataOptions } = otherOptions || {} as any
|
||||||
|
|
||||||
return useAsyncDataWithError(queryKey, () => (client as any)[path][lastArg](input, trpc), asyncDataOptions)
|
let controller: AbortController
|
||||||
|
|
||||||
|
if (trpc?.abortOnUnmount) {
|
||||||
|
if (getCurrentInstance()) {
|
||||||
|
onScopeDispose(() => {
|
||||||
|
controller?.abort?.()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
controller = typeof AbortController !== 'undefined' ? new AbortController() : {} as AbortController
|
||||||
|
}
|
||||||
|
|
||||||
|
return useAsyncDataWithError(queryKey, () => (client as any)[path][lastArg](input, {
|
||||||
|
...trpc,
|
||||||
|
signal: controller?.signal,
|
||||||
|
}), asyncDataOptions)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -19,6 +19,10 @@ import type {
|
|||||||
// Inspired by trpc/react-query client types
|
// Inspired by trpc/react-query client types
|
||||||
// https://github.com/trpc/trpc/blob/next/packages/react-query/src/createTRPCReact.tsx
|
// https://github.com/trpc/trpc/blob/next/packages/react-query/src/createTRPCReact.tsx
|
||||||
|
|
||||||
|
interface TRPCOptions extends TRPCRequestOptions {
|
||||||
|
abortOnUnmount?: boolean
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
@@ -33,7 +37,7 @@ export type DecorateProcedure<
|
|||||||
PickKeys extends KeyOfRes<Transform> = KeyOfRes<Transform>,
|
PickKeys extends KeyOfRes<Transform> = KeyOfRes<Transform>,
|
||||||
>(
|
>(
|
||||||
input: inferProcedureInput<TProcedure>,
|
input: inferProcedureInput<TProcedure>,
|
||||||
opts?: AsyncDataOptions<TData, Transform, PickKeys> & { trpc: TRPCRequestOptions },
|
opts?: AsyncDataOptions<TData, Transform, PickKeys> & { trpc: TRPCOptions },
|
||||||
) => AsyncData<PickFrom<ReturnType<Transform>, PickKeys>, TRPCClientErrorLike<TProcedure>>
|
) => AsyncData<PickFrom<ReturnType<Transform>, PickKeys>, TRPCClientErrorLike<TProcedure>>
|
||||||
} : TProcedure extends AnyMutationProcedure ? {
|
} : TProcedure extends AnyMutationProcedure ? {
|
||||||
mutate: <
|
mutate: <
|
||||||
@@ -42,7 +46,7 @@ export type DecorateProcedure<
|
|||||||
PickKeys extends KeyOfRes<Transform> = KeyOfRes<Transform>,
|
PickKeys extends KeyOfRes<Transform> = KeyOfRes<Transform>,
|
||||||
>(
|
>(
|
||||||
input: inferProcedureInput<TProcedure>,
|
input: inferProcedureInput<TProcedure>,
|
||||||
opts?: AsyncDataOptions<TData, Transform, PickKeys> & { trpc: TRPCRequestOptions },
|
opts?: AsyncDataOptions<TData, Transform, PickKeys> & { trpc: TRPCOptions },
|
||||||
) => AsyncData<PickFrom<ReturnType<Transform>, PickKeys>, TRPCClientErrorLike<TProcedure>>
|
) => AsyncData<PickFrom<ReturnType<Transform>, PickKeys>, TRPCClientErrorLike<TProcedure>>
|
||||||
} : never
|
} : never
|
||||||
|
|
||||||
|
|||||||
@@ -5,9 +5,8 @@ export default defineConfig({
|
|||||||
format: ['cjs', 'esm'],
|
format: ['cjs', 'esm'],
|
||||||
splitting: false,
|
splitting: false,
|
||||||
clean: true,
|
clean: true,
|
||||||
external: ['#app', '#imports'],
|
external: ['#app', '#imports', 'vue'],
|
||||||
dts: true,
|
dts: true,
|
||||||
// @ts-expect-error: Missing type
|
|
||||||
outExtension({ format }) {
|
outExtension({ format }) {
|
||||||
return {
|
return {
|
||||||
js: format === 'esm' ? '.mjs' : `.${format}`,
|
js: format === 'esm' ? '.mjs' : `.${format}`,
|
||||||
|
|||||||
Reference in New Issue
Block a user