(
request: () => { query: string; variables?: Record<string, unknown> },
{ next, error }: SubscriptionCallbacks<T>,
deps?: DependencyList,
)
| 12 | } |
| 13 | |
| 14 | export default function useSubscription<T>( |
| 15 | request: () => { query: string; variables?: Record<string, unknown> }, |
| 16 | { next, error }: SubscriptionCallbacks<T>, |
| 17 | deps?: DependencyList, |
| 18 | ): void { |
| 19 | const { subscriptionClient, connected } = useContext(SubscriptionContext); |
| 20 | const nextRef = useRef(next); |
| 21 | const errorRef = useRef(error); |
| 22 | |
| 23 | useEffect(() => { |
| 24 | nextRef.current = next; |
| 25 | }, [next]); |
| 26 | |
| 27 | useEffect(() => { |
| 28 | errorRef.current = error; |
| 29 | }, [error]); |
| 30 | |
| 31 | useEffect(() => { |
| 32 | if (connected) { |
| 33 | return subscriptionClient.subscribe<T>(request(), { |
| 34 | next: ({ data }: Payload<T>) => { |
| 35 | nextRef.current?.(data); |
| 36 | }, |
| 37 | error: (subscribeError) => errorRef.current?.(subscribeError), |
| 38 | complete: () => {}, |
| 39 | }); |
| 40 | } |
| 41 | return undefined; |
| 42 | // @NOTE see https://dailydotdev.atlassian.net/l/cp/dK9h1zoM |
| 43 | // eslint-disable-next-line react-hooks/exhaustive-deps |
| 44 | }, [connected, ...(deps ?? [])]); |
| 45 | } |
no outgoing calls
no test coverage detected