1- import { notifyManager } from '@tanstack/query-core'
21import { useIsRestoring } from './useIsRestoring.js'
32import { useQueryClient } from './useQueryClient.js'
4- import type {
5- CreateBaseQueryOptions ,
6- CreateBaseQueryResult ,
7- FunctionedParams ,
8- } from './types.js'
9- import type {
10- QueryClient ,
11- QueryKey ,
12- QueryObserver ,
13- QueryObserverResult ,
14- } from '@tanstack/query-core'
3+ import { createReactiveThunk } from './containers.svelte.js'
4+ import type { CreateBaseQueryOptions , CreateBaseQueryResult } from './types.js'
5+ import type { QueryClient , QueryKey , QueryObserver } from '@tanstack/query-core'
156
167export function createBaseQuery <
178 TQueryFnData ,
@@ -20,64 +11,56 @@ export function createBaseQuery<
2011 TQueryData ,
2112 TQueryKey extends QueryKey ,
2213> (
23- options : FunctionedParams <
24- CreateBaseQueryOptions < TQueryFnData , TError , TData , TQueryData , TQueryKey >
14+ options : CreateBaseQueryOptions <
15+ TQueryFnData ,
16+ TError ,
17+ TData ,
18+ TQueryData ,
19+ TQueryKey
2520 > ,
2621 Observer : typeof QueryObserver ,
2722 queryClient ?: QueryClient ,
28- ) : CreateBaseQueryResult < TData , TError > {
23+ ) : ( ) => CreateBaseQueryResult < TData , TError > {
2924 /** Load query client */
3025 const client = useQueryClient ( queryClient )
31- const isRestoring = useIsRestoring ( )
26+ const isRestoring = $derived . by ( useIsRestoring ( ) )
3227
3328 /** Creates a store that has the default options applied */
34- const defaultedOptions = $derived ( ( ) => {
35- const defaultOptions = client . defaultQueryOptions ( options ( ) )
36- defaultOptions . _optimisticResults = isRestoring ( )
37- ? 'isRestoring'
38- : 'optimistic'
39- defaultOptions . structuralSharing = false
40- return defaultOptions
29+ const resolvedOptions = $derived . by ( ( ) => {
30+ const opts = client . defaultQueryOptions ( options )
31+ opts . _optimisticResults = isRestoring ? 'isRestoring' : 'optimistic'
32+ opts . structuralSharing = false
33+ return opts
4134 } )
4235
36+ let updateEffects = ( ) => { }
37+
4338 /** Creates the observer */
4439 const observer = new Observer <
4540 TQueryFnData ,
4641 TError ,
4742 TData ,
4843 TQueryData ,
4944 TQueryKey
50- > ( client , defaultedOptions ( ) )
45+ > ( client , resolvedOptions )
5146
52- const result = $state < QueryObserverResult < TData , TError > > (
53- observer . getOptimisticResult ( defaultedOptions ( ) ) ,
54- )
55-
56- function updateResult ( r : QueryObserverResult < TData , TError > ) {
57- Object . assign ( result , r )
58- }
59-
60- $effect ( ( ) => {
61- const unsubscribe = isRestoring ( )
62- ? ( ) => undefined
63- : observer . subscribe ( ( ) => {
64- notifyManager . batchCalls ( ( ) => {
65- updateResult ( observer . getOptimisticResult ( defaultedOptions ( ) ) )
66- } ) ( )
67- } )
68-
69- observer . updateResult ( )
70- return ( ) => unsubscribe ( )
71- } )
72-
73- /** Subscribe to changes in result and defaultedOptionsStore */
47+ /** Subscribe to changes in result and defaultedOptions */
7448 $effect . pre ( ( ) => {
75- observer . setOptions ( defaultedOptions ( ) , { listeners : false } )
76- updateResult ( observer . getOptimisticResult ( defaultedOptions ( ) ) )
49+ observer . setOptions ( resolvedOptions , { listeners : false } )
50+ updateEffects ( )
7751 } )
7852
79- // Handle result property usage tracking
80- return ! defaultedOptions ( ) . notifyOnChangeProps
81- ? observer . trackResult ( result )
82- : result
53+ return createReactiveThunk (
54+ ( ) => {
55+ const result = observer . getOptimisticResult ( resolvedOptions )
56+ if ( ! resolvedOptions . notifyOnChangeProps ) {
57+ return observer . trackResult ( result )
58+ }
59+ return result
60+ } ,
61+ ( update ) => {
62+ updateEffects = update
63+ return observer . subscribe ( update )
64+ } ,
65+ )
8366}
0 commit comments