forked from HoudiniGraphql/houdini
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseDocumentSubscription.ts
58 lines (52 loc) · 1.46 KB
/
useDocumentSubscription.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
import type { DocumentArtifact, GraphQLVariables, QueryResult } from '$houdini/lib/types'
import type { DocumentStore, SendParams } from '$houdini/runtime/client'
import type { GraphQLObject } from 'houdini'
import { useSession } from '../routing/hooks'
import useDeepCompareEffect from './useDeepCompareEffect'
import { useDocumentStore, type UseDocumentStoreParams } from './useDocumentStore'
export function useDocumentSubscription<
_Artifact extends DocumentArtifact = DocumentArtifact,
_Data extends GraphQLObject = GraphQLObject,
_Input extends GraphQLVariables = GraphQLVariables
>({
artifact,
variables,
send,
disabled,
...observeParams
}: UseDocumentStoreParams<_Artifact, _Data, _Input> & {
variables: _Input
disabled?: boolean
send?: Partial<SendParams>
}): [QueryResult<_Data, _Input> & { parent?: string | null }, DocumentStore<_Data, _Input>] {
const [storeValue, observer] = useDocumentStore<_Data, _Input>({
artifact,
...observeParams,
})
// grab the current session value
const [session] = useSession()
// whenever the variables change, we need to retrigger the query
useDeepCompareEffect(() => {
if (!disabled) {
observer.send({
variables,
session,
// TODO: metadata
metadata: {},
...send,
})
}
return () => {
if (!disabled) {
observer.cleanup()
}
}
}, [disabled, session, observer, variables ?? {}, send ?? {}])
return [
{
parent: send?.stuff?.parentID,
...storeValue,
},
observer,
]
}