@tanstack/svelte-query 5.0.0-alpha.3 → 5.0.0-alpha.31
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/__tests__/CreateMutation.svelte +10 -0
- package/build/lib/__tests__/CreateMutation.svelte.d.ts +17 -0
- package/build/lib/__tests__/CreateQueries.svelte +16 -0
- package/build/lib/__tests__/CreateQueries.svelte.d.ts +19 -0
- package/build/lib/__tests__/CreateQuery.svelte +22 -0
- package/build/lib/__tests__/CreateQuery.svelte.d.ts +17 -0
- package/build/lib/__tests__/createMutation.test.d.ts +1 -0
- package/build/lib/__tests__/createMutation.test.js +19 -0
- package/build/lib/__tests__/createQueries.test.d.ts +1 -0
- package/build/lib/__tests__/createQueries.test.js +38 -0
- package/build/lib/__tests__/createQuery.test.d.ts +1 -0
- package/build/lib/__tests__/createQuery.test.js +57 -0
- package/build/lib/__tests__/utils.d.ts +9 -0
- package/build/lib/__tests__/utils.js +33 -0
- package/build/lib/createBaseQuery.js +0 -10
- package/build/lib/createQueries.d.ts +2 -3
- package/build/lib/createQueries.js +1 -1
- package/package.json +26 -21
- package/src/createBaseQuery.ts +0 -19
- package/src/createQueries.ts +8 -7
- package/build/lib/.gitignore +0 -9
- package/src/__tests__/CreateMutation.svelte +0 -17
- package/src/__tests__/CreateQueries.svelte +0 -20
- package/src/__tests__/CreateQuery.svelte +0 -30
- package/src/__tests__/createMutation.test.ts +0 -24
- package/src/__tests__/createQueries.test.ts +0 -41
- package/src/__tests__/createQuery.test.ts +0 -64
- package/src/__tests__/utils.ts +0 -40
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
<script>import { QueryClient } from "@tanstack/query-core";
|
|
2
|
+
import { setQueryClientContext } from "../context";
|
|
3
|
+
import { createMutation } from "../createMutation";
|
|
4
|
+
export let options;
|
|
5
|
+
const queryClient = new QueryClient();
|
|
6
|
+
setQueryClientContext(queryClient);
|
|
7
|
+
const mutation = createMutation(options);
|
|
8
|
+
</script>
|
|
9
|
+
|
|
10
|
+
<button on:click={() => $mutation.mutate()}>Click</button>
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { SvelteComponentTyped } from "svelte";
|
|
2
|
+
import type { CreateMutationOptions } from '../types';
|
|
3
|
+
declare const __propDef: {
|
|
4
|
+
props: {
|
|
5
|
+
options: CreateMutationOptions;
|
|
6
|
+
};
|
|
7
|
+
events: {
|
|
8
|
+
[evt: string]: CustomEvent<any>;
|
|
9
|
+
};
|
|
10
|
+
slots: {};
|
|
11
|
+
};
|
|
12
|
+
export declare type CreateMutationProps = typeof __propDef.props;
|
|
13
|
+
export declare type CreateMutationEvents = typeof __propDef.events;
|
|
14
|
+
export declare type CreateMutationSlots = typeof __propDef.slots;
|
|
15
|
+
export default class CreateMutation extends SvelteComponentTyped<CreateMutationProps, CreateMutationEvents, CreateMutationSlots> {
|
|
16
|
+
}
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
<script>import { QueryClient } from "@tanstack/query-core";
|
|
2
|
+
import { setQueryClientContext } from "../context";
|
|
3
|
+
import { createQueries } from "../createQueries";
|
|
4
|
+
export let options;
|
|
5
|
+
const queryClient = new QueryClient();
|
|
6
|
+
setQueryClientContext(queryClient);
|
|
7
|
+
const queries = createQueries(options);
|
|
8
|
+
</script>
|
|
9
|
+
|
|
10
|
+
{#each $queries as query, index}
|
|
11
|
+
{#if query.isPending}
|
|
12
|
+
<p>Loading {index + 1}</p>
|
|
13
|
+
{:else if query.isSuccess}
|
|
14
|
+
<p>{query.data}</p>
|
|
15
|
+
{/if}
|
|
16
|
+
{/each}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { SvelteComponentTyped } from "svelte";
|
|
2
|
+
import type { QueriesOptions } from '../createQueries';
|
|
3
|
+
declare const __propDef: {
|
|
4
|
+
props: {
|
|
5
|
+
options: {
|
|
6
|
+
queries: [...QueriesOptions<any>];
|
|
7
|
+
};
|
|
8
|
+
};
|
|
9
|
+
events: {
|
|
10
|
+
[evt: string]: CustomEvent<any>;
|
|
11
|
+
};
|
|
12
|
+
slots: {};
|
|
13
|
+
};
|
|
14
|
+
export declare type CreateQueriesProps = typeof __propDef.props;
|
|
15
|
+
export declare type CreateQueriesEvents = typeof __propDef.events;
|
|
16
|
+
export declare type CreateQueriesSlots = typeof __propDef.slots;
|
|
17
|
+
export default class CreateQueries extends SvelteComponentTyped<CreateQueriesProps, CreateQueriesEvents, CreateQueriesSlots> {
|
|
18
|
+
}
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
<script>import { QueryClient } from "@tanstack/query-core";
|
|
2
|
+
import { setQueryClientContext } from "../context";
|
|
3
|
+
import { createQuery } from "../createQuery";
|
|
4
|
+
export let options;
|
|
5
|
+
const queryClient = new QueryClient();
|
|
6
|
+
setQueryClientContext(queryClient);
|
|
7
|
+
const query = createQuery(options);
|
|
8
|
+
</script>
|
|
9
|
+
|
|
10
|
+
{#if $query.isPending}
|
|
11
|
+
<p>Loading</p>
|
|
12
|
+
{:else if $query.isError}
|
|
13
|
+
<p>Error</p>
|
|
14
|
+
{:else if $query.isSuccess}
|
|
15
|
+
<p>Success</p>
|
|
16
|
+
{/if}
|
|
17
|
+
|
|
18
|
+
<ul>
|
|
19
|
+
{#each $query.data ?? [] as entry}
|
|
20
|
+
<li>id: {entry.id}</li>
|
|
21
|
+
{/each}
|
|
22
|
+
</ul>
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { SvelteComponentTyped } from "svelte";
|
|
2
|
+
import type { CreateQueryOptions, WritableOrVal } from '../types';
|
|
3
|
+
declare const __propDef: {
|
|
4
|
+
props: {
|
|
5
|
+
options: WritableOrVal<CreateQueryOptions<any>>;
|
|
6
|
+
};
|
|
7
|
+
events: {
|
|
8
|
+
[evt: string]: CustomEvent<any>;
|
|
9
|
+
};
|
|
10
|
+
slots: {};
|
|
11
|
+
};
|
|
12
|
+
export declare type CreateQueryProps = typeof __propDef.props;
|
|
13
|
+
export declare type CreateQueryEvents = typeof __propDef.events;
|
|
14
|
+
export declare type CreateQuerySlots = typeof __propDef.slots;
|
|
15
|
+
export default class CreateQuery extends SvelteComponentTyped<CreateQueryProps, CreateQueryEvents, CreateQuerySlots> {
|
|
16
|
+
}
|
|
17
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { describe, it, expect, vi } from 'vitest';
|
|
2
|
+
import { fireEvent, render, waitFor } from '@testing-library/svelte';
|
|
3
|
+
import CreateMutation from './CreateMutation.svelte';
|
|
4
|
+
describe('createMutation', () => {
|
|
5
|
+
it('Call mutate and check function runs', async () => {
|
|
6
|
+
const mutationFn = vi.fn();
|
|
7
|
+
const rendered = render(CreateMutation, {
|
|
8
|
+
props: {
|
|
9
|
+
options: {
|
|
10
|
+
mutationFn,
|
|
11
|
+
},
|
|
12
|
+
},
|
|
13
|
+
});
|
|
14
|
+
fireEvent.click(rendered.getByRole('button'));
|
|
15
|
+
await waitFor(() => {
|
|
16
|
+
expect(mutationFn).toHaveBeenCalledTimes(1);
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { render, waitFor } from '@testing-library/svelte';
|
|
3
|
+
import CreateQueries from './CreateQueries.svelte';
|
|
4
|
+
import { sleep } from './utils';
|
|
5
|
+
describe('createQueries', () => {
|
|
6
|
+
it('Render and wait for success', async () => {
|
|
7
|
+
const rendered = render(CreateQueries, {
|
|
8
|
+
props: {
|
|
9
|
+
options: {
|
|
10
|
+
queries: [
|
|
11
|
+
{
|
|
12
|
+
queryKey: ['key-1'],
|
|
13
|
+
queryFn: async () => {
|
|
14
|
+
await sleep(10);
|
|
15
|
+
return 'Success 1';
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
queryKey: ['key-2'],
|
|
20
|
+
queryFn: async () => {
|
|
21
|
+
await sleep(10);
|
|
22
|
+
return 'Success 2';
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
],
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
await waitFor(() => {
|
|
30
|
+
expect(rendered.getByText('Loading 1')).toBeInTheDocument();
|
|
31
|
+
expect(rendered.getByText('Loading 2')).toBeInTheDocument();
|
|
32
|
+
});
|
|
33
|
+
await waitFor(() => {
|
|
34
|
+
expect(rendered.getByText('Success 1')).toBeInTheDocument();
|
|
35
|
+
expect(rendered.getByText('Success 2')).toBeInTheDocument();
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { render, waitFor } from '@testing-library/svelte';
|
|
3
|
+
import { writable } from 'svelte/store';
|
|
4
|
+
import CreateQuery from './CreateQuery.svelte';
|
|
5
|
+
import { sleep } from './utils';
|
|
6
|
+
describe('createQuery', () => {
|
|
7
|
+
it('Render and wait for success', async () => {
|
|
8
|
+
const rendered = render(CreateQuery, {
|
|
9
|
+
props: {
|
|
10
|
+
options: {
|
|
11
|
+
queryKey: ['test'],
|
|
12
|
+
queryFn: async () => {
|
|
13
|
+
await sleep(10);
|
|
14
|
+
return 'Success';
|
|
15
|
+
},
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
});
|
|
19
|
+
await waitFor(() => {
|
|
20
|
+
expect(rendered.getByText('Loading')).toBeInTheDocument();
|
|
21
|
+
});
|
|
22
|
+
await waitFor(() => {
|
|
23
|
+
expect(rendered.getByText('Success')).toBeInTheDocument();
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
it('should keep previous data when returned as placeholder data', async () => {
|
|
27
|
+
const options = writable({
|
|
28
|
+
queryKey: ['test', [1]],
|
|
29
|
+
queryFn: async ({ queryKey }) => {
|
|
30
|
+
await sleep(10);
|
|
31
|
+
const ids = queryKey[1];
|
|
32
|
+
if (!ids || !Array.isArray(ids))
|
|
33
|
+
return [];
|
|
34
|
+
return ids.map((id) => ({ id }));
|
|
35
|
+
},
|
|
36
|
+
placeholderData: (previousData) => previousData,
|
|
37
|
+
});
|
|
38
|
+
const rendered = render(CreateQuery, { props: { options } });
|
|
39
|
+
await waitFor(() => {
|
|
40
|
+
expect(rendered.queryByText('id: 1')).not.toBeInTheDocument();
|
|
41
|
+
expect(rendered.queryByText('id: 2')).not.toBeInTheDocument();
|
|
42
|
+
});
|
|
43
|
+
await waitFor(() => {
|
|
44
|
+
expect(rendered.queryByText('id: 1')).toBeInTheDocument();
|
|
45
|
+
expect(rendered.queryByText('id: 2')).not.toBeInTheDocument();
|
|
46
|
+
});
|
|
47
|
+
options.update((o) => ({ ...o, queryKey: ['test', [1, 2]] }));
|
|
48
|
+
await waitFor(() => {
|
|
49
|
+
expect(rendered.queryByText('id: 1')).toBeInTheDocument();
|
|
50
|
+
expect(rendered.queryByText('id: 2')).not.toBeInTheDocument();
|
|
51
|
+
});
|
|
52
|
+
await waitFor(() => {
|
|
53
|
+
expect(rendered.queryByText('id: 1')).toBeInTheDocument();
|
|
54
|
+
expect(rendered.queryByText('id: 2')).toBeInTheDocument();
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
});
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { QueryClient, type QueryClientConfig } from '../index';
|
|
3
|
+
export declare function createQueryClient(config?: QueryClientConfig): QueryClient;
|
|
4
|
+
export declare function mockVisibilityState(value: DocumentVisibilityState): import("vitest/dist/index-1cfc7f58").S<[], DocumentVisibilityState>;
|
|
5
|
+
export declare function mockNavigatorOnLine(value: boolean): import("vitest/dist/index-1cfc7f58").S<[], boolean>;
|
|
6
|
+
export declare function queryKey(): Array<string>;
|
|
7
|
+
export declare function sleep(timeout: number): Promise<void>;
|
|
8
|
+
export declare function simplefetcher(): Promise<string>;
|
|
9
|
+
export declare function setActTimeout(fn: () => void, ms?: number): NodeJS.Timeout;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { vi } from 'vitest';
|
|
2
|
+
import { act } from '@testing-library/svelte';
|
|
3
|
+
import { QueryClient } from '../index';
|
|
4
|
+
export function createQueryClient(config) {
|
|
5
|
+
return new QueryClient(config);
|
|
6
|
+
}
|
|
7
|
+
export function mockVisibilityState(value) {
|
|
8
|
+
return vi.spyOn(document, 'visibilityState', 'get').mockReturnValue(value);
|
|
9
|
+
}
|
|
10
|
+
export function mockNavigatorOnLine(value) {
|
|
11
|
+
return vi.spyOn(navigator, 'onLine', 'get').mockReturnValue(value);
|
|
12
|
+
}
|
|
13
|
+
let queryKeyCount = 0;
|
|
14
|
+
export function queryKey() {
|
|
15
|
+
queryKeyCount++;
|
|
16
|
+
return [`query_${queryKeyCount}`];
|
|
17
|
+
}
|
|
18
|
+
export function sleep(timeout) {
|
|
19
|
+
return new Promise((resolve, _reject) => {
|
|
20
|
+
setTimeout(resolve, timeout);
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
export async function simplefetcher() {
|
|
24
|
+
await sleep(10);
|
|
25
|
+
return 'test';
|
|
26
|
+
}
|
|
27
|
+
export function setActTimeout(fn, ms) {
|
|
28
|
+
return setTimeout(() => {
|
|
29
|
+
act(() => {
|
|
30
|
+
fn();
|
|
31
|
+
});
|
|
32
|
+
}, ms);
|
|
33
|
+
}
|
|
@@ -8,16 +8,6 @@ export function createBaseQuery(options, Observer, queryClient) {
|
|
|
8
8
|
const defaultedOptionsStore = derived(optionsStore, ($options) => {
|
|
9
9
|
const defaultedOptions = client.defaultQueryOptions($options);
|
|
10
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
11
|
return defaultedOptions;
|
|
22
12
|
});
|
|
23
13
|
const observer = new Observer(client, get(defaultedOptionsStore));
|
|
@@ -46,8 +46,7 @@ export declare type QueriesOptions<T extends any[], Result extends any[] = [], D
|
|
|
46
46
|
*/
|
|
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 ? DefaultError : TError>[] : QueryObserverResult[];
|
|
48
48
|
export declare type CreateQueriesResult<T extends any[]> = Readable<QueriesResults<T>>;
|
|
49
|
-
export declare function createQueries<T extends any[]>({ queries,
|
|
49
|
+
export declare function createQueries<T extends any[]>({ queries, }: {
|
|
50
50
|
queries: WritableOrVal<[...QueriesOptions<T>]>;
|
|
51
|
-
|
|
52
|
-
}): CreateQueriesResult<T>;
|
|
51
|
+
}, queryClient?: QueryClient): CreateQueriesResult<T>;
|
|
53
52
|
export {};
|
|
@@ -2,7 +2,7 @@ import { notifyManager, QueriesObserver } from '@tanstack/query-core';
|
|
|
2
2
|
import { derived, get, readable, writable } from 'svelte/store';
|
|
3
3
|
import { useQueryClient } from './useQueryClient';
|
|
4
4
|
import { isWritable } from './utils';
|
|
5
|
-
export function createQueries({ queries,
|
|
5
|
+
export function createQueries({ queries, }, queryClient) {
|
|
6
6
|
const client = useQueryClient(queryClient);
|
|
7
7
|
// const isRestoring = useIsRestoring()
|
|
8
8
|
const queriesStore = isWritable(queries) ? queries : writable(queries);
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/svelte-query",
|
|
3
|
-
"version": "5.0.0-alpha.
|
|
3
|
+
"version": "5.0.0-alpha.31",
|
|
4
4
|
"description": "Primitives for managing, caching and syncing asynchronous and remote data in Svelte",
|
|
5
|
-
"author": "
|
|
5
|
+
"author": "Lachlan Collins",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"repository": "tanstack/query",
|
|
8
8
|
"homepage": "https://tanstack.com/query",
|
|
@@ -10,44 +10,49 @@
|
|
|
10
10
|
"type": "github",
|
|
11
11
|
"url": "https://github.com/sponsors/tannerlinsley"
|
|
12
12
|
},
|
|
13
|
+
"type": "module",
|
|
13
14
|
"types": "build/lib/index.d.ts",
|
|
14
15
|
"module": "build/lib/index.js",
|
|
15
|
-
"
|
|
16
|
+
"svelte": "build/lib/index.js",
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"types": "./build/lib/index.d.ts",
|
|
20
|
+
"import": "./build/lib/index.js",
|
|
21
|
+
"svelte": "./build/lib/index.js",
|
|
22
|
+
"default": "./build/lib/index.js"
|
|
23
|
+
},
|
|
24
|
+
"./package.json": "./package.json"
|
|
25
|
+
},
|
|
26
|
+
"files": [
|
|
27
|
+
"build/lib",
|
|
28
|
+
"src",
|
|
29
|
+
"!build/lib/__tests__",
|
|
30
|
+
"!src/__tests__"
|
|
31
|
+
],
|
|
16
32
|
"devDependencies": {
|
|
17
|
-
"@sveltejs/package": "^
|
|
33
|
+
"@sveltejs/package": "^2.0.2",
|
|
18
34
|
"@sveltejs/vite-plugin-svelte": "^2.0.2",
|
|
19
35
|
"@testing-library/svelte": "^3.2.2",
|
|
20
|
-
"@vitest/coverage-istanbul": "^0.27.1",
|
|
21
36
|
"eslint-plugin-svelte": "^2.14.1",
|
|
22
37
|
"jsdom": "^20.0.3",
|
|
23
38
|
"svelte": "^3.54.0",
|
|
24
39
|
"svelte-check": "^2.9.2",
|
|
25
40
|
"tslib": "^2.4.1",
|
|
26
41
|
"typescript": "^4.7.4",
|
|
27
|
-
"vite": "^4.0.0"
|
|
28
|
-
"vitest": "^0.27.1"
|
|
42
|
+
"vite": "^4.0.0"
|
|
29
43
|
},
|
|
30
44
|
"dependencies": {
|
|
31
|
-
"@tanstack/query-core": "5.0.0-alpha.
|
|
45
|
+
"@tanstack/query-core": "5.0.0-alpha.31"
|
|
32
46
|
},
|
|
33
47
|
"peerDependencies": {
|
|
34
48
|
"svelte": "^3.54.0"
|
|
35
49
|
},
|
|
36
|
-
"exports": {
|
|
37
|
-
"./package.json": "./package.json",
|
|
38
|
-
".": "./build/lib/index.js"
|
|
39
|
-
},
|
|
40
|
-
"svelte": "./build/lib/index.js",
|
|
41
|
-
"files": [
|
|
42
|
-
"build/lib/*",
|
|
43
|
-
"src"
|
|
44
|
-
],
|
|
45
50
|
"scripts": {
|
|
46
51
|
"clean": "rimraf ./build",
|
|
47
|
-
"test:types": "svelte-check --tsconfig ./tsconfig.json",
|
|
52
|
+
"test:types": "svelte-check --tsconfig ./tsconfig.json && pnpm build",
|
|
48
53
|
"test:eslint": "eslint --ext .svelte,.ts ./src",
|
|
49
|
-
"test:lib": "vitest run --coverage
|
|
50
|
-
"test:lib:dev": "
|
|
51
|
-
"build": "svelte-package
|
|
54
|
+
"test:lib": "vitest run --coverage",
|
|
55
|
+
"test:lib:dev": "pnpm run test:lib --watch",
|
|
56
|
+
"build": "svelte-package --input ./src --output ./build/lib"
|
|
52
57
|
}
|
|
53
58
|
}
|
package/src/createBaseQuery.ts
CHANGED
|
@@ -30,25 +30,6 @@ export function createBaseQuery<
|
|
|
30
30
|
const defaultedOptions = client.defaultQueryOptions($options)
|
|
31
31
|
defaultedOptions._optimisticResults = 'optimistic'
|
|
32
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
33
|
return defaultedOptions
|
|
53
34
|
})
|
|
54
35
|
|
package/src/createQueries.ts
CHANGED
|
@@ -152,13 +152,14 @@ export type QueriesResults<
|
|
|
152
152
|
|
|
153
153
|
export type CreateQueriesResult<T extends any[]> = Readable<QueriesResults<T>>
|
|
154
154
|
|
|
155
|
-
export function createQueries<T extends any[]>(
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
}: {
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
155
|
+
export function createQueries<T extends any[]>(
|
|
156
|
+
{
|
|
157
|
+
queries,
|
|
158
|
+
}: {
|
|
159
|
+
queries: WritableOrVal<[...QueriesOptions<T>]>
|
|
160
|
+
},
|
|
161
|
+
queryClient?: QueryClient,
|
|
162
|
+
): CreateQueriesResult<T> {
|
|
162
163
|
const client = useQueryClient(queryClient)
|
|
163
164
|
// const isRestoring = useIsRestoring()
|
|
164
165
|
|
package/build/lib/.gitignore
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
<script lang="ts">
|
|
2
|
-
import {
|
|
3
|
-
createMutation,
|
|
4
|
-
QueryClient,
|
|
5
|
-
type CreateMutationOptions,
|
|
6
|
-
} from '../index'
|
|
7
|
-
import { setQueryClientContext } from '../context'
|
|
8
|
-
|
|
9
|
-
export let options: CreateMutationOptions
|
|
10
|
-
|
|
11
|
-
const queryClient = new QueryClient()
|
|
12
|
-
setQueryClientContext(queryClient)
|
|
13
|
-
|
|
14
|
-
const mutation = createMutation(options)
|
|
15
|
-
</script>
|
|
16
|
-
|
|
17
|
-
<button on:click={() => $mutation.mutate()}>Click</button>
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
<script lang="ts">
|
|
2
|
-
import { createQueries, QueryClient } from '../index'
|
|
3
|
-
import { setQueryClientContext } from '../context'
|
|
4
|
-
import type { QueriesOptions } from '../createQueries'
|
|
5
|
-
|
|
6
|
-
export let options: { queries: [...QueriesOptions<any>] }
|
|
7
|
-
|
|
8
|
-
const queryClient = new QueryClient()
|
|
9
|
-
setQueryClientContext(queryClient)
|
|
10
|
-
|
|
11
|
-
const queries = createQueries(options)
|
|
12
|
-
</script>
|
|
13
|
-
|
|
14
|
-
{#each $queries as query, index}
|
|
15
|
-
{#if query.isPending}
|
|
16
|
-
<p>Loading {index + 1}</p>
|
|
17
|
-
{:else if query.isSuccess}
|
|
18
|
-
<p>{query.data}</p>
|
|
19
|
-
{/if}
|
|
20
|
-
{/each}
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
<script lang="ts">
|
|
2
|
-
import {
|
|
3
|
-
createQuery,
|
|
4
|
-
QueryClient,
|
|
5
|
-
type CreateQueryOptions,
|
|
6
|
-
type WritableOrVal,
|
|
7
|
-
} from '../index'
|
|
8
|
-
import { setQueryClientContext } from '../context'
|
|
9
|
-
|
|
10
|
-
export let options: WritableOrVal<CreateQueryOptions<any>>
|
|
11
|
-
|
|
12
|
-
const queryClient = new QueryClient()
|
|
13
|
-
setQueryClientContext(queryClient)
|
|
14
|
-
|
|
15
|
-
const query = createQuery(options)
|
|
16
|
-
</script>
|
|
17
|
-
|
|
18
|
-
{#if $query.isPending}
|
|
19
|
-
<p>Loading</p>
|
|
20
|
-
{:else if $query.isError}
|
|
21
|
-
<p>Error</p>
|
|
22
|
-
{:else if $query.isSuccess}
|
|
23
|
-
<p>Success</p>
|
|
24
|
-
{/if}
|
|
25
|
-
|
|
26
|
-
<ul>
|
|
27
|
-
{#each $query.data ?? [] as entry}
|
|
28
|
-
<li>id: {entry.id}</li>
|
|
29
|
-
{/each}
|
|
30
|
-
</ul>
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, vi } from 'vitest'
|
|
2
|
-
import { fireEvent, render, screen } from '@testing-library/svelte'
|
|
3
|
-
import CreateMutation from './CreateMutation.svelte'
|
|
4
|
-
import { sleep } from './utils'
|
|
5
|
-
|
|
6
|
-
describe('createMutation', () => {
|
|
7
|
-
it('Call mutate and check function runs', async () => {
|
|
8
|
-
const mutationFn = vi.fn()
|
|
9
|
-
|
|
10
|
-
render(CreateMutation, {
|
|
11
|
-
props: {
|
|
12
|
-
options: {
|
|
13
|
-
mutationFn,
|
|
14
|
-
},
|
|
15
|
-
},
|
|
16
|
-
})
|
|
17
|
-
|
|
18
|
-
fireEvent.click(screen.getByRole('button'))
|
|
19
|
-
|
|
20
|
-
await sleep(20)
|
|
21
|
-
|
|
22
|
-
expect(mutationFn).toHaveBeenCalledTimes(1)
|
|
23
|
-
})
|
|
24
|
-
})
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from 'vitest'
|
|
2
|
-
import { render, waitFor } from '@testing-library/svelte'
|
|
3
|
-
import CreateQueries from './CreateQueries.svelte'
|
|
4
|
-
import { sleep } from './utils'
|
|
5
|
-
|
|
6
|
-
describe('createQueries', () => {
|
|
7
|
-
it('Render and wait for success', async () => {
|
|
8
|
-
const rendered = render(CreateQueries, {
|
|
9
|
-
props: {
|
|
10
|
-
options: {
|
|
11
|
-
queries: [
|
|
12
|
-
{
|
|
13
|
-
queryKey: ['key-1'],
|
|
14
|
-
queryFn: async () => {
|
|
15
|
-
await sleep(10)
|
|
16
|
-
return 'Success 1'
|
|
17
|
-
},
|
|
18
|
-
},
|
|
19
|
-
{
|
|
20
|
-
queryKey: ['key-2'],
|
|
21
|
-
queryFn: async () => {
|
|
22
|
-
await sleep(10)
|
|
23
|
-
return 'Success 2'
|
|
24
|
-
},
|
|
25
|
-
},
|
|
26
|
-
],
|
|
27
|
-
},
|
|
28
|
-
},
|
|
29
|
-
})
|
|
30
|
-
|
|
31
|
-
await waitFor(() => {
|
|
32
|
-
expect(rendered.getByText('Loading 1')).toBeInTheDocument()
|
|
33
|
-
expect(rendered.getByText('Loading 2')).toBeInTheDocument()
|
|
34
|
-
})
|
|
35
|
-
|
|
36
|
-
await waitFor(() => {
|
|
37
|
-
expect(rendered.getByText('Success 1')).toBeInTheDocument()
|
|
38
|
-
expect(rendered.getByText('Success 2')).toBeInTheDocument()
|
|
39
|
-
})
|
|
40
|
-
})
|
|
41
|
-
})
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from 'vitest'
|
|
2
|
-
import { render, waitFor } from '@testing-library/svelte'
|
|
3
|
-
import { writable } from 'svelte/store'
|
|
4
|
-
import CreateQuery from './CreateQuery.svelte'
|
|
5
|
-
import { sleep } from './utils'
|
|
6
|
-
import type { CreateQueryOptions, WritableOrVal } from '../types'
|
|
7
|
-
|
|
8
|
-
describe('createQuery', () => {
|
|
9
|
-
it('Render and wait for success', async () => {
|
|
10
|
-
const rendered = render(CreateQuery, {
|
|
11
|
-
props: {
|
|
12
|
-
options: {
|
|
13
|
-
queryKey: ['test'],
|
|
14
|
-
queryFn: async () => {
|
|
15
|
-
await sleep(10)
|
|
16
|
-
return 'Success'
|
|
17
|
-
},
|
|
18
|
-
},
|
|
19
|
-
},
|
|
20
|
-
})
|
|
21
|
-
|
|
22
|
-
await waitFor(() => {
|
|
23
|
-
expect(rendered.getByText('Loading')).toBeInTheDocument()
|
|
24
|
-
})
|
|
25
|
-
|
|
26
|
-
await waitFor(() => {
|
|
27
|
-
expect(rendered.getByText('Success')).toBeInTheDocument()
|
|
28
|
-
})
|
|
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
|
-
})
|
|
64
|
-
})
|
package/src/__tests__/utils.ts
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { vi } from 'vitest'
|
|
2
|
-
import { act } from '@testing-library/svelte'
|
|
3
|
-
import { QueryClient, type QueryClientConfig } from '../index'
|
|
4
|
-
|
|
5
|
-
export function createQueryClient(config?: QueryClientConfig): QueryClient {
|
|
6
|
-
return new QueryClient(config)
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export function mockVisibilityState(value: DocumentVisibilityState) {
|
|
10
|
-
return vi.spyOn(document, 'visibilityState', 'get').mockReturnValue(value)
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export function mockNavigatorOnLine(value: boolean) {
|
|
14
|
-
return vi.spyOn(navigator, 'onLine', 'get').mockReturnValue(value)
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
let queryKeyCount = 0
|
|
18
|
-
export function queryKey(): Array<string> {
|
|
19
|
-
queryKeyCount++
|
|
20
|
-
return [`query_${queryKeyCount}`]
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export function sleep(timeout: number): Promise<void> {
|
|
24
|
-
return new Promise((resolve, _reject) => {
|
|
25
|
-
setTimeout(resolve, timeout)
|
|
26
|
-
})
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export async function simplefetcher() {
|
|
30
|
-
await sleep(10)
|
|
31
|
-
return 'test'
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export function setActTimeout(fn: () => void, ms?: number) {
|
|
35
|
-
return setTimeout(() => {
|
|
36
|
-
act(() => {
|
|
37
|
-
fn()
|
|
38
|
-
})
|
|
39
|
-
}, ms)
|
|
40
|
-
}
|