Skip to content

Commit

Permalink
merge master
Browse files Browse the repository at this point in the history
  • Loading branch information
dai-shi committed Jul 15, 2021
2 parents a1ad23a + 868f120 commit eb1471b
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 56 deletions.
50 changes: 25 additions & 25 deletions .size-snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
}
},
"utils.js": {
"bundled": 13479,
"minified": 6775,
"gzipped": 2544,
"bundled": 13716,
"minified": 6895,
"gzipped": 2574,
"treeshaked": {
"rollup": {
"code": 28,
Expand Down Expand Up @@ -70,23 +70,23 @@
}
},
"query.js": {
"bundled": 5223,
"minified": 2285,
"gzipped": 714,
"bundled": 5444,
"minified": 2279,
"gzipped": 710,
"treeshaked": {
"rollup": {
"code": 105,
"code": 80,
"import_statements": 71
},
"webpack": {
"code": 1131
"code": 1093
}
}
},
"xstate.js": {
"bundled": 2977,
"minified": 1314,
"gzipped": 653,
"bundled": 3565,
"minified": 1470,
"gzipped": 684,
"treeshaked": {
"rollup": {
"code": 29,
Expand All @@ -98,9 +98,9 @@
}
},
"valtio.js": {
"bundled": 1235,
"minified": 598,
"gzipped": 335,
"bundled": 1333,
"minified": 642,
"gzipped": 350,
"treeshaked": {
"rollup": {
"code": 37,
Expand All @@ -112,9 +112,9 @@
}
},
"zustand.js": {
"bundled": 549,
"minified": 262,
"gzipped": 188,
"bundled": 647,
"minified": 306,
"gzipped": 204,
"treeshaked": {
"rollup": {
"code": 14,
Expand All @@ -126,9 +126,9 @@
}
},
"redux.js": {
"bundled": 458,
"minified": 220,
"gzipped": 167,
"bundled": 516,
"minified": 248,
"gzipped": 180,
"treeshaked": {
"rollup": {
"code": 14,
Expand All @@ -140,16 +140,16 @@
}
},
"urql.js": {
"bundled": 4312,
"minified": 2311,
"gzipped": 852,
"bundled": 4539,
"minified": 2369,
"gzipped": 856,
"treeshaked": {
"rollup": {
"code": 199,
"code": 170,
"import_statements": 85
},
"webpack": {
"code": 1387
"code": 1355
}
}
}
Expand Down
27 changes: 11 additions & 16 deletions src/query/atomWithInfiniteQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ export function atomWithInfiniteQuery<
TData,
TQueryData
>),
equalityFn: (
a: InfiniteData<TData>,
b: InfiniteData<TData>
) => boolean = Object.is,
getQueryClient: (get: Getter) => QueryClient = (get) => get(queryClientAtom)
): WritableAtom<InfiniteData<TData | TQueryData>, AtomWithInfiniteQueryAction> {
const queryDataAtom = atom(
Expand All @@ -63,11 +59,14 @@ export function atomWithInfiniteQuery<
>
)()
: options.initialData

const initialData = getInitialData()

const dataAtom = atom<
| InfiniteData<TData | TQueryData>
| Promise<InfiniteData<TData | TQueryData>>
>(
getInitialData() ||
initialData ||
new Promise<InfiniteData<TData>>((resolve, reject) => {
settlePromise = (data, err) => {
if (err) {
Expand All @@ -84,8 +83,6 @@ export function atomWithInfiniteQuery<
) => void = () => {
throw new Error('atomWithInfiniteQuery: setting data without mount')
}
let prevData: InfiniteData<TData> | null = null

const listener = (
result:
| QueryObserverResult<InfiniteData<TData>, TError>
Expand All @@ -100,13 +97,9 @@ export function atomWithInfiniteQuery<
}
return
}
if (
result.data === undefined ||
(prevData !== null && equalityFn(prevData, result.data))
) {
if (result.data === undefined) {
return
}
prevData = result.data
if (settlePromise) {
settlePromise(result.data)
settlePromise = null
Expand All @@ -123,10 +116,12 @@ export function atomWithInfiniteQuery<

const observer = new InfiniteQueryObserver(queryClient, defaultedOptions)

observer
.fetchOptimistic(defaultedOptions)
.then(listener)
.catch((error) => listener({ error }))
if (!initialData) {
observer
.fetchOptimistic(defaultedOptions)
.then(listener)
.catch((error) => listener({ error }))
}

dataAtom.onMount = (update) => {
setData = update
Expand Down
23 changes: 11 additions & 12 deletions src/query/atomWithQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ export function atomWithQuery<
| ((
get: Getter
) => AtomWithQueryOptions<TQueryFnData, TError, TData, TQueryData>),
equalityFn: (a: TData, b: TData) => boolean = Object.is,
getQueryClient: (get: Getter) => QueryClient = (get) => get(queryClientAtom)
): WritableAtom<TData | TQueryData, AtomWithQueryAction> {
const queryDataAtom: WritableAtom<
Expand All @@ -48,8 +47,11 @@ export function atomWithQuery<
typeof options.initialData === 'function'
? (options.initialData as InitialDataFunction<TQueryData>)()
: options.initialData

const initialData = getInitialData()

const dataAtom = atom<TData | TQueryData | Promise<TData | TQueryData>>(
getInitialData() ||
initialData ||
new Promise<TData>((resolve, reject) => {
settlePromise = (data, err) => {
if (err) {
Expand All @@ -64,7 +66,6 @@ export function atomWithQuery<
let setData: (data: TData | Promise<TData>) => void = () => {
throw new Error('atomWithQuery: setting data without mount')
}
let prevData: TData | null = null
const listener = (
result:
| QueryObserverResult<TData, TError>
Expand All @@ -79,13 +80,9 @@ export function atomWithQuery<
}
return
}
if (
result.data === undefined ||
(prevData !== null && equalityFn(prevData, result.data))
) {
if (result.data === undefined) {
return
}
prevData = result.data
if (settlePromise) {
settlePromise(result.data)
settlePromise = null
Expand All @@ -98,10 +95,12 @@ export function atomWithQuery<
defaultedOptions.staleTime = 1000
}
const observer = new QueryObserver(queryClient, defaultedOptions)
observer
.fetchOptimistic(defaultedOptions)
.then(listener)
.catch((error) => listener({ error }))
if (!initialData) {
observer
.fetchOptimistic(defaultedOptions)
.then(listener)
.catch((error) => listener({ error }))
}
dataAtom.onMount = (update) => {
setData = update
const unsubscribe = observer.subscribe(listener)
Expand Down
9 changes: 6 additions & 3 deletions tests/query/atomWithQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -271,14 +271,15 @@ it('query with enabled (#500)', async () => {
})

it('query with initialData test', async () => {
const mockFetch = jest.fn(fakeFetch)

const countAtom = atomWithQuery(() => ({
queryKey: 'count1',
queryFn: async () => {
return await fakeFetch({ count: 10 }) // will run after "initialData"
return await mockFetch({ count: 10 })
},
initialData: { response: { count: 0 } },
keepPreviousData: true, // to prevent suspense on refresh
refetchInterval: 0, // to immediately refresh (after mount) to count: 10
refetchInterval: 100,
}))
const Counter: React.FC = () => {
const [
Expand All @@ -301,5 +302,7 @@ it('query with initialData test', async () => {

// NOTE: the atom never suspends
await findByText('count: 0')
expect(mockFetch).toHaveBeenCalledTimes(0)
await findByText('count: 10')
expect(mockFetch).toHaveBeenCalledTimes(1)
})

0 comments on commit eb1471b

Please sign in to comment.