-
-
Notifications
You must be signed in to change notification settings - Fork 452
/
useSubscription.ts
311 lines (290 loc) · 9.93 KB
/
useSubscription.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/* eslint-disable react-hooks/exhaustive-deps */
import { pipe, subscribe, onEnd } from 'wonka';
import * as React from 'react';
import type {
GraphQLRequestParams,
AnyVariables,
CombinedError,
OperationContext,
Operation,
} from '@urql/core';
import { useClient } from '../context';
import { useRequest } from './useRequest';
import {
deferDispatch,
initialState,
computeNextState,
hasDepsChanged,
} from './state';
/** Input arguments for the {@link useSubscription} hook.
*
* @param query - The GraphQL subscription document that `useSubscription` executes.
* @param variables - The variables for the GraphQL subscription that `useSubscription` executes.
*/
export type UseSubscriptionArgs<
Variables extends AnyVariables = AnyVariables,
Data = any,
> = {
/** Prevents {@link useSubscription} from automatically starting GraphQL subscriptions.
*
* @remarks
* `pause` may be set to `true` to stop {@link useSubscription} from starting its subscription
* automatically. The hook will stop receiving updates from the {@link Client}
* and won’t start the subscription operation, until either it’s set to `false`
* or the {@link UseSubscriptionExecute} function is called.
*/
pause?: boolean;
/** Updates the {@link OperationContext} for the executed GraphQL subscription operation.
*
* @remarks
* `context` may be passed to {@link useSubscription}, to update the {@link OperationContext}
* of a subscription operation. This may be used to update the `context` that exchanges
* will receive for a single hook.
*
* Hint: This should be wrapped in a `useMemo` hook, to make sure that your
* component doesn’t infinitely update.
*
* @example
* ```ts
* const [result, reexecute] = useSubscription({
* query,
* context: useMemo(() => ({
* additionalTypenames: ['Item'],
* }), [])
* });
* ```
*/
context?: Partial<OperationContext>;
} & GraphQLRequestParams<Data, Variables>;
/** Combines previous data with an incoming subscription result’s data.
*
* @remarks
* A `SubscriptionHandler` may be passed to {@link useSubscription} to
* aggregate subscription results into a combined {@link UseSubscriptionState.data}
* value.
*
* This is useful when a subscription event delivers a single item, while
* you’d like to display a list of events.
*
* @example
* ```ts
* const NotificationsSubscription = gql`
* subscription { newNotification { id, text } }
* `;
*
* const combineNotifications = (notifications = [], data) => {
* return [...notifications, data.newNotification];
* };
*
* const [result, executeSubscription] = useSubscription(
* { query: NotificationsSubscription },
* combineNotifications,
* );
* ```
*/
export type SubscriptionHandler<T, R> = (prev: R | undefined, data: T) => R;
/** State of the current subscription, your {@link useSubscription} hook is executing.
*
* @remarks
* `UseSubscriptionState` is returned (in a tuple) by {@link useSubscription} and
* gives you the updating {@link OperationResult} of GraphQL subscriptions.
*
* If a {@link SubscriptionHandler} has been passed to `useSubscription` then
* {@link UseSubscriptionState.data} is instead the updated data as returned
* by the handler, otherwise it’s the latest result’s data.
*
* Hint: Even when the query and variables passed to {@link useSubscription} change,
* this state preserves the prior state.
*/
export interface UseSubscriptionState<
Data = any,
Variables extends AnyVariables = AnyVariables,
> {
/** Indicates whether `useSubscription`’s subscription is active.
*
* @remarks
* When `useSubscription` starts a subscription, the `fetching` flag
* is set to `true` and will remain `true` until the subscription
* completes on the API, or the {@link UseSubscriptionArgs.pause}
* flag is set to `true`.
*/
fetching: boolean;
/** Indicates that the subscription result is not fresh.
*
* @remarks
* This is mostly unused for subscriptions and will rarely affect you, and
* is more relevant for queries.
*
* @see {@link OperationResult.stale} for the source of this value.
*/
stale: boolean;
/** The {@link OperationResult.data} for the executed subscription, or data returned by a handler.
*
* @remarks
* `data` will be set to the last {@link OperationResult.data} value
* received for the subscription.
*
* It will instead be set to the values that {@link SubscriptionHandler}
* returned, if a handler has been passed to {@link useSubscription}.
*/
data?: Data;
/** The {@link OperationResult.error} for the executed subscription. */
error?: CombinedError;
/** The {@link OperationResult.extensions} for the executed mutation. */
extensions?: Record<string, any>;
/** The {@link Operation} that the current state is for.
*
* @remarks
* This is the subscription {@link Operation} that is currently active.
* When {@link UseSubscriptionState.fetching} is `true`, this is the
* last `Operation` that the current state was for.
*/
operation?: Operation<Data, Variables>;
}
/** Triggers {@link useSubscription} to reexecute a GraphQL subscription operation.
*
* @param opts - optionally, context options that will be merged with the hook's
* {@link UseSubscriptionArgs.context} options and the `Client`’s options.
*
* @remarks
* When called, {@link useSubscription} will restart the GraphQL subscription
* operation it currently holds. If {@link UseSubscriptionArgs.pause} is set
* to `true`, it will start executing the subscription.
*
* ```ts
* const [result, executeSubscription] = useSubscription({
* query,
* pause: true,
* });
*
* const start = () => {
* executeSubscription();
* };
* ```
*/
export type UseSubscriptionExecute = (opts?: Partial<OperationContext>) => void;
/** Result tuple returned by the {@link useSubscription} hook.
*
* @remarks
* Similarly to a `useState` hook’s return value,
* the first element is the {@link useSubscription}’s state,
* a {@link UseSubscriptionState} object,
* and the second is used to imperatively re-execute or start the subscription
* via a {@link UseMutationExecute} function.
*/
export type UseSubscriptionResponse<
Data = any,
Variables extends AnyVariables = AnyVariables,
> = [UseSubscriptionState<Data, Variables>, UseSubscriptionExecute];
/** Hook to run a GraphQL subscription and get updated GraphQL results.
*
* @param args - a {@link UseSubscriptionArgs} object, to pass a `query`, `variables`, and options.
* @param handler - optionally, a {@link SubscriptionHandler} function to combine multiple subscription results.
* @returns a {@link UseSubscriptionResponse} tuple of a {@link UseSubscriptionState} result, and an execute function.
*
* @remarks
* `useSubscription` allows GraphQL subscriptions to be defined and executed.
* Given {@link UseSubscriptionArgs.query}, it executes the GraphQL subscription with the
* context’s {@link Client}.
*
* The returned result updates when the `Client` has new results
* for the subscription, and `data` is updated with the result’s data
* or with the `data` that a `handler` returns.
*
* @example
* ```ts
* import { gql, useSubscription } from 'urql';
*
* const NotificationsSubscription = gql`
* subscription { newNotification { id, text } }
* `;
*
* const combineNotifications = (notifications = [], data) => {
* return [...notifications, data.newNotification];
* };
*
* const Notifications = () => {
* const [result, executeSubscription] = useSubscription(
* { query: NotificationsSubscription },
* combineNotifications,
* );
* // ...
* };
* ```
*/
export function useSubscription<
Data = any,
Result = Data,
Variables extends AnyVariables = AnyVariables,
>(
args: UseSubscriptionArgs<Variables, Data>,
handler?: SubscriptionHandler<Data, Result>
): UseSubscriptionResponse<Result, Variables> {
const client = useClient();
const request = useRequest(args.query, args.variables as Variables);
const handlerRef = React.useRef<
SubscriptionHandler<Data, Result> | undefined
>(handler);
handlerRef.current = handler;
const source = React.useMemo(
() =>
!args.pause ? client.executeSubscription(request, args.context) : null,
[client, request, args.pause, args.context]
);
const deps = [client, request, args.context, args.pause] as const;
const [state, setState] = React.useState(
() => [source, { ...initialState, fetching: !!source }, deps] as const
);
let currentResult = state[1];
if (source !== state[0] && hasDepsChanged(state[2], deps)) {
setState([
source,
(currentResult = computeNextState(state[1], { fetching: !!source })),
deps,
]);
}
React.useEffect(() => {
const updateResult = (
result: Partial<UseSubscriptionState<Data, Variables>>
) => {
deferDispatch(setState, state => {
const nextResult = computeNextState(state[1], result);
if (state[1] === nextResult) return state;
if (
handlerRef.current &&
nextResult.data != null &&
state[1].data !== nextResult.data
) {
nextResult.data = handlerRef.current(
state[1].data,
nextResult.data
) as any;
}
return [state[0], nextResult as any, state[2]];
});
};
if (state[0]) {
return pipe(
state[0],
onEnd(() => {
updateResult({ fetching: !!source });
}),
subscribe(updateResult)
).unsubscribe;
} else {
updateResult({ fetching: false });
}
}, [state[0]]);
// This is the imperative execute function passed to the user
const executeSubscription = React.useCallback(
(opts?: Partial<OperationContext>) => {
const source = client.executeSubscription(request, {
...args.context,
...opts,
});
deferDispatch(setState, state => [source, state[1], deps]);
},
[client, request, args.context, args.pause]
);
return [currentResult, executeSubscription];
}