11import { useIsRestoring } from './useIsRestoring.js'
22import { useQueryClient } from './useQueryClient.js'
3- import { createReactiveThunk } from './containers.svelte.js'
4- import type { CreateBaseQueryOptions , CreateBaseQueryResult } from './types.js'
3+ import { createRawRef } from './containers.svelte.js'
54import type { QueryClient , QueryKey , QueryObserver } from '@tanstack/query-core'
5+ import type { CreateBaseQueryOptions , CreateBaseQueryResult } from './types.js'
66
77export function createBaseQuery <
88 TQueryFnData ,
@@ -20,15 +20,16 @@ export function createBaseQuery<
2020 > ,
2121 Observer : typeof QueryObserver ,
2222 queryClient ?: QueryClient ,
23- ) : ( ) => CreateBaseQueryResult < TData , TError > {
23+ ) : CreateBaseQueryResult < TData , TError > {
2424 /** Load query client */
25+ console . log ( 'createBaseQuery' , options )
2526 const client = useQueryClient ( queryClient )
26- const isRestoring = $derived . by ( useIsRestoring ( ) )
27+ const isRestoring = useIsRestoring ( )
2728
2829 /** Creates a store that has the default options applied */
2930 const resolvedOptions = $derived . by ( ( ) => {
3031 const opts = client . defaultQueryOptions ( options )
31- opts . _optimisticResults = isRestoring ? 'isRestoring' : 'optimistic'
32+ opts . _optimisticResults = isRestoring . current ? 'isRestoring' : 'optimistic'
3233 opts . structuralSharing = false
3334 return opts
3435 } )
@@ -42,23 +43,25 @@ export function createBaseQuery<
4243 TQueryKey
4344 > ( client , resolvedOptions )
4445
45- return createReactiveThunk (
46- ( ) => {
46+ let [ query , update ] = createRawRef (
47+ observer . getOptimisticResult ( resolvedOptions ) ,
48+ )
49+
50+ // if you update this effect in the future, _make sure_ the unsubscribe function is still being returned
51+ $effect ( ( ) =>
52+ observer . subscribe ( ( ) => {
4753 const result = observer . getOptimisticResult ( resolvedOptions )
48- if ( ! resolvedOptions . notifyOnChangeProps ) {
49- return observer . trackResult ( result )
50- }
51- return result
52- } ,
53- ( update ) => observer . subscribe ( update ) ,
54- [
55- {
56- type : 'pre' ,
57- fn : ( update ) => {
58- observer . setOptions ( resolvedOptions , { listeners : false } )
59- update ( )
60- } ,
61- } ,
62- ] ,
54+ update ( result )
55+ } ) ,
6356 )
57+
58+ $effect . pre ( ( ) => {
59+ observer . setOptions ( resolvedOptions , { listeners : false } )
60+ const result = observer . getOptimisticResult ( resolvedOptions )
61+ update ( result )
62+ } )
63+
64+ return resolvedOptions . notifyOnChangeProps
65+ ? observer . trackResult ( query )
66+ : query
6467}
0 commit comments