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

feat: batch calls, cleanup unused code #91

Merged
merged 1 commit into from
Aug 18, 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
7 changes: 2 additions & 5 deletions src/atomWithMutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
MutationObserver,
MutationOptions,
QueryClient,
notifyManager,
} from '@tanstack/query-core'
import { Atom, Getter, atom } from 'jotai'
import { queryClientAtom } from './queryClientAtom'
Expand Down Expand Up @@ -67,17 +68,13 @@ export function atomWithMutation<

const dataAtom = atom((get) => {
const observer = get(observerAtom)
// const observable = get(observableAtom)

const currentResult = observer.getCurrentResult()
const resultAtom = atom(currentResult)

resultAtom.onMount = (set) => {
const unsubscribe = observer.subscribe((state) => {
set(state)
})
observer.subscribe(notifyManager.batchCalls(set))
return () => {
unsubscribe
observer.reset()
}
}
Expand Down
39 changes: 13 additions & 26 deletions src/baseAtomWithQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
QueryKey,
QueryObserver,
QueryObserverResult,
notifyManager,
} from '@tanstack/query-core'
import { Atom, Getter, atom } from 'jotai'
import { queryClientAtom } from './queryClientAtom'
Expand Down Expand Up @@ -31,11 +32,6 @@ export function baseAtomWithQuery<
| QueryObserverResult<TData, TError>
| Promise<QueryObserverResult<TData, TError>>
> {
const resetAtom = atom(0)
if (process.env.NODE_ENV !== 'production') {
resetAtom.debugPrivate = true
}

const clientAtom = atom(getQueryClient)
if (process.env.NODE_ENV !== 'production') {
clientAtom.debugPrivate = true
Expand Down Expand Up @@ -94,20 +90,24 @@ export function baseAtomWithQuery<
}

const dataAtom = atom((get) => {
const client = getQueryClient(get)
const observer = get(observerAtom)
const defaultedOptions = get(defaultedOptionsAtom)
const result = observer.getOptimisticResult(defaultedOptions)

const currentResult = observer.getCurrentResult()

const resultAtom = atom(currentResult)
const resultAtom = atom(result)
if (process.env.NODE_ENV !== 'production') {
resultAtom.debugPrivate = true
}

resultAtom.onMount = (set) => {
const unsubscribe = observer.subscribe((state) => {
set(state)
})
return unsubscribe
const unsubscribe = observer.subscribe(notifyManager.batchCalls(set))
return () => {
if (observer.getCurrentResult().isError) {
client.resetQueries({ queryKey: observer.getCurrentQuery().queryKey })
}
unsubscribe()
}
}

return resultAtom
Expand All @@ -120,20 +120,7 @@ export function baseAtomWithQuery<
const observer = get(observerAtom)
const defaultedOptions = get(defaultedOptionsAtom)

const client = getQueryClient(get)

resetAtom.onMount = () => {
return () => {
if (observer.getCurrentResult().isError) {
client.resetQueries({ queryKey: observer.getCurrentQuery().queryKey })
}
}
}

get(resetAtom)
get(get(dataAtom))

const result = observer.getOptimisticResult(defaultedOptions)
const result = get(get(dataAtom))

if (shouldSuspend(defaultedOptions, result, false)) {
return observer.fetchOptimistic(defaultedOptions)
Expand Down
Loading