@tanstack/svelte-query 5.0.0-alpha.0 → 5.0.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/lib/createBaseQuery.d.ts +2 -2
- package/build/lib/createBaseQuery.js +23 -19
- package/build/lib/createInfiniteQuery.d.ts +2 -2
- package/build/lib/createInfiniteQuery.js +3 -1
- package/build/lib/createMutation.d.ts +2 -2
- package/build/lib/createMutation.js +6 -5
- package/build/lib/createQueries.d.ts +2 -2
- package/build/lib/createQueries.js +10 -10
- package/build/lib/createQuery.d.ts +3 -3
- package/build/lib/types.d.ts +2 -1
- package/build/lib/utils.d.ts +3 -0
- package/build/lib/utils.js +3 -0
- package/package.json +2 -2
- package/src/__tests__/CreateQueries.svelte +1 -1
- package/src/__tests__/CreateQuery.svelte +13 -2
- package/src/__tests__/createQuery.test.ts +36 -0
- package/src/createBaseQuery.ts +43 -36
- package/src/createInfiniteQuery.ts +11 -7
- package/src/createMutation.ts +13 -7
- package/src/createQueries.ts +13 -13
- package/src/createQuery.ts +10 -3
- package/src/types.ts +3 -1
- package/src/utils.ts +8 -0
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import type { QueryClient, QueryKey, QueryObserver } from '@tanstack/query-core';
|
|
2
|
-
import type { CreateBaseQueryOptions, CreateBaseQueryResult } from './types';
|
|
3
|
-
export declare function createBaseQuery<TQueryFnData, TError, TData, TQueryData, TQueryKey extends QueryKey>(options: CreateBaseQueryOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey
|
|
2
|
+
import type { CreateBaseQueryOptions, CreateBaseQueryResult, WritableOrVal } from './types';
|
|
3
|
+
export declare function createBaseQuery<TQueryFnData, TError, TData, TQueryData, TQueryKey extends QueryKey>(options: WritableOrVal<CreateBaseQueryOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey>>, Observer: typeof QueryObserver, queryClient?: QueryClient): CreateBaseQueryResult<TData, TError>;
|
|
@@ -1,33 +1,37 @@
|
|
|
1
1
|
import { notifyManager } from '@tanstack/query-core';
|
|
2
2
|
import { useQueryClient } from './useQueryClient';
|
|
3
|
-
import { derived, readable } from 'svelte/store';
|
|
3
|
+
import { derived, get, readable, writable } from 'svelte/store';
|
|
4
|
+
import { isWritable } from './utils';
|
|
4
5
|
export function createBaseQuery(options, Observer, queryClient) {
|
|
5
6
|
const client = useQueryClient(queryClient);
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
7
|
+
const optionsStore = isWritable(options) ? options : writable(options);
|
|
8
|
+
const defaultedOptionsStore = derived(optionsStore, ($options) => {
|
|
9
|
+
const defaultedOptions = client.defaultQueryOptions($options);
|
|
10
|
+
defaultedOptions._optimisticResults = 'optimistic';
|
|
11
|
+
// Include callbacks in batch renders
|
|
12
|
+
if (defaultedOptions.onError) {
|
|
13
|
+
defaultedOptions.onError = notifyManager.batchCalls(defaultedOptions.onError);
|
|
14
|
+
}
|
|
15
|
+
if (defaultedOptions.onSuccess) {
|
|
16
|
+
defaultedOptions.onSuccess = notifyManager.batchCalls(defaultedOptions.onSuccess);
|
|
17
|
+
}
|
|
18
|
+
if (defaultedOptions.onSettled) {
|
|
19
|
+
defaultedOptions.onSettled = notifyManager.batchCalls(defaultedOptions.onSettled);
|
|
20
|
+
}
|
|
21
|
+
return defaultedOptions;
|
|
22
|
+
});
|
|
23
|
+
const observer = new Observer(client, get(defaultedOptionsStore));
|
|
24
|
+
defaultedOptionsStore.subscribe(($defaultedOptions) => {
|
|
21
25
|
// Do not notify on updates because of changes in the options because
|
|
22
26
|
// these changes should already be reflected in the optimistic result.
|
|
23
|
-
observer.setOptions(defaultedOptions, { listeners: false });
|
|
27
|
+
observer.setOptions($defaultedOptions, { listeners: false });
|
|
24
28
|
});
|
|
25
29
|
const result = readable(observer.getCurrentResult(), (set) => {
|
|
26
30
|
return observer.subscribe(notifyManager.batchCalls(set));
|
|
27
31
|
});
|
|
28
32
|
const { subscribe } = derived(result, ($result) => {
|
|
29
|
-
$result = observer.getOptimisticResult(
|
|
30
|
-
return !
|
|
33
|
+
$result = observer.getOptimisticResult(get(defaultedOptionsStore));
|
|
34
|
+
return !get(defaultedOptionsStore).notifyOnChangeProps
|
|
31
35
|
? observer.trackResult($result)
|
|
32
36
|
: $result;
|
|
33
37
|
});
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import type { QueryKey, QueryClient, RegisteredError, InfiniteData } from '@tanstack/query-core';
|
|
2
|
-
import type { CreateInfiniteQueryOptions, CreateInfiniteQueryResult } from './types';
|
|
3
|
-
export declare function createInfiniteQuery<TQueryFnData, TError = RegisteredError, TData = InfiniteData<TQueryFnData>, TQueryKey extends QueryKey = QueryKey, TPageParam = unknown>(options: CreateInfiniteQueryOptions<TQueryFnData, TError, TData, TQueryFnData, TQueryKey, TPageParam
|
|
2
|
+
import type { CreateInfiniteQueryOptions, CreateInfiniteQueryResult, WritableOrVal } from './types';
|
|
3
|
+
export declare function createInfiniteQuery<TQueryFnData, TError = RegisteredError, TData = InfiniteData<TQueryFnData>, TQueryKey extends QueryKey = QueryKey, TPageParam = unknown>(options: WritableOrVal<CreateInfiniteQueryOptions<TQueryFnData, TError, TData, TQueryFnData, TQueryKey, TPageParam>>, queryClient?: QueryClient): CreateInfiniteQueryResult<TData, TError>;
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { InfiniteQueryObserver } from '@tanstack/query-core';
|
|
2
2
|
import { createBaseQuery } from './createBaseQuery';
|
|
3
3
|
export function createInfiniteQuery(options, queryClient) {
|
|
4
|
-
return createBaseQuery(options,
|
|
4
|
+
return createBaseQuery(options,
|
|
5
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
|
|
6
|
+
InfiniteQueryObserver, queryClient);
|
|
5
7
|
}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import type { QueryClient, RegisteredError } from '@tanstack/query-core';
|
|
2
|
-
import type { CreateMutationOptions, CreateMutationResult } from './types';
|
|
3
|
-
export declare function createMutation<TData = unknown, TError = RegisteredError, TVariables = void, TContext = unknown>(options: CreateMutationOptions<TData, TError, TVariables, TContext
|
|
2
|
+
import type { CreateMutationOptions, CreateMutationResult, WritableOrVal } from './types';
|
|
3
|
+
export declare function createMutation<TData = unknown, TError = RegisteredError, TVariables = void, TContext = unknown>(options: WritableOrVal<CreateMutationOptions<TData, TError, TVariables, TContext>>, queryClient?: QueryClient): CreateMutationResult<TData, TError, TVariables, TContext>;
|
|
@@ -1,16 +1,17 @@
|
|
|
1
|
-
import { readable, derived } from 'svelte/store';
|
|
1
|
+
import { readable, derived, writable, get } from 'svelte/store';
|
|
2
2
|
import { MutationObserver, notifyManager } from '@tanstack/query-core';
|
|
3
3
|
import { useQueryClient } from './useQueryClient';
|
|
4
|
+
import { isWritable } from './utils';
|
|
4
5
|
export function createMutation(options, queryClient) {
|
|
5
6
|
const client = useQueryClient(queryClient);
|
|
6
|
-
|
|
7
|
+
const optionsStore = isWritable(options) ? options : writable(options);
|
|
8
|
+
const observer = new MutationObserver(client, get(optionsStore));
|
|
7
9
|
let mutate;
|
|
8
|
-
|
|
9
|
-
observer = $observer;
|
|
10
|
+
optionsStore.subscribe(($options) => {
|
|
10
11
|
mutate = (variables, mutateOptions) => {
|
|
11
12
|
observer.mutate(variables, mutateOptions).catch(noop);
|
|
12
13
|
};
|
|
13
|
-
observer.setOptions(options);
|
|
14
|
+
observer.setOptions($options);
|
|
14
15
|
});
|
|
15
16
|
const result = readable(observer.getCurrentResult(), (set) => {
|
|
16
17
|
return observer.subscribe(notifyManager.batchCalls((val) => set(val)));
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { QueryKey, QueryFunction, QueryClient, QueriesPlaceholderDataFunction, QueryObserverResult, RegisteredError } from '@tanstack/query-core';
|
|
2
2
|
import { type Readable } from 'svelte/store';
|
|
3
|
-
import type { CreateQueryOptions } from './types';
|
|
3
|
+
import type { CreateQueryOptions, WritableOrVal } from './types';
|
|
4
4
|
declare type CreateQueryOptionsForCreateQueries<TQueryFnData = unknown, TError = RegisteredError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey> = Omit<CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey>, 'placeholderData'> & {
|
|
5
5
|
placeholderData?: TQueryFnData | QueriesPlaceholderDataFunction<TQueryFnData>;
|
|
6
6
|
};
|
|
@@ -47,7 +47,7 @@ export declare type QueriesOptions<T extends any[], Result extends any[] = [], D
|
|
|
47
47
|
export declare type QueriesResults<T extends any[], Result extends any[] = [], Depth extends ReadonlyArray<number> = []> = Depth['length'] extends MAXIMUM_DEPTH ? QueryObserverResult[] : T extends [] ? [] : T extends [infer Head] ? [...Result, GetResults<Head>] : T extends [infer Head, ...infer Tail] ? QueriesResults<[...Tail], [...Result, GetResults<Head>], [...Depth, 1]> : T extends CreateQueryOptionsForCreateQueries<infer TQueryFnData, infer TError, infer TData, any>[] ? QueryObserverResult<unknown extends TData ? TQueryFnData : TData, unknown extends TError ? RegisteredError : TError>[] : QueryObserverResult[];
|
|
48
48
|
export declare type CreateQueriesResult<T extends any[]> = Readable<QueriesResults<T>>;
|
|
49
49
|
export declare function createQueries<T extends any[]>({ queries, queryClient, }: {
|
|
50
|
-
queries:
|
|
50
|
+
queries: WritableOrVal<[...QueriesOptions<T>]>;
|
|
51
51
|
queryClient?: QueryClient;
|
|
52
52
|
}): CreateQueriesResult<T>;
|
|
53
53
|
export {};
|
|
@@ -1,26 +1,26 @@
|
|
|
1
1
|
import { notifyManager, QueriesObserver } from '@tanstack/query-core';
|
|
2
|
-
import { readable } from 'svelte/store';
|
|
2
|
+
import { derived, get, readable, writable } from 'svelte/store';
|
|
3
3
|
import { useQueryClient } from './useQueryClient';
|
|
4
|
+
import { isWritable } from './utils';
|
|
4
5
|
export function createQueries({ queries, queryClient, }) {
|
|
5
6
|
const client = useQueryClient(queryClient);
|
|
6
7
|
// const isRestoring = useIsRestoring()
|
|
7
|
-
|
|
8
|
-
|
|
8
|
+
const queriesStore = isWritable(queries) ? queries : writable(queries);
|
|
9
|
+
const defaultedQueriesStore = derived(queriesStore, ($queries) => {
|
|
10
|
+
return $queries.map((options) => {
|
|
9
11
|
const defaultedOptions = client.defaultQueryOptions(options);
|
|
10
12
|
// Make sure the results are already in fetching state before subscribing or updating options
|
|
11
13
|
defaultedOptions._optimisticResults = 'optimistic';
|
|
12
14
|
return defaultedOptions;
|
|
13
15
|
});
|
|
14
|
-
}
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
readable(observer).subscribe(($observer) => {
|
|
18
|
-
observer = $observer;
|
|
16
|
+
});
|
|
17
|
+
const observer = new QueriesObserver(client, get(defaultedQueriesStore));
|
|
18
|
+
defaultedQueriesStore.subscribe(($defaultedQueries) => {
|
|
19
19
|
// Do not notify on updates because of changes in the options because
|
|
20
20
|
// these changes should already be reflected in the optimistic result.
|
|
21
|
-
observer.setQueries(defaultedQueries, { listeners: false });
|
|
21
|
+
observer.setQueries($defaultedQueries, { listeners: false });
|
|
22
22
|
});
|
|
23
|
-
const { subscribe } = readable(observer.getOptimisticResult(
|
|
23
|
+
const { subscribe } = readable(observer.getOptimisticResult(get(defaultedQueriesStore)), (set) => {
|
|
24
24
|
return observer.subscribe(notifyManager.batchCalls(set));
|
|
25
25
|
});
|
|
26
26
|
return { subscribe };
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import type { QueryKey, QueryClient, RegisteredError } from '@tanstack/query-core';
|
|
2
|
-
import type { DefinedCreateQueryResult, CreateQueryOptions, CreateQueryResult } from './types';
|
|
2
|
+
import type { DefinedCreateQueryResult, CreateQueryOptions, CreateQueryResult, WritableOrVal } from './types';
|
|
3
3
|
declare type UndefinedInitialDataOptions<TQueryFnData = unknown, TError = RegisteredError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey> = CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey> & {
|
|
4
4
|
initialData?: undefined;
|
|
5
5
|
};
|
|
6
6
|
declare type DefinedInitialDataOptions<TQueryFnData = unknown, TError = RegisteredError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey> = CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey> & {
|
|
7
7
|
initialData: TQueryFnData | (() => TQueryFnData);
|
|
8
8
|
};
|
|
9
|
-
export declare function createQuery<TQueryFnData = unknown, TError = RegisteredError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey>(options: UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey
|
|
10
|
-
export declare function createQuery<TQueryFnData = unknown, TError = RegisteredError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey>(options: DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey
|
|
9
|
+
export declare function createQuery<TQueryFnData = unknown, TError = RegisteredError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey>(options: WritableOrVal<UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>>, queryClient?: QueryClient): CreateQueryResult<TData, TError>;
|
|
10
|
+
export declare function createQuery<TQueryFnData = unknown, TError = RegisteredError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey>(options: WritableOrVal<DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>>, queryClient?: QueryClient): DefinedCreateQueryResult<TData, TError>;
|
|
11
11
|
export {};
|
package/build/lib/types.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { InfiniteQueryObserverOptions, InfiniteQueryObserverResult, MutationObserverResult, QueryObserverOptions, QueryObserverResult, QueryKey, MutationObserverOptions, MutateFunction, DefinedQueryObserverResult, RegisteredError } from '@tanstack/query-core';
|
|
2
2
|
import type { QueryClient } from '@tanstack/query-core';
|
|
3
|
-
import type { Readable } from 'svelte/store';
|
|
3
|
+
import type { Readable, Writable } from 'svelte/store';
|
|
4
|
+
export declare type WritableOrVal<T> = T | Writable<T>;
|
|
4
5
|
export interface ContextOptions {
|
|
5
6
|
/**
|
|
6
7
|
* Use this to pass your Svelte Query context. Otherwise, `defaultContext` will be used.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/svelte-query",
|
|
3
|
-
"version": "5.0.0-alpha.
|
|
3
|
+
"version": "5.0.0-alpha.1",
|
|
4
4
|
"description": "Primitives for managing, caching and syncing asynchronous and remote data in Svelte",
|
|
5
5
|
"author": "Dre Johnson",
|
|
6
6
|
"license": "MIT",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"vitest": "^0.27.1"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@tanstack/query-core": "5.0.0-alpha.
|
|
31
|
+
"@tanstack/query-core": "5.0.0-alpha.1"
|
|
32
32
|
},
|
|
33
33
|
"peerDependencies": {
|
|
34
34
|
"svelte": "^3.54.0"
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
import { setQueryClientContext } from '../context'
|
|
4
4
|
import type { QueriesOptions } from '../createQueries'
|
|
5
5
|
|
|
6
|
-
export let options: { queries:
|
|
6
|
+
export let options: { queries: [...QueriesOptions<any>] }
|
|
7
7
|
|
|
8
8
|
const queryClient = new QueryClient()
|
|
9
9
|
setQueryClientContext(queryClient)
|
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
createQuery,
|
|
4
|
+
QueryClient,
|
|
5
|
+
type CreateQueryOptions,
|
|
6
|
+
type WritableOrVal,
|
|
7
|
+
} from '../index'
|
|
3
8
|
import { setQueryClientContext } from '../context'
|
|
4
9
|
|
|
5
|
-
export let options: CreateQueryOptions<any
|
|
10
|
+
export let options: WritableOrVal<CreateQueryOptions<any>>
|
|
6
11
|
|
|
7
12
|
const queryClient = new QueryClient()
|
|
8
13
|
setQueryClientContext(queryClient)
|
|
@@ -17,3 +22,9 @@
|
|
|
17
22
|
{:else if $query.isSuccess}
|
|
18
23
|
<p>Success</p>
|
|
19
24
|
{/if}
|
|
25
|
+
|
|
26
|
+
<ul>
|
|
27
|
+
{#each $query.data ?? [] as entry}
|
|
28
|
+
<li>id: {entry.id}</li>
|
|
29
|
+
{/each}
|
|
30
|
+
</ul>
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { describe, it, expect } from 'vitest'
|
|
2
2
|
import { render, waitFor } from '@testing-library/svelte'
|
|
3
|
+
import { writable } from 'svelte/store'
|
|
3
4
|
import CreateQuery from './CreateQuery.svelte'
|
|
4
5
|
import { sleep } from './utils'
|
|
6
|
+
import type { CreateQueryOptions, WritableOrVal } from '../types'
|
|
5
7
|
|
|
6
8
|
describe('createQuery', () => {
|
|
7
9
|
it('Render and wait for success', async () => {
|
|
@@ -25,4 +27,38 @@ describe('createQuery', () => {
|
|
|
25
27
|
expect(rendered.getByText('Success')).toBeInTheDocument()
|
|
26
28
|
})
|
|
27
29
|
})
|
|
30
|
+
|
|
31
|
+
it('should keep previous data when returned as placeholder data', async () => {
|
|
32
|
+
const options: WritableOrVal<CreateQueryOptions> = writable({
|
|
33
|
+
queryKey: ['test', [1]],
|
|
34
|
+
queryFn: async ({ queryKey }) => {
|
|
35
|
+
await sleep(50)
|
|
36
|
+
const ids = queryKey[1]
|
|
37
|
+
if (!ids || !Array.isArray(ids)) return []
|
|
38
|
+
return ids.map((id) => ({ id }))
|
|
39
|
+
},
|
|
40
|
+
placeholderData: (previousData: { id: number }[]) => previousData,
|
|
41
|
+
})
|
|
42
|
+
const rendered = render(CreateQuery, { props: { options } })
|
|
43
|
+
|
|
44
|
+
expect(rendered.queryByText('id: 1')).not.toBeInTheDocument()
|
|
45
|
+
expect(rendered.queryByText('id: 2')).not.toBeInTheDocument()
|
|
46
|
+
|
|
47
|
+
await sleep(100)
|
|
48
|
+
|
|
49
|
+
expect(rendered.queryByText('id: 1')).toBeInTheDocument()
|
|
50
|
+
expect(rendered.queryByText('id: 2')).not.toBeInTheDocument()
|
|
51
|
+
|
|
52
|
+
options.update((o) => ({ ...o, queryKey: ['test', [1, 2]] }))
|
|
53
|
+
|
|
54
|
+
await sleep(0)
|
|
55
|
+
|
|
56
|
+
expect(rendered.queryByText('id: 1')).toBeInTheDocument()
|
|
57
|
+
expect(rendered.queryByText('id: 2')).not.toBeInTheDocument()
|
|
58
|
+
|
|
59
|
+
await sleep(100)
|
|
60
|
+
|
|
61
|
+
expect(rendered.queryByText('id: 1')).toBeInTheDocument()
|
|
62
|
+
expect(rendered.queryByText('id: 2')).toBeInTheDocument()
|
|
63
|
+
})
|
|
28
64
|
})
|
package/src/createBaseQuery.ts
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
import type { QueryClient, QueryKey, QueryObserver } from '@tanstack/query-core'
|
|
2
2
|
import { notifyManager } from '@tanstack/query-core'
|
|
3
|
-
import type {
|
|
3
|
+
import type {
|
|
4
|
+
CreateBaseQueryOptions,
|
|
5
|
+
CreateBaseQueryResult,
|
|
6
|
+
WritableOrVal,
|
|
7
|
+
} from './types'
|
|
4
8
|
import { useQueryClient } from './useQueryClient'
|
|
5
|
-
import { derived, readable } from 'svelte/store'
|
|
9
|
+
import { derived, get, readable, writable } from 'svelte/store'
|
|
10
|
+
import { isWritable } from './utils'
|
|
6
11
|
|
|
7
12
|
export function createBaseQuery<
|
|
8
13
|
TQueryFnData,
|
|
@@ -11,52 +16,54 @@ export function createBaseQuery<
|
|
|
11
16
|
TQueryData,
|
|
12
17
|
TQueryKey extends QueryKey,
|
|
13
18
|
>(
|
|
14
|
-
options:
|
|
15
|
-
TQueryFnData,
|
|
16
|
-
TError,
|
|
17
|
-
TData,
|
|
18
|
-
TQueryData,
|
|
19
|
-
TQueryKey
|
|
19
|
+
options: WritableOrVal<
|
|
20
|
+
CreateBaseQueryOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey>
|
|
20
21
|
>,
|
|
21
22
|
Observer: typeof QueryObserver,
|
|
22
23
|
queryClient?: QueryClient,
|
|
23
24
|
): CreateBaseQueryResult<TData, TError> {
|
|
24
25
|
const client = useQueryClient(queryClient)
|
|
25
|
-
const defaultedOptions = client.defaultQueryOptions(options)
|
|
26
|
-
defaultedOptions._optimisticResults = 'optimistic'
|
|
27
26
|
|
|
28
|
-
|
|
27
|
+
const optionsStore = isWritable(options) ? options : writable(options)
|
|
28
|
+
|
|
29
|
+
const defaultedOptionsStore = derived(optionsStore, ($options) => {
|
|
30
|
+
const defaultedOptions = client.defaultQueryOptions($options)
|
|
31
|
+
defaultedOptions._optimisticResults = 'optimistic'
|
|
32
|
+
|
|
33
|
+
// Include callbacks in batch renders
|
|
34
|
+
if (defaultedOptions.onError) {
|
|
35
|
+
defaultedOptions.onError = notifyManager.batchCalls(
|
|
36
|
+
defaultedOptions.onError,
|
|
37
|
+
)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (defaultedOptions.onSuccess) {
|
|
41
|
+
defaultedOptions.onSuccess = notifyManager.batchCalls(
|
|
42
|
+
defaultedOptions.onSuccess,
|
|
43
|
+
)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (defaultedOptions.onSettled) {
|
|
47
|
+
defaultedOptions.onSettled = notifyManager.batchCalls(
|
|
48
|
+
defaultedOptions.onSettled,
|
|
49
|
+
)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return defaultedOptions
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
const observer = new Observer<
|
|
29
56
|
TQueryFnData,
|
|
30
57
|
TError,
|
|
31
58
|
TData,
|
|
32
59
|
TQueryData,
|
|
33
60
|
TQueryKey
|
|
34
|
-
>(client,
|
|
35
|
-
|
|
36
|
-
// Include callbacks in batch renders
|
|
37
|
-
if (defaultedOptions.onError) {
|
|
38
|
-
defaultedOptions.onError = notifyManager.batchCalls(
|
|
39
|
-
defaultedOptions.onError,
|
|
40
|
-
)
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
if (defaultedOptions.onSuccess) {
|
|
44
|
-
defaultedOptions.onSuccess = notifyManager.batchCalls(
|
|
45
|
-
defaultedOptions.onSuccess,
|
|
46
|
-
)
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
if (defaultedOptions.onSettled) {
|
|
50
|
-
defaultedOptions.onSettled = notifyManager.batchCalls(
|
|
51
|
-
defaultedOptions.onSettled,
|
|
52
|
-
)
|
|
53
|
-
}
|
|
61
|
+
>(client, get(defaultedOptionsStore))
|
|
54
62
|
|
|
55
|
-
|
|
56
|
-
observer = $observer
|
|
63
|
+
defaultedOptionsStore.subscribe(($defaultedOptions) => {
|
|
57
64
|
// Do not notify on updates because of changes in the options because
|
|
58
65
|
// these changes should already be reflected in the optimistic result.
|
|
59
|
-
observer.setOptions(defaultedOptions, { listeners: false })
|
|
66
|
+
observer.setOptions($defaultedOptions, { listeners: false })
|
|
60
67
|
})
|
|
61
68
|
|
|
62
69
|
const result = readable(observer.getCurrentResult(), (set) => {
|
|
@@ -64,8 +71,8 @@ export function createBaseQuery<
|
|
|
64
71
|
})
|
|
65
72
|
|
|
66
73
|
const { subscribe } = derived(result, ($result) => {
|
|
67
|
-
$result = observer.getOptimisticResult(
|
|
68
|
-
return !
|
|
74
|
+
$result = observer.getOptimisticResult(get(defaultedOptionsStore))
|
|
75
|
+
return !get(defaultedOptionsStore).notifyOnChangeProps
|
|
69
76
|
? observer.trackResult($result)
|
|
70
77
|
: $result
|
|
71
78
|
})
|
|
@@ -9,6 +9,7 @@ import { InfiniteQueryObserver } from '@tanstack/query-core'
|
|
|
9
9
|
import type {
|
|
10
10
|
CreateInfiniteQueryOptions,
|
|
11
11
|
CreateInfiniteQueryResult,
|
|
12
|
+
WritableOrVal,
|
|
12
13
|
} from './types'
|
|
13
14
|
import { createBaseQuery } from './createBaseQuery'
|
|
14
15
|
|
|
@@ -19,18 +20,21 @@ export function createInfiniteQuery<
|
|
|
19
20
|
TQueryKey extends QueryKey = QueryKey,
|
|
20
21
|
TPageParam = unknown,
|
|
21
22
|
>(
|
|
22
|
-
options:
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
23
|
+
options: WritableOrVal<
|
|
24
|
+
CreateInfiniteQueryOptions<
|
|
25
|
+
TQueryFnData,
|
|
26
|
+
TError,
|
|
27
|
+
TData,
|
|
28
|
+
TQueryFnData,
|
|
29
|
+
TQueryKey,
|
|
30
|
+
TPageParam
|
|
31
|
+
>
|
|
29
32
|
>,
|
|
30
33
|
queryClient?: QueryClient,
|
|
31
34
|
): CreateInfiniteQueryResult<TData, TError> {
|
|
32
35
|
return createBaseQuery(
|
|
33
36
|
options,
|
|
37
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
|
|
34
38
|
InfiniteQueryObserver as typeof QueryObserver,
|
|
35
39
|
queryClient,
|
|
36
40
|
) as CreateInfiniteQueryResult<TData, TError>
|
package/src/createMutation.ts
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
|
-
import { readable, derived } from 'svelte/store'
|
|
1
|
+
import { readable, derived, writable, get } from 'svelte/store'
|
|
2
2
|
import type { QueryClient, RegisteredError } from '@tanstack/query-core'
|
|
3
3
|
import { MutationObserver, notifyManager } from '@tanstack/query-core'
|
|
4
4
|
import type {
|
|
5
5
|
CreateMutateFunction,
|
|
6
6
|
CreateMutationOptions,
|
|
7
7
|
CreateMutationResult,
|
|
8
|
+
WritableOrVal,
|
|
8
9
|
} from './types'
|
|
9
10
|
import { useQueryClient } from './useQueryClient'
|
|
11
|
+
import { isWritable } from './utils'
|
|
10
12
|
|
|
11
13
|
export function createMutation<
|
|
12
14
|
TData = unknown,
|
|
@@ -14,22 +16,26 @@ export function createMutation<
|
|
|
14
16
|
TVariables = void,
|
|
15
17
|
TContext = unknown,
|
|
16
18
|
>(
|
|
17
|
-
options:
|
|
19
|
+
options: WritableOrVal<
|
|
20
|
+
CreateMutationOptions<TData, TError, TVariables, TContext>
|
|
21
|
+
>,
|
|
18
22
|
queryClient?: QueryClient,
|
|
19
23
|
): CreateMutationResult<TData, TError, TVariables, TContext> {
|
|
20
24
|
const client = useQueryClient(queryClient)
|
|
21
|
-
|
|
25
|
+
|
|
26
|
+
const optionsStore = isWritable(options) ? options : writable(options)
|
|
27
|
+
|
|
28
|
+
const observer = new MutationObserver<TData, TError, TVariables, TContext>(
|
|
22
29
|
client,
|
|
23
|
-
|
|
30
|
+
get(optionsStore),
|
|
24
31
|
)
|
|
25
32
|
let mutate: CreateMutateFunction<TData, TError, TVariables, TContext>
|
|
26
33
|
|
|
27
|
-
|
|
28
|
-
observer = $observer
|
|
34
|
+
optionsStore.subscribe(($options) => {
|
|
29
35
|
mutate = (variables, mutateOptions) => {
|
|
30
36
|
observer.mutate(variables, mutateOptions).catch(noop)
|
|
31
37
|
}
|
|
32
|
-
observer.setOptions(options)
|
|
38
|
+
observer.setOptions($options)
|
|
33
39
|
})
|
|
34
40
|
|
|
35
41
|
const result = readable(observer.getCurrentResult(), (set) => {
|
package/src/createQueries.ts
CHANGED
|
@@ -8,10 +8,11 @@ import type {
|
|
|
8
8
|
} from '@tanstack/query-core'
|
|
9
9
|
|
|
10
10
|
import { notifyManager, QueriesObserver } from '@tanstack/query-core'
|
|
11
|
-
import { readable, type Readable } from 'svelte/store'
|
|
11
|
+
import { derived, get, readable, writable, type Readable } from 'svelte/store'
|
|
12
12
|
|
|
13
|
-
import type { CreateQueryOptions } from './types'
|
|
13
|
+
import type { CreateQueryOptions, WritableOrVal } from './types'
|
|
14
14
|
import { useQueryClient } from './useQueryClient'
|
|
15
|
+
import { isWritable } from './utils'
|
|
15
16
|
|
|
16
17
|
// This defines the `CreateQueryOptions` that are accepted in `QueriesOptions` & `GetOptions`.
|
|
17
18
|
// `placeholderData` function does not have a parameter
|
|
@@ -155,34 +156,33 @@ export function createQueries<T extends any[]>({
|
|
|
155
156
|
queries,
|
|
156
157
|
queryClient,
|
|
157
158
|
}: {
|
|
158
|
-
queries:
|
|
159
|
+
queries: WritableOrVal<[...QueriesOptions<T>]>
|
|
159
160
|
queryClient?: QueryClient
|
|
160
161
|
}): CreateQueriesResult<T> {
|
|
161
162
|
const client = useQueryClient(queryClient)
|
|
162
163
|
// const isRestoring = useIsRestoring()
|
|
163
164
|
|
|
164
|
-
|
|
165
|
-
|
|
165
|
+
const queriesStore = isWritable(queries) ? queries : writable(queries)
|
|
166
|
+
|
|
167
|
+
const defaultedQueriesStore = derived(queriesStore, ($queries) => {
|
|
168
|
+
return $queries.map((options) => {
|
|
166
169
|
const defaultedOptions = client.defaultQueryOptions(options)
|
|
167
170
|
// Make sure the results are already in fetching state before subscribing or updating options
|
|
168
171
|
defaultedOptions._optimisticResults = 'optimistic'
|
|
169
172
|
|
|
170
173
|
return defaultedOptions
|
|
171
174
|
})
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
const defaultedQueries = getDefaultQuery(queries)
|
|
175
|
-
let observer = new QueriesObserver(client, defaultedQueries)
|
|
175
|
+
})
|
|
176
|
+
const observer = new QueriesObserver(client, get(defaultedQueriesStore))
|
|
176
177
|
|
|
177
|
-
|
|
178
|
-
observer = $observer
|
|
178
|
+
defaultedQueriesStore.subscribe(($defaultedQueries) => {
|
|
179
179
|
// Do not notify on updates because of changes in the options because
|
|
180
180
|
// these changes should already be reflected in the optimistic result.
|
|
181
|
-
observer.setQueries(defaultedQueries, { listeners: false })
|
|
181
|
+
observer.setQueries($defaultedQueries, { listeners: false })
|
|
182
182
|
})
|
|
183
183
|
|
|
184
184
|
const { subscribe } = readable(
|
|
185
|
-
observer.getOptimisticResult(
|
|
185
|
+
observer.getOptimisticResult(get(defaultedQueriesStore)) as any,
|
|
186
186
|
(set) => {
|
|
187
187
|
return observer.subscribe(notifyManager.batchCalls(set))
|
|
188
188
|
},
|
package/src/createQuery.ts
CHANGED
|
@@ -9,6 +9,7 @@ import type {
|
|
|
9
9
|
DefinedCreateQueryResult,
|
|
10
10
|
CreateQueryOptions,
|
|
11
11
|
CreateQueryResult,
|
|
12
|
+
WritableOrVal,
|
|
12
13
|
} from './types'
|
|
13
14
|
|
|
14
15
|
type UndefinedInitialDataOptions<
|
|
@@ -35,7 +36,9 @@ export function createQuery<
|
|
|
35
36
|
TData = TQueryFnData,
|
|
36
37
|
TQueryKey extends QueryKey = QueryKey,
|
|
37
38
|
>(
|
|
38
|
-
options:
|
|
39
|
+
options: WritableOrVal<
|
|
40
|
+
UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>
|
|
41
|
+
>,
|
|
39
42
|
queryClient?: QueryClient,
|
|
40
43
|
): CreateQueryResult<TData, TError>
|
|
41
44
|
|
|
@@ -45,7 +48,9 @@ export function createQuery<
|
|
|
45
48
|
TData = TQueryFnData,
|
|
46
49
|
TQueryKey extends QueryKey = QueryKey,
|
|
47
50
|
>(
|
|
48
|
-
options:
|
|
51
|
+
options: WritableOrVal<
|
|
52
|
+
DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>
|
|
53
|
+
>,
|
|
49
54
|
queryClient?: QueryClient,
|
|
50
55
|
): DefinedCreateQueryResult<TData, TError>
|
|
51
56
|
|
|
@@ -55,7 +60,9 @@ export function createQuery<
|
|
|
55
60
|
TData = TQueryFnData,
|
|
56
61
|
TQueryKey extends QueryKey = QueryKey,
|
|
57
62
|
>(
|
|
58
|
-
options:
|
|
63
|
+
options: WritableOrVal<
|
|
64
|
+
CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey>
|
|
65
|
+
>,
|
|
59
66
|
queryClient?: QueryClient,
|
|
60
67
|
) {
|
|
61
68
|
return createBaseQuery(options, QueryObserver, queryClient)
|
package/src/types.ts
CHANGED
|
@@ -11,7 +11,9 @@ import type {
|
|
|
11
11
|
RegisteredError,
|
|
12
12
|
} from '@tanstack/query-core'
|
|
13
13
|
import type { QueryClient } from '@tanstack/query-core'
|
|
14
|
-
import type { Readable } from 'svelte/store'
|
|
14
|
+
import type { Readable, Writable } from 'svelte/store'
|
|
15
|
+
|
|
16
|
+
export type WritableOrVal<T> = T | Writable<T>
|
|
15
17
|
|
|
16
18
|
export interface ContextOptions {
|
|
17
19
|
/**
|
package/src/utils.ts
ADDED