Skip to content

Commit

Permalink
feat(useSubscription): onResult, onError
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume Chau committed Dec 1, 2019
1 parent 74ffbd0 commit d4b18db
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion packages/vue-apollo-composable/src/useSubscription.ts
Expand Up @@ -8,6 +8,7 @@ import { ReactiveFunction } from './util/ReactiveFunction'
import { paramToRef } from './util/paramToRef'
import { paramToReactive } from './util/paramToReactive'
import { useApolloClient } from './useApolloClient'
import { useEventHook } from './util/useEventHook'

export interface UseSubscriptionOptions <
TResult = any,
Expand All @@ -32,7 +33,9 @@ export function useSubscription <
const optionsRef = paramToReactive(options)

const result = ref<TResult>()
const error = ref(null)
const resultEvent = useEventHook<FetchResult<TResult>>()
const error = ref<Error>(null)
const errorEvent = useEventHook<Error>()

const loading = ref(false)

Expand Down Expand Up @@ -65,11 +68,13 @@ export function useSubscription <
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 () {
Expand Down Expand Up @@ -166,5 +171,7 @@ export function useSubscription <
variables: variablesRef,
options: optionsRef,
subscription,
onResult: resultEvent.on,
onError: errorEvent.on,
}
}

0 comments on commit d4b18db

Please sign in to comment.