-
-
Notifications
You must be signed in to change notification settings - Fork 514
/
useSubscription.ts
292 lines (258 loc) Β· 8.34 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
import { DocumentNode } from 'graphql'
import {
Ref,
ref,
watch,
isRef,
computed,
getCurrentInstance,
onBeforeUnmount,
nextTick,
} from 'vue-demi'
import {
OperationVariables,
SubscriptionOptions,
FetchResult,
Observable,
ObservableSubscription,
} from '@apollo/client/core'
import { throttle, debounce } from 'throttle-debounce'
import { ReactiveFunction } from './util/ReactiveFunction'
import { paramToRef } from './util/paramToRef'
import { paramToReactive } from './util/paramToReactive'
import { useApolloClient } from './useApolloClient'
import { useEventHook } from './util/useEventHook'
import { trackSubscription } from './util/loadingTracking'
export interface UseSubscriptionOptions <
TResult = any,
TVariables = OperationVariables
> extends Omit<SubscriptionOptions<TVariables>, 'query' | 'variables'> {
clientId?: string
enabled?: boolean
throttle?: number
debounce?: number
}
export interface UseSubscriptionReturn<TResult, TVariables> {
result: Ref<TResult>
loading: Ref<boolean>
error: Ref<Error>
start: () => void
stop: () => void
restart: () => void
document: Ref<DocumentNode>
variables: Ref<TVariables>
options: UseSubscriptionOptions<TResult, TVariables> | Ref<UseSubscriptionOptions<TResult, TVariables>>
subscription: Ref<Observable<FetchResult<TResult, Record<string, any>, Record<string, any>>>>
onResult: (fn: (param: FetchResult<TResult, Record<string, any>, Record<string, any>>) => void) => {
off: () => void
}
onError: (fn: (param: Error) => void) => {
off: () => void
}
}
/**
* Use a subscription that does not require variables or options.
* */
export function useSubscription<TResult = any>(
document: DocumentNode | Ref<DocumentNode> | ReactiveFunction<DocumentNode>
): UseSubscriptionReturn<TResult, undefined>
/**
* Use a subscription that requires options but not variables.
*/
export function useSubscription<TResult = any, TVariables extends undefined = undefined>(
document: DocumentNode | Ref<DocumentNode> | ReactiveFunction<DocumentNode>,
variables: TVariables,
options: UseSubscriptionOptions<TResult, TVariables> | Ref<UseSubscriptionOptions<TResult, TVariables>> | ReactiveFunction<UseSubscriptionOptions<TResult, TVariables>>
): UseSubscriptionReturn<TResult, TVariables>
/**
* Use a subscription that requires variables.
*/
export function useSubscription<TResult = any, TVariables extends OperationVariables = OperationVariables>(
document: DocumentNode | Ref<DocumentNode> | ReactiveFunction<DocumentNode>,
variables: TVariables | Ref<TVariables> | ReactiveFunction<TVariables>
): UseSubscriptionReturn<TResult, TVariables>
/**
* Use a subscription that has optional variables.
*/
export function useSubscription<TResult = any, TVariables extends OperationVariables = OperationVariables>(
document: DocumentNode | Ref<DocumentNode> | ReactiveFunction<DocumentNode>
): UseSubscriptionReturn<TResult, TVariables>
/**
* Use a subscription that requires variables and options.
*/
export function useSubscription<TResult = any, TVariables extends OperationVariables = OperationVariables>(
document: DocumentNode | Ref<DocumentNode> | ReactiveFunction<DocumentNode>,
variables: TVariables | Ref<TVariables> | ReactiveFunction<TVariables>,
options: UseSubscriptionOptions<TResult, TVariables> | Ref<UseSubscriptionOptions<TResult, TVariables>> | ReactiveFunction<UseSubscriptionOptions<TResult, TVariables>>
): UseSubscriptionReturn<TResult, TVariables>
export function useSubscription <
TResult,
TVariables
> (
document: DocumentNode | Ref<DocumentNode> | ReactiveFunction<DocumentNode>,
variables: TVariables | Ref<TVariables> | ReactiveFunction<TVariables> = null,
options: UseSubscriptionOptions<TResult, TVariables> | Ref<UseSubscriptionOptions<TResult, TVariables>> | ReactiveFunction<UseSubscriptionOptions<TResult, TVariables>> = null
): UseSubscriptionReturn<TResult, TVariables> {
// Is on server?
const vm: any = getCurrentInstance()
const isServer = vm?.$isServer
if (variables == null) variables = ref()
if (!options) options = {}
const documentRef = paramToRef(document)
const variablesRef = paramToRef(variables)
const optionsRef = paramToReactive(options)
const result = ref<TResult>()
const resultEvent = useEventHook<FetchResult<TResult>>()
const error = ref<Error>(null)
const errorEvent = useEventHook<Error>()
const loading = ref(false)
trackSubscription(loading)
// Apollo Client
const { resolveClient } = useApolloClient()
const subscription: Ref<Observable<FetchResult<TResult>>> = ref()
let observer: ObservableSubscription
let started = false
function start () {
if (started || !isEnabled.value || isServer) return
started = true
loading.value = true
const client = resolveClient(currentOptions.value.clientId)
subscription.value = client.subscribe<TResult, TVariables>({
query: currentDocument,
variables: currentVariables,
...currentOptions.value,
})
observer = subscription.value.subscribe({
next: onNextResult,
error: onError,
})
}
function onNextResult (fetchResult: FetchResult<TResult>) {
result.value = fetchResult.data
loading.value = false
resultEvent.trigger(fetchResult)
}
function onError (fetchError: any) {
error.value = fetchError
loading.value = false
errorEvent.trigger(fetchError)
}
function stop () {
if (!started) return
started = false
loading.value = false
if (subscription.value) {
subscription.value = null
}
if (observer) {
observer.unsubscribe()
observer = null
}
}
// Restart
let restarting = false
/**
* Queue a restart of the query (on next tick) if it is already active
*/
function baseRestart () {
if (!started || restarting) return
restarting = true
nextTick(() => {
if (started) {
stop()
start()
}
restarting = false
})
}
let debouncedRestart: Function
function updateRestartFn () {
if (currentOptions.value.throttle) {
debouncedRestart = throttle(currentOptions.value.throttle, baseRestart)
} else if (currentOptions.value.debounce) {
debouncedRestart = debounce(currentOptions.value.debounce, baseRestart)
} else {
debouncedRestart = baseRestart
}
}
function restart () {
if (!debouncedRestart) updateRestartFn()
debouncedRestart()
}
// Applying options
const currentOptions = ref<UseSubscriptionOptions<TResult, TVariables>>()
watch(() => isRef(optionsRef) ? optionsRef.value : optionsRef, value => {
if (currentOptions.value && (
currentOptions.value.throttle !== value.throttle ||
currentOptions.value.debounce !== value.debounce
)) {
updateRestartFn()
}
currentOptions.value = value
restart()
}, {
deep: true,
immediate: true
})
// Applying document
let currentDocument: DocumentNode
watch(documentRef, value => {
currentDocument = value
restart()
}, {
immediate: true
})
// Applying variables
let currentVariables: TVariables
let currentVariablesSerialized: string
watch(variablesRef, (value, oldValue) => {
const serialized = JSON.stringify(value)
if (serialized !== currentVariablesSerialized) {
currentVariables = value
restart()
}
currentVariablesSerialized = serialized
}, {
deep: true,
immediate: true
})
// Internal enabled returned to user
// @TODO Doesn't fully work yet, need to initialize with option
// const enabled = ref<boolean>()
const enabledOption = computed(() => !currentOptions.value || currentOptions.value.enabled == null || currentOptions.value.enabled)
// const isEnabled = computed(() => !!((typeof enabled.value === 'boolean' && enabled.value) && enabledOption.value))
const isEnabled = enabledOption
// watch(enabled, value => {
// if (value == null) {
// enabled.value = enabledOption.value
// }
// })
// Auto start & stop
watch(isEnabled, value => {
if (value) {
start()
} else {
stop()
}
}, {
immediate: true
})
// Teardown
onBeforeUnmount(stop)
return {
result,
loading,
error,
// @TODO doesn't fully work yet
// enabled,
start,
stop,
restart,
document: documentRef,
variables: variablesRef,
options: optionsRef,
subscription,
onResult: resultEvent.on,
onError: errorEvent.on,
}
}