minor reactive fixes

This commit is contained in:
OrJDev
2022-11-05 10:13:28 +02:00
parent d2999933c2
commit 359c30dd8e
4 changed files with 48 additions and 29 deletions
+20 -1
View File
@@ -57,5 +57,24 @@ export default App;
import { trpc } from "./utils/trpc"; import { trpc } from "./utils/trpc";
import { createSignal } from "solid-js"; import { createSignal } from "solid-js";
const [name, setName] = createSignal(""); const [name, setName] = createSignal("");
const res = trpc.queryName.useQuery(() => ({ name: name() })); // this will be called when name changes const res = trpc.queryName.useQuery(() => ({ name: name() })); // this will be called onMount and when name changes
``` ```
### Using Options
If you are using the `enabled` property make sure you follow Solid Query rules:
```ts
const [enabled, setEnabled] = createSignal(false);
const query = trpc.queryName.useQuery(() => "hey there", {
// ❌ passing a signal directly is not reactive
// enabled: enabled(),
// ✅ passing a function that returns a signal is reactive
get enabled() {
return enabled();
},
});
```
And options could be passed to all hooks.
+1 -1
View File
@@ -3,7 +3,7 @@
"description": "SolidJS tRPC", "description": "SolidJS tRPC",
"author": "OrJDev", "author": "OrJDev",
"license": "MIT", "license": "MIT",
"version": "0.0.6-rc.1", "version": "0.0.7-rc.1",
"publishConfig": { "publishConfig": {
"access": "public", "access": "public",
"tag": "next" "tag": "next"
+4 -4
View File
@@ -45,7 +45,7 @@ export type DecorateProcedure<
TData = inferProcedureOutput<TProcedure> TData = inferProcedureOutput<TProcedure>
>( >(
input: () => inferProcedureInput<TProcedure>, input: () => inferProcedureInput<TProcedure>,
opts?: () => UseTRPCQueryOptions< opts?: UseTRPCQueryOptions<
TPath, TPath,
inferProcedureInput<TProcedure>, inferProcedureInput<TProcedure>,
TQueryFnData, TQueryFnData,
@@ -60,7 +60,7 @@ export type DecorateProcedure<
TData = inferProcedureOutput<TProcedure> TData = inferProcedureOutput<TProcedure>
>( >(
input: () => Omit<inferProcedureInput<TProcedure>, "cursor">, input: () => Omit<inferProcedureInput<TProcedure>, "cursor">,
opts?: () => UseTRPCInfiniteQueryOptions< opts?: UseTRPCInfiniteQueryOptions<
TPath, TPath,
inferProcedureInput<TProcedure>, inferProcedureInput<TProcedure>,
TData, TData,
@@ -75,7 +75,7 @@ export type DecorateProcedure<
: TProcedure extends AnyMutationProcedure : TProcedure extends AnyMutationProcedure
? { ? {
useMutation: <TContext = unknown>( useMutation: <TContext = unknown>(
opts?: () => UseTRPCMutationOptions< opts?: UseTRPCMutationOptions<
inferProcedureInput<TProcedure>, inferProcedureInput<TProcedure>,
TRPCClientErrorLike<TProcedure>, TRPCClientErrorLike<TProcedure>,
inferProcedureOutput<TProcedure>, inferProcedureOutput<TProcedure>,
@@ -92,7 +92,7 @@ export type DecorateProcedure<
? { ? {
useSubscription: ( useSubscription: (
input: () => inferProcedureInput<TProcedure>, input: () => inferProcedureInput<TProcedure>,
opts?: () => UseTRPCSubscriptionOptions< opts?: UseTRPCSubscriptionOptions<
inferObservableValue<inferProcedureOutput<TProcedure>>, inferObservableValue<inferProcedureOutput<TProcedure>>,
TRPCClientErrorLike<TProcedure> TRPCClientErrorLike<TProcedure>
> >
+23 -23
View File
@@ -376,7 +376,7 @@ export function createHooksInternal<
path: TPath, path: TPath,
...args: inferHandlerInput<TQueries[TPath]> ...args: inferHandlerInput<TQueries[TPath]>
], ],
opts?: () => UseTRPCQueryOptions< opts?: UseTRPCQueryOptions<
TPath, TPath,
TQueryValues[TPath]["input"], TQueryValues[TPath]["input"],
TQueryFnData, TQueryFnData,
@@ -385,25 +385,26 @@ export function createHooksInternal<
> >
): UseTRPCQueryResult<TData, TError> { ): UseTRPCQueryResult<TData, TError> {
const ctx = useContext(); const ctx = useContext();
// createEffect(() => console.log("opts", opts?.()));
if ( if (
typeof window === "undefined" && typeof window === "undefined" &&
ctx.ssrState() === "prepass" && ctx.ssrState() === "prepass" &&
opts?.().trpc?.ssr !== false && opts?.trpc?.ssr !== false &&
opts?.().enabled !== false && opts?.enabled !== false &&
!ctx.queryClient.getQueryCache().find(getArrayQueryKey(pathAndInput())) !ctx.queryClient.getQueryCache().find(getArrayQueryKey(pathAndInput()))
) { ) {
void ctx.prefetchQuery(pathAndInput(), opts?.() as any); void ctx.prefetchQuery(pathAndInput(), opts as any);
} }
const shouldAbortOnUnmount = () => const shouldAbortOnUnmount = () =>
opts?.().trpc?.abortOnUnmount ?? ctx?.abortOnUnmount ?? false; opts?.trpc?.abortOnUnmount ?? ctx?.abortOnUnmount ?? false;
const hook = __useQuery( const hook = __useQuery(
() => getArrayQueryKey(pathAndInput()), () => getArrayQueryKey(pathAndInput()),
(queryFunctionContext) => { (queryFunctionContext) => {
const actualOpts = () => ({ const actualOpts = () => ({
...opts?.(), ...opts,
trpc: { trpc: {
...opts?.()?.trpc, ...opts?.trpc,
...(shouldAbortOnUnmount() ...(shouldAbortOnUnmount()
? { signal: queryFunctionContext.signal } ? { signal: queryFunctionContext.signal }
: {}), : {}),
@@ -413,7 +414,7 @@ export function createHooksInternal<
...getClientArgs(pathAndInput(), actualOpts()) ...getClientArgs(pathAndInput(), actualOpts())
); );
}, },
{ context: SolidQueryContext, ...opts?.() } as any opts as any
) as UseTRPCQueryResult<TData, TError>; ) as UseTRPCQueryResult<TData, TError>;
hook.trpc = useHookResult({ hook.trpc = useHookResult({
path: pathAndInput()[0], path: pathAndInput()[0],
@@ -426,7 +427,7 @@ export function createHooksInternal<
TContext = unknown TContext = unknown
>( >(
path: TPath | [TPath], path: TPath | [TPath],
opts?: () => UseTRPCMutationOptions< opts?: UseTRPCMutationOptions<
TMutationValues[TPath]["input"], TMutationValues[TPath]["input"],
TError, TError,
TMutationValues[TPath]["output"], TMutationValues[TPath]["output"],
@@ -446,14 +447,14 @@ export function createHooksInternal<
const actualPath = Array.isArray(path) ? path[0] : path; const actualPath = Array.isArray(path) ? path[0] : path;
return (ctx.client.mutation as any)( return (ctx.client.mutation as any)(
...getClientArgs([actualPath, input], opts?.()) ...getClientArgs([actualPath, input], opts)
); );
}, },
{ {
context: SolidQueryContext, context: SolidQueryContext,
...opts?.(), ...opts,
onSuccess(...args) { onSuccess(...args) {
const originalFn = () => opts?.().onSuccess?.(...args); const originalFn = () => opts?.onSuccess?.(...args);
return mutationSuccessOverride({ originalFn, queryClient }); return mutationSuccessOverride({ originalFn, queryClient });
}, },
} }
@@ -485,16 +486,15 @@ export function createHooksInternal<
path: TPath, path: TPath,
...args: inferHandlerInput<TSubscriptions[TPath]> ...args: inferHandlerInput<TSubscriptions[TPath]>
], ],
opts: () => UseTRPCSubscriptionOptions< opts: UseTRPCSubscriptionOptions<
inferObservableValue<inferProcedureOutput<TSubscriptions[TPath]>>, inferObservableValue<inferProcedureOutput<TSubscriptions[TPath]>>,
inferProcedureClientError<TSubscriptions[TPath]> inferProcedureClientError<TSubscriptions[TPath]>
> >
) { ) {
const enabled = opts?.().enabled ?? true;
const ctx = useContext(); const ctx = useContext();
return createEffect(() => { return createEffect(() => {
if (!enabled) { if (!(opts.enabled ?? true)) {
return; return;
} }
// noop // noop
@@ -510,17 +510,17 @@ export function createHooksInternal<
>(pathAndInput()[0], (pathAndInput()[1] ?? undefined) as any, { >(pathAndInput()[0], (pathAndInput()[1] ?? undefined) as any, {
onStarted: () => { onStarted: () => {
if (!isStopped) { if (!isStopped) {
opts?.().onStarted?.(); opts?.onStarted?.();
} }
}, },
onData: (data) => { onData: (data) => {
if (!isStopped) { if (!isStopped) {
opts().onData(data); opts?.onData(data);
} }
}, },
onError: (err) => { onError: (err) => {
if (!isStopped) { if (!isStopped) {
opts().onError?.(err); opts?.onError?.(err);
} }
}, },
}); });
@@ -536,7 +536,7 @@ export function createHooksInternal<
path: TPath, path: TPath,
input: Omit<TQueryValues[TPath]["input"], "cursor"> input: Omit<TQueryValues[TPath]["input"], "cursor">
], ],
opts?: () => UseTRPCInfiniteQueryOptions< opts?: UseTRPCInfiniteQueryOptions<
TPath, TPath,
Omit<TQueryValues[TPath]["input"], "cursor">, Omit<TQueryValues[TPath]["input"], "cursor">,
TQueryValues[TPath]["output"], TQueryValues[TPath]["output"],
@@ -548,18 +548,18 @@ export function createHooksInternal<
if ( if (
typeof window === "undefined" && typeof window === "undefined" &&
ctx.ssrState() === "prepass" && ctx.ssrState() === "prepass" &&
opts?.()?.trpc?.ssr !== false && opts?.trpc?.ssr !== false &&
opts?.()?.enabled !== false && opts?.enabled !== false &&
!ctx.queryClient.getQueryCache().find(getArrayQueryKey(pathAndInput())) !ctx.queryClient.getQueryCache().find(getArrayQueryKey(pathAndInput()))
) { ) {
void ctx.prefetchInfiniteQuery(pathAndInput as any, opts as any); void ctx.prefetchInfiniteQuery(pathAndInput as any, opts as any);
} }
const ssrOpts = useSSRQueryOptionsIfNeeded(pathAndInput(), opts?.()); const ssrOpts = useSSRQueryOptionsIfNeeded(pathAndInput(), opts);
// request option should take priority over global // request option should take priority over global
const shouldAbortOnUnmount = const shouldAbortOnUnmount =
opts?.()?.trpc?.abortOnUnmount ?? ctx?.abortOnUnmount ?? false; opts?.trpc?.abortOnUnmount ?? ctx?.abortOnUnmount ?? false;
const hook = __useInfiniteQuery( const hook = __useInfiniteQuery(
() => getArrayQueryKey(pathAndInput()), () => getArrayQueryKey(pathAndInput()),