Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(svelte-5-adapter): simplify createBaseQuery #7808

Merged
merged 2 commits into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading