Skip to content

Commit

Permalink
refactor(svelte-5-adapter): simplify createBaseQuery (#7808)
Browse files Browse the repository at this point in the history
* refactor: simplify createBaseQuery

* Don't take snapshot of queryKey
  • Loading branch information
lachlancollins committed Jul 31, 2024
1 parent 233666d commit 54135bf
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 59 deletions.
73 changes: 22 additions & 51 deletions packages/svelte-query/src/createBaseQuery.svelte.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { notifyManager } from '@tanstack/query-core'
import { untrack } from 'svelte'
import { useIsRestoring } from './useIsRestoring'
import { useQueryClient } from './useQueryClient'
import type {
Expand Down Expand Up @@ -32,81 +31,53 @@ export function createBaseQuery<
const isRestoring = useIsRestoring()

/** Creates a store that has the default options applied */
function updateOptions() {
const queryKey: TQueryKey = $state.snapshot(options().queryKey) as any // remove proxy prevent reactive query in DevTools
const defaultedOptions = client.defaultQueryOptions({
...options(),
queryKey: queryKey,
})
defaultedOptions._optimisticResults = 'optimistic'
if (isRestoring()) {
defaultedOptions._optimisticResults = 'isRestoring'
}

defaultedOptions.structuralSharing = false

return defaultedOptions
}
const defaultedOptions = $derived(() => {
const defaultOptions = client.defaultQueryOptions(options())
defaultOptions._optimisticResults = isRestoring()
? 'isRestoring'
: 'optimistic'
defaultOptions.structuralSharing = false
return defaultOptions
})

const defaultedOptionsStore = updateOptions
/** Creates the observer */
const observer = new Observer<
TQueryFnData,
TError,
TData,
TQueryData,
TQueryKey
>(client, defaultedOptionsStore())
>(client, defaultedOptions())

const result = $state<QueryObserverResult<TData, TError>>(
observer.getOptimisticResult(defaultedOptionsStore()),
observer.getOptimisticResult(defaultedOptions()),
)

function upResult(r: QueryObserverResult<TData, TError>) {
function updateResult(r: QueryObserverResult<TData, TError>) {
Object.assign(result, r)
}

$effect(() => {
let un = () => undefined
if (!isRestoring()) {
{
// @ts-expect-error
un = observer.subscribe((v) => {
const unsubscribe = isRestoring()
? () => undefined
: observer.subscribe(() => {
notifyManager.batchCalls(() => {
const temp = observer.getOptimisticResult(defaultedOptionsStore())
upResult(temp)
updateResult(observer.getOptimisticResult(defaultedOptions()))
})()
})
}
}

observer.updateResult()
return () => {
un()
}
return () => unsubscribe()
})

/** Subscribe to changes in result and defaultedOptionsStore */
$effect.pre(() => {
observer.setOptions(defaultedOptionsStore(), { listeners: false })
upResult(observer.getOptimisticResult(defaultedOptionsStore()))
// result = observer.getOptimisticResult(defaultedOptionsStore()) //prevent lag , somehow observer.subscribe does not return
observer.setOptions(defaultedOptions(), { listeners: false })
updateResult(observer.getOptimisticResult(defaultedOptions()))
})

const final_ = $state({ value: result })

// update result
$effect(() => {
// svelte does not need this with it is proxy state and fine-grained reactivity?
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (result !== null)
untrack(() => {
const v = !defaultedOptionsStore().notifyOnChangeProps
? observer.trackResult(result)
: result

final_.value = Object.assign(final_.value, v)
})
})
return final_.value
// Handle result property usage tracking
return !defaultedOptions().notifyOnChangeProps
? observer.trackResult(result)
: result
}
6 changes: 2 additions & 4 deletions packages/svelte-query/tests/createQuery/BaseExample.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@
const query = createQuery(options, queryClient)
$effect(() => {
states.value = [
...untrack(() => states.value),
$state.snapshot(query) as QueryObserverResult,
]
// @ts-expect-error
states.value = [...untrack(() => states.value), $state.snapshot(query)]
})
</script>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@
)
$effect(() => {
states.value = [
...untrack(() => states.value),
$state.snapshot(query) as QueryObserverResult,
]
// @ts-expect-error
states.value = [...untrack(() => states.value), $state.snapshot(query)]
})
</script>

Expand Down

0 comments on commit 54135bf

Please sign in to comment.