@tanstack/solid-query 4.4.0

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.
Files changed (108) hide show
  1. package/build/lib/QueryClientProvider.d.ts +24 -0
  2. package/build/lib/QueryClientProvider.esm.js +68 -0
  3. package/build/lib/QueryClientProvider.esm.js.map +1 -0
  4. package/build/lib/QueryClientProvider.js +74 -0
  5. package/build/lib/QueryClientProvider.js.map +1 -0
  6. package/build/lib/QueryClientProvider.mjs +68 -0
  7. package/build/lib/QueryClientProvider.mjs.map +1 -0
  8. package/build/lib/createBaseQuery.d.ts +4 -0
  9. package/build/lib/createBaseQuery.esm.js +77 -0
  10. package/build/lib/createBaseQuery.esm.js.map +1 -0
  11. package/build/lib/createBaseQuery.js +81 -0
  12. package/build/lib/createBaseQuery.js.map +1 -0
  13. package/build/lib/createBaseQuery.mjs +77 -0
  14. package/build/lib/createBaseQuery.mjs.map +1 -0
  15. package/build/lib/createInfiniteQuery.d.ts +5 -0
  16. package/build/lib/createInfiniteQuery.esm.js +20 -0
  17. package/build/lib/createInfiniteQuery.esm.js.map +1 -0
  18. package/build/lib/createInfiniteQuery.js +24 -0
  19. package/build/lib/createInfiniteQuery.js.map +1 -0
  20. package/build/lib/createInfiniteQuery.mjs +20 -0
  21. package/build/lib/createInfiniteQuery.mjs.map +1 -0
  22. package/build/lib/createMutation.d.ts +6 -0
  23. package/build/lib/createMutation.esm.js +45 -0
  24. package/build/lib/createMutation.esm.js.map +1 -0
  25. package/build/lib/createMutation.js +49 -0
  26. package/build/lib/createMutation.js.map +1 -0
  27. package/build/lib/createMutation.mjs +45 -0
  28. package/build/lib/createMutation.mjs.map +1 -0
  29. package/build/lib/createQueries.d.ts +49 -0
  30. package/build/lib/createQueries.esm.js +54 -0
  31. package/build/lib/createQueries.esm.js.map +1 -0
  32. package/build/lib/createQueries.js +58 -0
  33. package/build/lib/createQueries.js.map +1 -0
  34. package/build/lib/createQueries.mjs +54 -0
  35. package/build/lib/createQueries.mjs.map +1 -0
  36. package/build/lib/createQuery.d.ts +23 -0
  37. package/build/lib/createQuery.esm.js +25 -0
  38. package/build/lib/createQuery.esm.js.map +1 -0
  39. package/build/lib/createQuery.js +29 -0
  40. package/build/lib/createQuery.js.map +1 -0
  41. package/build/lib/createQuery.mjs +25 -0
  42. package/build/lib/createQuery.mjs.map +1 -0
  43. package/build/lib/index.d.ts +10 -0
  44. package/build/lib/index.esm.js +9 -0
  45. package/build/lib/index.esm.js.map +1 -0
  46. package/build/lib/index.js +31 -0
  47. package/build/lib/index.js.map +1 -0
  48. package/build/lib/index.mjs +9 -0
  49. package/build/lib/index.mjs.map +1 -0
  50. package/build/lib/types.d.ts +47 -0
  51. package/build/lib/useIsFetching.d.ts +7 -0
  52. package/build/lib/useIsFetching.esm.js +29 -0
  53. package/build/lib/useIsFetching.esm.js.map +1 -0
  54. package/build/lib/useIsFetching.js +33 -0
  55. package/build/lib/useIsFetching.js.map +1 -0
  56. package/build/lib/useIsFetching.mjs +29 -0
  57. package/build/lib/useIsFetching.mjs.map +1 -0
  58. package/build/lib/useIsMutating.d.ts +8 -0
  59. package/build/lib/useIsMutating.esm.js +22 -0
  60. package/build/lib/useIsMutating.esm.js.map +1 -0
  61. package/build/lib/useIsMutating.js +26 -0
  62. package/build/lib/useIsMutating.js.map +1 -0
  63. package/build/lib/useIsMutating.mjs +22 -0
  64. package/build/lib/useIsMutating.mjs.map +1 -0
  65. package/build/lib/utils.d.ts +14 -0
  66. package/build/lib/utils.esm.js +63 -0
  67. package/build/lib/utils.esm.js.map +1 -0
  68. package/build/lib/utils.js +72 -0
  69. package/build/lib/utils.js.map +1 -0
  70. package/build/lib/utils.mjs +63 -0
  71. package/build/lib/utils.mjs.map +1 -0
  72. package/build/solid/QueryClientProvider.jsx +42 -0
  73. package/build/solid/createBaseQuery.js +69 -0
  74. package/build/solid/createInfiniteQuery.js +16 -0
  75. package/build/solid/createMutation.js +40 -0
  76. package/build/solid/createQueries.js +39 -0
  77. package/build/solid/createQuery.js +16 -0
  78. package/build/solid/index.js +11 -0
  79. package/build/solid/types.js +1 -0
  80. package/build/solid/useIsFetching.js +23 -0
  81. package/build/solid/useIsMutating.js +16 -0
  82. package/build/solid/utils.js +45 -0
  83. package/build/umd/index.development.js +3467 -0
  84. package/build/umd/index.development.js.map +1 -0
  85. package/build/umd/index.production.js +2 -0
  86. package/build/umd/index.production.js.map +1 -0
  87. package/package.json +52 -0
  88. package/src/QueryClientProvider.tsx +100 -0
  89. package/src/__tests__/QueryClientProvider.test.tsx +267 -0
  90. package/src/__tests__/createInfiniteQuery.test.tsx +1889 -0
  91. package/src/__tests__/createMutation.test.tsx +1205 -0
  92. package/src/__tests__/createQueries.test.tsx +1163 -0
  93. package/src/__tests__/createQuery.test.tsx +6487 -0
  94. package/src/__tests__/createQuery.types.test.tsx +160 -0
  95. package/src/__tests__/suspense.test.tsx +1093 -0
  96. package/src/__tests__/useIsFetching.test.tsx +298 -0
  97. package/src/__tests__/useIsMutating.test.tsx +301 -0
  98. package/src/__tests__/utils.tsx +75 -0
  99. package/src/createBaseQuery.ts +121 -0
  100. package/src/createInfiniteQuery.ts +116 -0
  101. package/src/createMutation.ts +131 -0
  102. package/src/createQueries.ts +203 -0
  103. package/src/createQuery.ts +157 -0
  104. package/src/index.ts +17 -0
  105. package/src/types.ts +167 -0
  106. package/src/useIsFetching.ts +58 -0
  107. package/src/useIsMutating.ts +42 -0
  108. package/src/utils.ts +85 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sources":["../../src/utils.ts"],"sourcesContent":["import type {\n SolidQueryKey,\n SolidQueryFilters,\n ParseFilterArgs,\n ParseQueryArgs,\n} from './types'\nimport type { QueryFunction, QueryOptions } from '@tanstack/query-core'\n\nexport function isQueryKey(value: unknown): value is SolidQueryKey {\n return typeof value === 'function'\n}\n\n// The parseQuery Args functions helps normalize the arguments into the correct form.\n// Whatever the parameters are, they are normalized into the correct form.\nexport function parseQueryArgs<\n TOptions extends Omit<\n QueryOptions<any, any, any, ReturnType<TQueryKey>>,\n 'queryKey'\n > & {\n queryKey?: TQueryKey\n },\n TQueryKey extends () => readonly unknown[] = SolidQueryKey,\n>(\n arg1: TQueryKey | TOptions,\n arg2?: QueryFunction<any, ReturnType<TQueryKey>> | TOptions,\n arg3?: TOptions,\n): ParseQueryArgs<TOptions, TQueryKey> {\n if (!isQueryKey(arg1)) {\n const { queryKey: solidKey, ...opts } = arg1 as any\n if (solidKey) {\n return {\n ...opts,\n queryKey: solidKey(),\n }\n }\n return arg1 as any\n }\n\n if (typeof arg2 === 'function') {\n return { ...arg3, queryKey: arg1(), queryFn: arg2 } as any\n }\n\n return { ...arg2, queryKey: arg1() } as any\n}\n\nexport function parseFilterArgs<\n TFilters extends SolidQueryFilters,\n TOptions = unknown,\n>(\n arg1?: SolidQueryKey | TFilters,\n arg2?: TFilters | TOptions,\n arg3?: TOptions,\n): [ParseFilterArgs<TFilters>, TOptions | undefined] {\n return (\n isQueryKey(arg1)\n ? [{ ...arg2, queryKey: arg1() }, arg3]\n : [{ ...arg1, queryKey: arg1?.queryKey?.() }, arg2]\n ) as [ParseFilterArgs<TFilters>, TOptions]\n}\n\nexport function shouldThrowError<T extends (...args: any[]) => boolean>(\n _useErrorBoundary: boolean | T | undefined,\n params: Parameters<T>,\n): boolean {\n // Allow useErrorBoundary function to override throwing behavior on a per-error basis\n if (typeof _useErrorBoundary === 'function') {\n return _useErrorBoundary(...params)\n }\n\n return !!_useErrorBoundary\n}\n\nexport function sleep(timeout: number): Promise<void> {\n return new Promise((resolve) => {\n setTimeout(resolve, timeout)\n })\n}\n\n/**\n * Schedules a microtask.\n * This can be useful to schedule state updates after rendering.\n */\nexport function scheduleMicrotask(callback: () => void) {\n sleep(0).then(callback)\n}\n"],"names":["isQueryKey","value","parseQueryArgs","arg1","arg2","arg3","queryKey","solidKey","opts","queryFn","parseFilterArgs","shouldThrowError","_useErrorBoundary","params","sleep","timeout","Promise","resolve","setTimeout","scheduleMic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BAAT,CACLC,iBADK,EAELC,MAFK,EAGI;AACT;AACA,EAAA,IAAI,OAAOD,iBAAP,KAA6B,UAAjC,EAA6C;AAC3C,IAAA,OAAOA,iBAAiB,CAAC,GAAGC,MAAJ,CAAxB,CAAA;AACD,GAAA;;EAED,OAAO,CAAC,CAACD,iBAAT,CAAA;AACD,CAAA;AAEM,SAASE,KAAT,CAAeC,OAAf,EAA+C;AACpD,EAAA,OAAO,IAAIC,OAAJ,CAAaC,OAAD,IAAa;AAC9BC,IAAAA,UAAU,CAACD,OAAD,EAAUF,OAAV,CAAV,CAAA;AACD,GAFM,CAAP,CAAA;AAGD,CAAA;AAED;AACA;AACA;AACA;;AACO,SAASI,iBAAT,CAA2BC,QAA3B,EAAiD;AACtDN,EAAAA,KAAK,CAAC,CAAD,CAAL,CAASO,IAAT,CAAcD,QAAd,CAAA,CAAA;AACD;;;;;;;;;"}
@@ -0,0 +1,63 @@
1
+ function isQueryKey(value) {
2
+ return typeof value === 'function';
3
+ } // The parseQuery Args functions helps normalize the arguments into the correct form.
4
+ // Whatever the parameters are, they are normalized into the correct form.
5
+
6
+ function parseQueryArgs(arg1, arg2, arg3) {
7
+ if (!isQueryKey(arg1)) {
8
+ const {
9
+ queryKey: solidKey,
10
+ ...opts
11
+ } = arg1;
12
+
13
+ if (solidKey) {
14
+ return { ...opts,
15
+ queryKey: solidKey()
16
+ };
17
+ }
18
+
19
+ return arg1;
20
+ }
21
+
22
+ if (typeof arg2 === 'function') {
23
+ return { ...arg3,
24
+ queryKey: arg1(),
25
+ queryFn: arg2
26
+ };
27
+ }
28
+
29
+ return { ...arg2,
30
+ queryKey: arg1()
31
+ };
32
+ }
33
+ function parseFilterArgs(arg1, arg2, arg3) {
34
+ return isQueryKey(arg1) ? [{ ...arg2,
35
+ queryKey: arg1()
36
+ }, arg3] : [{ ...arg1,
37
+ queryKey: arg1 == null ? void 0 : arg1.queryKey == null ? void 0 : arg1.queryKey()
38
+ }, arg2];
39
+ }
40
+ function shouldThrowError(_useErrorBoundary, params) {
41
+ // Allow useErrorBoundary function to override throwing behavior on a per-error basis
42
+ if (typeof _useErrorBoundary === 'function') {
43
+ return _useErrorBoundary(...params);
44
+ }
45
+
46
+ return !!_useErrorBoundary;
47
+ }
48
+ function sleep(timeout) {
49
+ return new Promise(resolve => {
50
+ setTimeout(resolve, timeout);
51
+ });
52
+ }
53
+ /**
54
+ * Schedules a microtask.
55
+ * This can be useful to schedule state updates after rendering.
56
+ */
57
+
58
+ function scheduleMicrotask(callback) {
59
+ sleep(0).then(callback);
60
+ }
61
+
62
+ export { isQueryKey, parseFilterArgs, parseQueryArgs, scheduleMicrotask, shouldThrowError, sleep };
63
+ //# sourceMappingURL=utils.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.mjs","sources":["../../src/utils.ts"],"sourcesContent":["import type {\n SolidQueryKey,\n SolidQueryFilters,\n ParseFilterArgs,\n ParseQueryArgs,\n} from './types'\nimport type { QueryFunction, QueryOptions } from '@tanstack/query-core'\n\nexport function isQueryKey(value: unknown): value is SolidQueryKey {\n return typeof value === 'function'\n}\n\n// The parseQuery Args functions helps normalize the arguments into the correct form.\n// Whatever the parameters are, they are normalized into the correct form.\nexport function parseQueryArgs<\n TOptions extends Omit<\n QueryOptions<any, any, any, ReturnType<TQueryKey>>,\n 'queryKey'\n > & {\n queryKey?: TQueryKey\n },\n TQueryKey extends () => readonly unknown[] = SolidQueryKey,\n>(\n arg1: TQueryKey | TOptions,\n arg2?: QueryFunction<any, ReturnType<TQueryKey>> | TOptions,\n arg3?: TOptions,\n): ParseQueryArgs<TOptions, TQueryKey> {\n if (!isQueryKey(arg1)) {\n const { queryKey: solidKey, ...opts } = arg1 as any\n if (solidKey) {\n return {\n ...opts,\n queryKey: solidKey(),\n }\n }\n return arg1 as any\n }\n\n if (typeof arg2 === 'function') {\n return { ...arg3, queryKey: arg1(), queryFn: arg2 } as any\n }\n\n return { ...arg2, queryKey: arg1() } as any\n}\n\nexport function parseFilterArgs<\n TFilters extends SolidQueryFilters,\n TOptions = unknown,\n>(\n arg1?: SolidQueryKey | TFilters,\n arg2?: TFilters | TOptions,\n arg3?: TOptions,\n): [ParseFilterArgs<TFilters>, TOptions | undefined] {\n return (\n isQueryKey(arg1)\n ? [{ ...arg2, queryKey: arg1() }, arg3]\n : [{ ...arg1, queryKey: arg1?.queryKey?.() }, arg2]\n ) as [ParseFilterArgs<TFilters>, TOptions]\n}\n\nexport function shouldThrowError<T extends (...args: any[]) => boolean>(\n _useErrorBoundary: boolean | T | undefined,\n params: Parameters<T>,\n): boolean {\n // Allow useErrorBoundary function to override throwing behavior on a per-error basis\n if (typeof _useErrorBoundary === 'function') {\n return _useErrorBoundary(...params)\n }\n\n return !!_useErrorBoundary\n}\n\nexport function sleep(timeout: number): Promise<void> {\n return new Promise((resolve) => {\n setTimeout(resolve, timeout)\n })\n}\n\n/**\n * Schedules a microtask.\n * This can be useful to schedule state updates after rendering.\n */\nexport function scheduleMicrotask(callback: () => void) {\n sleep(0).then(callback)\n}\n"],"names":["isQueryKey","value","parseQueryArgs","arg1","arg2","arg3","queryKey","solidKey","opts","queryFn","parseFilterArgs","shouldThrowError","_useErrorBoundary","params","sleep","timeout","Promise","resolve","setTimeout","scheduleMicrotask","callback","then"],"mappings":"AAQO,SAASA,UAAT,CAAoBC,KAApB,EAA4D;EACjE,OAAO,OAAOA,KAAP,KAAiB,UAAxB,CAAA;AACD;AAGD;;AACO,SAASC,cAAT,CASLC,IATK,EAULC,IAVK,EAWLC,IAXK,EAYgC;AACrC,EAAA,IAAI,CAACL,UAAU,CAACG,IAAD,CAAf,EAAuB;IACrB,MAAM;AAAEG,MAAAA,QAAQ,EAAEC,QAAZ;MAAsB,GAAGC,IAAAA;AAAzB,KAAA,GAAkCL,IAAxC,CAAA;;AACA,IAAA,IAAII,QAAJ,EAAc;MACZ,OAAO,EACL,GAAGC,IADE;AAELF,QAAAA,QAAQ,EAAEC,QAAQ,EAAA;OAFpB,CAAA;AAID,KAAA;;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,EAAAA,KAAK,CAAC,CAAD,CAAL,CAASO,IAAT,CAAcD,QAAd,CAAA,CAAA;AACD;;;;"}
@@ -0,0 +1,42 @@
1
+ import { createContext, useContext, onMount, onCleanup, mergeProps, } from 'solid-js';
2
+ export const defaultContext = createContext(undefined);
3
+ const QueryClientSharingContext = createContext(false);
4
+ // If we are given a context, we will use it.
5
+ // Otherwise, if contextSharing is on, we share the first and at least one
6
+ // instance of the context across the window
7
+ // to ensure that if Solid Query is used across
8
+ // different bundles or microfrontends they will
9
+ // all use the same **instance** of context, regardless
10
+ // of module scoping.
11
+ function getQueryClientContext(context, contextSharing) {
12
+ if (context) {
13
+ return context;
14
+ }
15
+ if (contextSharing && typeof window !== 'undefined') {
16
+ if (!window.SolidQueryClientContext) {
17
+ window.SolidQueryClientContext = defaultContext;
18
+ }
19
+ return window.SolidQueryClientContext;
20
+ }
21
+ return defaultContext;
22
+ }
23
+ export const useQueryClient = ({ context } = {}) => {
24
+ const queryClient = useContext(getQueryClientContext(context, useContext(QueryClientSharingContext)));
25
+ if (!queryClient) {
26
+ throw new Error('No QueryClient set, use QueryClientProvider to set one');
27
+ }
28
+ return queryClient;
29
+ };
30
+ export const QueryClientProvider = (props) => {
31
+ const mergedProps = mergeProps({
32
+ contextSharing: false,
33
+ }, props);
34
+ onMount(() => mergedProps.client.mount());
35
+ onCleanup(() => mergedProps.client.unmount());
36
+ const QueryClientContext = getQueryClientContext(mergedProps.context, mergedProps.contextSharing);
37
+ return (<QueryClientSharingContext.Provider value={!mergedProps.context && mergedProps.contextSharing}>
38
+ <QueryClientContext.Provider value={mergedProps.client}>
39
+ {mergedProps.children}
40
+ </QueryClientContext.Provider>
41
+ </QueryClientSharingContext.Provider>);
42
+ };
@@ -0,0 +1,69 @@
1
+ import { useQueryClient } from './QueryClientProvider';
2
+ import { onMount, onCleanup, createComputed, createResource, on, batch, } from 'solid-js';
3
+ import { createStore, unwrap } from 'solid-js/store';
4
+ import { shouldThrowError } from './utils';
5
+ // Base Query Function that is used to create the query.
6
+ export function createBaseQuery(options, Observer) {
7
+ const queryClient = useQueryClient({ context: options.context });
8
+ const defaultedOptions = queryClient.defaultQueryOptions(options);
9
+ defaultedOptions._optimisticResults = 'optimistic';
10
+ const observer = new Observer(queryClient, defaultedOptions);
11
+ const [state, setState] = createStore(
12
+ // @ts-ignore
13
+ observer.getOptimisticResult(defaultedOptions));
14
+ const [dataResource, { refetch, mutate }] = createResource(() => {
15
+ return new Promise((resolve) => {
16
+ if (!(state.isFetching && state.isLoading)) {
17
+ resolve(unwrap(state.data));
18
+ }
19
+ });
20
+ });
21
+ batch(() => {
22
+ mutate(() => unwrap(state.data));
23
+ refetch();
24
+ });
25
+ let taskQueue = [];
26
+ const unsubscribe = observer.subscribe((result) => {
27
+ taskQueue.push(() => {
28
+ batch(() => {
29
+ setState(unwrap(result));
30
+ mutate(() => unwrap(result.data));
31
+ refetch();
32
+ });
33
+ });
34
+ queueMicrotask(() => {
35
+ const taskToRun = taskQueue.pop();
36
+ if (taskToRun) {
37
+ taskToRun();
38
+ }
39
+ taskQueue = [];
40
+ });
41
+ });
42
+ onCleanup(() => unsubscribe());
43
+ onMount(() => {
44
+ observer.setOptions(defaultedOptions, { listeners: false });
45
+ });
46
+ createComputed(() => {
47
+ const newDefaultedOptions = queryClient.defaultQueryOptions(options);
48
+ observer.setOptions(newDefaultedOptions);
49
+ });
50
+ createComputed(on(() => state.status, () => {
51
+ if (state.isError &&
52
+ !state.isFetching &&
53
+ shouldThrowError(observer.options.useErrorBoundary, [
54
+ state.error,
55
+ observer.getCurrentQuery(),
56
+ ])) {
57
+ throw state.error;
58
+ }
59
+ }));
60
+ const handler = {
61
+ get(target, prop) {
62
+ if (prop === 'data' && target.isLoading && target.isFetching) {
63
+ return dataResource();
64
+ }
65
+ return Reflect.get(target, prop);
66
+ },
67
+ };
68
+ return new Proxy(state, handler);
69
+ }
@@ -0,0 +1,16 @@
1
+ import { InfiniteQueryObserver } from '@tanstack/query-core';
2
+ import { createBaseQuery } from './createBaseQuery';
3
+ import { createComputed } from 'solid-js';
4
+ import { createStore } from 'solid-js/store';
5
+ import { parseQueryArgs } from './utils';
6
+ export function createInfiniteQuery(arg1, arg2, arg3) {
7
+ // The parseQuery Args functions helps normalize the arguments into the correct form.
8
+ // Whatever the parameters are, they are normalized into the correct form.
9
+ const [parsedOptions, setParsedOptions] = createStore(parseQueryArgs(arg1, arg2, arg3));
10
+ // Watch for changes in the options and update the parsed options.
11
+ createComputed(() => {
12
+ const newParsedOptions = parseQueryArgs(arg1, arg2, arg3);
13
+ setParsedOptions(newParsedOptions);
14
+ });
15
+ return createBaseQuery(parsedOptions, InfiniteQueryObserver);
16
+ }
@@ -0,0 +1,40 @@
1
+ import { parseMutationArgs, MutationObserver } from '@tanstack/query-core';
2
+ import { useQueryClient } from './QueryClientProvider';
3
+ import { createComputed, onCleanup, on } from 'solid-js';
4
+ import { createStore } from 'solid-js/store';
5
+ import { shouldThrowError } from './utils';
6
+ export function createMutation(arg1, arg2, arg3) {
7
+ const [options, setOptions] = createStore(parseMutationArgs(arg1, arg2, arg3));
8
+ const queryClient = useQueryClient({ context: options.context });
9
+ const observer = new MutationObserver(queryClient, options);
10
+ const mutate = (variables, mutateOptions) => {
11
+ observer.mutate(variables, mutateOptions).catch(noop);
12
+ };
13
+ const [state, setState] = createStore({
14
+ ...observer.getCurrentResult(),
15
+ mutate,
16
+ mutateAsync: observer.getCurrentResult().mutate,
17
+ });
18
+ createComputed(() => {
19
+ const newParsedOptions = parseMutationArgs(arg1, arg2, arg3);
20
+ setOptions(newParsedOptions);
21
+ observer.setOptions(newParsedOptions);
22
+ });
23
+ createComputed(on(() => state.status, () => {
24
+ if (state.isError &&
25
+ shouldThrowError(observer.options.useErrorBoundary, [state.error])) {
26
+ throw state.error;
27
+ }
28
+ }));
29
+ const unsubscribe = observer.subscribe((result) => {
30
+ setState({
31
+ ...result,
32
+ mutate,
33
+ mutateAsync: result.mutate,
34
+ });
35
+ });
36
+ onCleanup(unsubscribe);
37
+ return state;
38
+ }
39
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
40
+ function noop() { }
@@ -0,0 +1,39 @@
1
+ import { createComputed, onCleanup, onMount } from 'solid-js';
2
+ import { QueriesObserver } from '@tanstack/query-core';
3
+ import { useQueryClient } from './QueryClientProvider';
4
+ import { createStore, unwrap } from 'solid-js/store';
5
+ import { scheduleMicrotask } from './utils';
6
+ export function createQueries(queriesOptions) {
7
+ const queryClient = useQueryClient({ context: queriesOptions.context });
8
+ const normalizeOptions = (options) => {
9
+ const normalizedOptions = { ...options, queryKey: options.queryKey?.() };
10
+ const defaultedOptions = queryClient.defaultQueryOptions(normalizedOptions);
11
+ defaultedOptions._optimisticResults = 'optimistic';
12
+ return defaultedOptions;
13
+ };
14
+ const defaultedQueries = queriesOptions.queries.map((options) => normalizeOptions(options));
15
+ const observer = new QueriesObserver(queryClient, defaultedQueries);
16
+ const [state, setState] = createStore(observer.getOptimisticResult(defaultedQueries));
17
+ const taskQueue = [];
18
+ const unsubscribe = observer.subscribe((result) => {
19
+ taskQueue.push(() => {
20
+ setState(unwrap(result));
21
+ });
22
+ scheduleMicrotask(() => {
23
+ const taskToRun = taskQueue.pop();
24
+ if (taskToRun) {
25
+ taskToRun();
26
+ taskQueue.splice(0, taskQueue.length);
27
+ }
28
+ });
29
+ });
30
+ onCleanup(unsubscribe);
31
+ onMount(() => {
32
+ observer.setQueries(defaultedQueries, { listeners: false });
33
+ });
34
+ createComputed(() => {
35
+ const updateDefaultedQueries = queriesOptions.queries.map((options) => normalizeOptions(options));
36
+ observer.setQueries(updateDefaultedQueries);
37
+ });
38
+ return state;
39
+ }
@@ -0,0 +1,16 @@
1
+ import { QueryObserver } from '@tanstack/query-core';
2
+ import { createComputed } from 'solid-js';
3
+ import { createStore } from 'solid-js/store';
4
+ import { parseQueryArgs } from './utils';
5
+ import { createBaseQuery } from './createBaseQuery';
6
+ export function createQuery(arg1, arg2, arg3) {
7
+ // The parseQuery Args functions helps normalize the arguments into the correct form.
8
+ // Whatever the parameters are, they are normalized into the correct form.
9
+ const [parsedOptions, setParsedOptions] = createStore(parseQueryArgs(arg1, arg2, arg3));
10
+ // Watch for changes in the options and update the parsed options.
11
+ createComputed(() => {
12
+ const newParsedOptions = parseQueryArgs(arg1, arg2, arg3);
13
+ setParsedOptions(newParsedOptions);
14
+ });
15
+ return createBaseQuery(parsedOptions, QueryObserver);
16
+ }
@@ -0,0 +1,11 @@
1
+ // Re-export core
2
+ export * from '@tanstack/query-core';
3
+ // Solid Query
4
+ export * from './types';
5
+ export { createQuery } from './createQuery';
6
+ export { defaultContext, QueryClientProvider, useQueryClient, } from './QueryClientProvider';
7
+ export { useIsFetching } from './useIsFetching';
8
+ export { useIsMutating } from './useIsMutating';
9
+ export { createMutation } from './createMutation';
10
+ export { createInfiniteQuery } from './createInfiniteQuery';
11
+ export { createQueries } from './createQueries';
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,23 @@
1
+ import { useQueryClient } from './QueryClientProvider';
2
+ import { createSignal, onCleanup, createComputed, createMemo } from 'solid-js';
3
+ import { parseFilterArgs } from './utils';
4
+ export function useIsFetching(arg1, arg2, arg3) {
5
+ const [filtersObj, optionsObj = {}] = parseFilterArgs(arg1, arg2, arg3);
6
+ const [filters, setFilters] = createSignal(filtersObj);
7
+ const [options, setOptions] = createSignal(optionsObj);
8
+ const queryClient = createMemo(() => useQueryClient({ context: options().context }));
9
+ const queryCache = createMemo(() => queryClient().getQueryCache());
10
+ const [fetches, setFetches] = createSignal(queryClient().isFetching(filters));
11
+ createComputed(() => {
12
+ const [newFiltersObj, newOptionsObj = {}] = parseFilterArgs(arg1, arg2, arg3);
13
+ setFilters(newFiltersObj);
14
+ setOptions(newOptionsObj);
15
+ });
16
+ const unsubscribe = queryCache().subscribe(() => {
17
+ setFetches(queryClient().isFetching(filters()));
18
+ });
19
+ onCleanup(() => {
20
+ unsubscribe();
21
+ });
22
+ return fetches;
23
+ }
@@ -0,0 +1,16 @@
1
+ import { parseMutationFilterArgs } from '@tanstack/query-core';
2
+ import { useQueryClient } from './QueryClientProvider';
3
+ import { createSignal, onCleanup } from 'solid-js';
4
+ export function useIsMutating(arg1, arg2, arg3) {
5
+ const [filters, options = {}] = parseMutationFilterArgs(arg1, arg2, arg3);
6
+ const queryClient = useQueryClient({ context: options.context });
7
+ const mutationCache = queryClient.getMutationCache();
8
+ const [mutations, setMutations] = createSignal(queryClient.isMutating(filters));
9
+ const unsubscribe = mutationCache.subscribe((_result) => {
10
+ setMutations(queryClient.isMutating(filters));
11
+ });
12
+ onCleanup(() => {
13
+ unsubscribe();
14
+ });
15
+ return mutations;
16
+ }
@@ -0,0 +1,45 @@
1
+ export function isQueryKey(value) {
2
+ return typeof value === 'function';
3
+ }
4
+ // The parseQuery Args functions helps normalize the arguments into the correct form.
5
+ // Whatever the parameters are, they are normalized into the correct form.
6
+ export function parseQueryArgs(arg1, arg2, arg3) {
7
+ if (!isQueryKey(arg1)) {
8
+ const { queryKey: solidKey, ...opts } = arg1;
9
+ if (solidKey) {
10
+ return {
11
+ ...opts,
12
+ queryKey: solidKey(),
13
+ };
14
+ }
15
+ return arg1;
16
+ }
17
+ if (typeof arg2 === 'function') {
18
+ return { ...arg3, queryKey: arg1(), queryFn: arg2 };
19
+ }
20
+ return { ...arg2, queryKey: arg1() };
21
+ }
22
+ export function parseFilterArgs(arg1, arg2, arg3) {
23
+ return (isQueryKey(arg1)
24
+ ? [{ ...arg2, queryKey: arg1() }, arg3]
25
+ : [{ ...arg1, queryKey: arg1?.queryKey?.() }, arg2]);
26
+ }
27
+ export function shouldThrowError(_useErrorBoundary, params) {
28
+ // Allow useErrorBoundary function to override throwing behavior on a per-error basis
29
+ if (typeof _useErrorBoundary === 'function') {
30
+ return _useErrorBoundary(...params);
31
+ }
32
+ return !!_useErrorBoundary;
33
+ }
34
+ export function sleep(timeout) {
35
+ return new Promise((resolve) => {
36
+ setTimeout(resolve, timeout);
37
+ });
38
+ }
39
+ /**
40
+ * Schedules a microtask.
41
+ * This can be useful to schedule state updates after rendering.
42
+ */
43
+ export function scheduleMicrotask(callback) {
44
+ sleep(0).then(callback);
45
+ }