deno.land / x / jotai@v1.8.4 / src / urql / atomWithQuery.ts

atomWithQuery.ts
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import type { AnyVariables, Client, OperationContext, OperationResult, RequestPolicy, TypedDocumentNode,} from '@urql/core'import { pipe, subscribe } from 'wonka'import type { Subscription } from 'wonka'import { atom } from 'jotai'import type { Getter, WritableAtom } from 'jotai'import { clientAtom } from './clientAtom'
type Timeout = ReturnType<typeof setTimeout>
/* * @deprecated use 'refetch' action */type DeprecatedAtomWithQueryAction = { type: 'reexecute' opts?: Partial<OperationContext>}
type AtomWithQueryAction = | { type: 'refetch' opts?: Partial<OperationContext> } | DeprecatedAtomWithQueryAction
type OperationResultWithData<Data, Variables extends AnyVariables> = Omit< OperationResult<Data, Variables>, 'data'> & { data: Data }
const isOperationResultWithData = <Data, Variables extends AnyVariables>( result: OperationResult<Data, Variables>): result is OperationResultWithData<Data, Variables> => 'data' in result && !result.error
type QueryArgs<Data, Variables extends AnyVariables> = { query: TypedDocumentNode<Data, Variables> | string variables: Variables requestPolicy?: RequestPolicy context?: Partial<OperationContext>}
type QueryArgsWithPause<Data, Variables extends AnyVariables> = QueryArgs< Data, Variables> & { pause: boolean }
export function atomWithQuery<Data, Variables extends AnyVariables>( createQueryArgs: (get: Getter) => QueryArgs<Data, Variables>, getClient?: (get: Getter) => Client): WritableAtom<OperationResultWithData<Data, Variables>, AtomWithQueryAction>
export function atomWithQuery<Data, Variables extends AnyVariables>( createQueryArgs: (get: Getter) => QueryArgsWithPause<Data, Variables>, getClient?: (get: Getter) => Client): WritableAtom< OperationResultWithData<Data, Variables> | null, AtomWithQueryAction>
export function atomWithQuery<Data, Variables extends AnyVariables>( createQueryArgs: (get: Getter) => QueryArgs<Data, Variables>, getClient: (get: Getter) => Client = (get) => get(clientAtom)) { type Result = OperationResult<Data, Variables> const queryResultAtom = atom((get) => { const args = createQueryArgs(get) if ((args as { pause?: boolean }).pause) { return null } const client = getClient(get) let resolve: ((result: Result) => void) | null = null const makePending = () => new Promise<Result>((r) => { resolve = r }) const resultAtom = atom<Result | Promise<Result>>(makePending()) let setResult: ((result: Result) => void) | null = null const listener = (result: Result) => { if (!resolve && !setResult) { throw new Error('setting result without mount') } if (resolve) { resolve(result) resolve = null } if (setResult) { setResult(result) } } let subscription: Subscription | null = null let timer: Timeout | undefined const startQuery = (opts?: Partial<OperationContext>) => { if (subscription) { clearTimeout(timer) subscription.unsubscribe() } subscription = pipe( client.query(args.query, args.variables, { ...(args.requestPolicy && { requestPolicy: args.requestPolicy }), ...args.context, ...opts, }), subscribe(listener) ) if (!setResult) { // not mounted yet timer = setTimeout(() => { if (subscription) { subscription.unsubscribe() subscription = null } }, 1000) } } startQuery() resultAtom.onMount = (update) => { setResult = update if (subscription) { clearTimeout(timer as Timeout) } else { startQuery() } return () => { setResult = null if (subscription) { subscription.unsubscribe() subscription = null } } } return { resultAtom, makePending, startQuery } }) const queryAtom = atom( (get) => { const queryResult = get(queryResultAtom) if (!queryResult) { return null } const { resultAtom } = queryResult const result = get(resultAtom) if (!isOperationResultWithData(result)) { throw result.error } return result }, (get, set, action: AtomWithQueryAction) => { if (action.type === 'reexecute') { console.warn( 'DEPRECATED [atomWithQuery] use refetch instead of reexecute' ) ;(action as AtomWithQueryAction).type = 'refetch' } switch (action.type) { case 'refetch': { const queryResult = get(queryResultAtom) if (!queryResult) { throw new Error('query is paused') } const { resultAtom, makePending, startQuery } = queryResult set(resultAtom, makePending()) startQuery(action.opts) return } } } ) return queryAtom}
jotai

Version Info

Tagged at
2 years ago