Skip to content

Commit c65e561

Browse files
authored
Merge pull request TanStack#3 from ardeora/luke/reformat
reformat with prettier
2 parents e611eab + 1e39918 commit c65e561

File tree

9 files changed

+138
-112
lines changed

9 files changed

+138
-112
lines changed
Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,56 @@
1-
import type { QueryClient } from "@tanstack/query-core";
2-
import { Component, Context, createContext, useContext, JSX, onMount, onCleanup } from "solid-js";
3-
import { ContextOptions } from "./types";
1+
import type { QueryClient } from '@tanstack/query-core'
2+
import {
3+
Component,
4+
Context,
5+
createContext,
6+
useContext,
7+
JSX,
8+
onMount,
9+
onCleanup,
10+
} from 'solid-js'
11+
import { ContextOptions } from './types'
412

513
declare global {
614
interface Window {
715
SolidQueryClientContext?: Context<QueryClient | undefined>
816
}
917
}
1018

11-
export const QueryClientContext = createContext<QueryClient>();
19+
export const QueryClientContext = createContext<QueryClient>()
1220
export const QueryClientSharingContext = createContext<boolean>(false)
1321

1422
interface Props {
15-
client: QueryClient;
16-
children: JSX.Element;
23+
client: QueryClient
24+
children: JSX.Element
1725
}
1826

1927
// Simple Query Client Context Provider
2028
export const QueryClientProvider: Component<Props> = (props) => {
2129
if (!props.client) {
22-
throw new Error("No queryClient found.");
30+
throw new Error('No queryClient found.')
2331
}
2432

25-
onMount(() => props.client.mount());
26-
onCleanup(() => props.client.unmount());
33+
onMount(() => props.client.mount())
34+
onCleanup(() => props.client.unmount())
2735

2836
return (
2937
<QueryClientContext.Provider value={props.client}>
3038
{props.children}
3139
</QueryClientContext.Provider>
32-
);
33-
};
40+
)
41+
}
3442

3543
function getQueryClientContext(
3644
context: Context<QueryClient | undefined> | undefined,
37-
contextSharing: boolean
45+
contextSharing: boolean,
3846
) {
3947
if (context) {
40-
return context;
48+
return context
4149
}
4250

4351
if (contextSharing && typeof window !== 'undefined') {
4452
if (!window.SolidQueryClientContext) {
45-
window.SolidQueryClientContext = QueryClientContext;
53+
window.SolidQueryClientContext = QueryClientContext
4654
}
4755

4856
return window.SolidQueryClientContext
@@ -53,12 +61,12 @@ function getQueryClientContext(
5361

5462
export const useQueryClient = ({ context }: ContextOptions = {}) => {
5563
const queryClient = useContext(
56-
getQueryClientContext(context, useContext(QueryClientSharingContext))
57-
);
64+
getQueryClientContext(context, useContext(QueryClientSharingContext)),
65+
)
5866

5967
if (!queryClient) {
60-
throw new Error('No QueryClient set, use QueryClientProvider to set one');
68+
throw new Error('No QueryClient set, use QueryClientProvider to set one')
6169
}
62-
63-
return queryClient;
64-
};
70+
71+
return queryClient
72+
}
Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
import { QueryObserver } from '@tanstack/query-core'
22
import type { QueryKey, QueryObserverResult } from '@tanstack/query-core'
3-
import { CreateBaseQueryOptions } from './types'
4-
import { useQueryClient } from "./QueryClientProvider";
5-
import { onMount, onCleanup, createComputed, createResource, createEffect, batch } from 'solid-js';
6-
import { createStore } from 'solid-js/store';
3+
import { CreateBaseQueryOptions } from './types'
4+
import { useQueryClient } from './QueryClientProvider'
5+
import {
6+
onMount,
7+
onCleanup,
8+
createComputed,
9+
createResource,
10+
createEffect,
11+
batch,
12+
} from 'solid-js'
13+
import { createStore } from 'solid-js/store'
714

815
// Base Query Function that is used to create the query.
916
export function createBaseQuery<
1017
TQueryFnData,
1118
TError,
1219
TData,
1320
TQueryData,
14-
TQueryKey extends QueryKey
21+
TQueryKey extends QueryKey,
1522
>(
1623
options: CreateBaseQueryOptions<
1724
TQueryFnData,
@@ -20,55 +27,57 @@ export function createBaseQuery<
2027
TQueryData,
2128
TQueryKey
2229
>,
23-
Observer: typeof QueryObserver
30+
Observer: typeof QueryObserver,
2431
): QueryObserverResult<TData, TError> {
25-
26-
const queryClient = useQueryClient();
32+
const queryClient = useQueryClient()
2733

2834
const defaultedOptions = queryClient.defaultQueryOptions(options)
29-
defaultedOptions._optimisticResults = 'optimistic';
30-
const observer = new QueryObserver(queryClient, defaultedOptions);
35+
defaultedOptions._optimisticResults = 'optimistic'
36+
const observer = new QueryObserver(queryClient, defaultedOptions)
3137

3238
const [state, setState] = createStore<QueryObserverResult<TData, TError>>(
3339
// @ts-ignore
3440
observer.getOptimisticResult(defaultedOptions),
35-
);
41+
)
3642

37-
const [ dataResource, { refetch } ] = createResource(() => {
43+
const [dataResource, { refetch }] = createResource(() => {
3844
return new Promise((resolve, reject) => {
3945
if (state.isSuccess) resolve(state.data)
40-
if (state.isError && !state.isFetching) {
46+
if (state.isError && !state.isFetching) {
4147
throw state.error
4248
}
4349
})
44-
});
50+
})
4551

4652
const unsubscribe = observer.subscribe((result) => {
47-
const reconciledResult = result;
53+
const reconciledResult = result
4854
// @ts-ignore
49-
setState(reconciledResult);
50-
refetch();
51-
});
55+
setState(reconciledResult)
56+
refetch()
57+
})
5258

53-
onCleanup(() => unsubscribe());
59+
onCleanup(() => unsubscribe())
5460

5561
onMount(() => {
56-
observer.setOptions(defaultedOptions, { listeners: false });
57-
});
62+
observer.setOptions(defaultedOptions, { listeners: false })
63+
})
5864

5965
createComputed(() => {
6066
const defaultedOptions = queryClient.defaultQueryOptions(options)
6167
observer.setOptions(defaultedOptions)
6268
})
6369

6470
const handler = {
65-
get(target: QueryObserverResult<TData, TError>, prop: (keyof QueryObserverResult<TData, TError>)): any {
71+
get(
72+
target: QueryObserverResult<TData, TError>,
73+
prop: keyof QueryObserverResult<TData, TError>,
74+
): any {
6675
if (prop === 'data') {
67-
return dataResource();
76+
return dataResource()
6877
}
69-
return Reflect.get(target, prop);
70-
}
78+
return Reflect.get(target, prop)
79+
},
7180
}
7281

73-
return new Proxy(state, handler) as QueryObserverResult<TData, TError>;
74-
}
82+
return new Proxy(state, handler) as QueryObserverResult<TData, TError>
83+
}

packages/solid-query/src/createMutation.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
CreateMutationResult,
1212
} from './types'
1313
import { createComputed, onCleanup } from 'solid-js'
14-
import { createStore } from 'solid-js/store';
14+
import { createStore } from 'solid-js/store'
1515

1616
// HOOK
1717
export function createMutation<
@@ -82,17 +82,21 @@ export function createMutation<
8282
options,
8383
)
8484

85-
const mutate: CreateMutateFunction<TData, TError, TVariables, TContext> =
86-
(variables, mutateOptions) => {
85+
const mutate: CreateMutateFunction<TData, TError, TVariables, TContext> = (
86+
variables,
87+
mutateOptions,
88+
) => {
8789
observer.mutate(variables, mutateOptions).catch(noop)
8890
}
89-
90-
const [state, setState] = createStore<CreateMutationResult<TData, TError, TVariables, TContext>>({
91+
92+
const [state, setState] = createStore<
93+
CreateMutationResult<TData, TError, TVariables, TContext>
94+
>({
9195
...observer.getCurrentResult(),
9296
mutate,
93-
mutateAsync: observer.getCurrentResult().mutate
97+
mutateAsync: observer.getCurrentResult().mutate,
9498
})
95-
99+
96100
createComputed(() => {
97101
const newParsedOptions = parseMutationArgs(arg1, arg2, arg3)
98102
setOptions(newParsedOptions)
@@ -103,7 +107,7 @@ export function createMutation<
103107
setState({
104108
...result,
105109
mutate,
106-
mutateAsync: result.mutate
110+
mutateAsync: result.mutate,
107111
})
108112
})
109113

@@ -113,4 +117,4 @@ export function createMutation<
113117
}
114118

115119
// eslint-disable-next-line @typescript-eslint/no-empty-function
116-
function noop() {}
120+
function noop() {}
Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { QueryObserver, QueryFunction } from '@tanstack/query-core';
1+
import { QueryObserver, QueryFunction } from '@tanstack/query-core'
22
import { CreateQueryOptions, CreateQueryResult, SolidQueryKey } from './types'
33
import { createComputed } from 'solid-js'
4-
import { createStore } from 'solid-js/store';
4+
import { createStore } from 'solid-js/store'
55
import { parseQueryArgs } from './utils'
6-
import { createBaseQuery } from './createBaseQuery';
6+
import { createBaseQuery } from './createBaseQuery'
77

88
// There are several ways to create a query.
99
// 1. createQuery(options: CreateQueryOptions)
@@ -14,56 +14,58 @@ export function createQuery<
1414
TQueryFnData = unknown,
1515
TError = unknown,
1616
TData = TQueryFnData,
17-
TQueryKey extends () => readonly unknown[] = SolidQueryKey
17+
TQueryKey extends () => readonly unknown[] = SolidQueryKey,
1818
>(
19-
options: CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey>
19+
options: CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
2020
): CreateQueryResult<TData, TError>
2121
export function createQuery<
2222
TQueryFnData = unknown,
2323
TError = unknown,
2424
TData = TQueryFnData,
25-
TQueryKey extends () => readonly unknown[] = SolidQueryKey
25+
TQueryKey extends () => readonly unknown[] = SolidQueryKey,
2626
>(
2727
queryKey: SolidQueryKey,
2828
options?: Omit<
2929
CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
3030
'queryKey'
31-
>
31+
>,
3232
): CreateQueryResult<TData, TError>
3333
export function createQuery<
3434
TQueryFnData = unknown,
3535
TError = unknown,
3636
TData = TQueryFnData,
37-
TQueryKey extends () => readonly unknown[] = SolidQueryKey
37+
TQueryKey extends () => readonly unknown[] = SolidQueryKey,
3838
>(
3939
queryKey: TQueryKey,
4040
queryFn: QueryFunction<TQueryFnData, ReturnType<TQueryKey>>,
4141
options?: Omit<
4242
CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
4343
'queryKey' | 'queryFn'
44-
>
44+
>,
4545
): CreateQueryResult<TData, TError>
4646
export function createQuery<
4747
TQueryFnData,
4848
TError,
4949
TData = TQueryFnData,
50-
TQueryKey extends () => readonly unknown[] = SolidQueryKey
50+
TQueryKey extends () => readonly unknown[] = SolidQueryKey,
5151
>(
5252
arg1: TQueryKey | CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
5353
arg2?:
5454
| QueryFunction<TQueryFnData, ReturnType<TQueryKey>>
5555
| CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
56-
arg3?: CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey>
56+
arg3?: CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
5757
): CreateQueryResult<TData, TError> {
5858
// The parseQuery Args functions helps normalize the arguments into the correct form.
5959
// Whatever the parameters are, they are normalized into the correct form.
60-
const [parsedOptions, setParsedOptions] = createStore(parseQueryArgs(arg1, arg2, arg3))
60+
const [parsedOptions, setParsedOptions] = createStore(
61+
parseQueryArgs(arg1, arg2, arg3),
62+
)
6163

6264
// Watch for changes in the options and update the parsed options.
6365
createComputed(() => {
6466
const newParsedOptions = parseQueryArgs(arg1, arg2, arg3)
6567
setParsedOptions(newParsedOptions)
6668
})
6769

68-
return createBaseQuery(parsedOptions, QueryObserver);
70+
return createBaseQuery(parsedOptions, QueryObserver)
6971
}

packages/solid-query/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
export * from '@tanstack/query-core'
33

44
// Solid Query
5-
export * from "./types"
5+
export * from './types'
66
// export { useQueries } from './useQueries'
77
// export type { QueriesResults, QueriesOptions } from './useQueries'
88
export { createQuery } from './createQuery'

0 commit comments

Comments
 (0)