Skip to content

Commit

Permalink
feat(createEventHook): added interface (#531)
Browse files Browse the repository at this point in the history
* feat(createEventHook): added interface

* added types for EventHookOn, EventHookOff, and EventHook trigger
  • Loading branch information
wheatjs committed May 24, 2021
1 parent 068144d commit 7b30a8b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
6 changes: 3 additions & 3 deletions packages/core/useFetch/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Ref, ref, unref, watch, computed, ComputedRef, shallowRef } from 'vue-demi'
import { Fn, MaybeRef, containsProp, createEventHook } from '@vueuse/shared'
import { Fn, MaybeRef, containsProp, createEventHook, EventHookOn } from '@vueuse/shared'
import { defaultWindow } from '../_configurable'

interface UseFetchReturnBase<T> {
Expand Down Expand Up @@ -56,12 +56,12 @@ interface UseFetchReturnBase<T> {
/**
* Fires after the fetch request has finished
*/
onFetchResponse: (fn: (response: Response) => void) => { off: () => void }
onFetchResponse: EventHookOn<Response>

/**
* Fires after a fetch request error
*/
onFetchError: (fn: (error: any) => void) => { off: () => void }
onFetchError: EventHookOn
}

type DataType = 'text' | 'json' | 'blob' | 'arrayBuffer' | 'formData'
Expand Down
14 changes: 12 additions & 2 deletions packages/shared/createEventHook/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
/**
* The source code for this function was inspiried by vue-apollo's `useEventHook` util
* The source code for this function was inspired by vue-apollo's `useEventHook` util
* https://github.com/vuejs/vue-apollo/blob/v4/packages/vue-apollo-composable/src/util/useEventHook.ts
*/

export type EventHookOn<T = any> = (fn: (param: T) => void) => { off: () => void }
export type EventHookOff<T = any> = (fn: (param: T) => void) => void
export type EventHookTrigger<T = any> = (param: T) => void

export interface EventHook<T = any> {
on: EventHookOn<T>
off: EventHookOff<T>
trigger: EventHookTrigger<T>
}

/**
* Utility for creating event hooks
*
* @see https://vueuse.org/createEventHook
*/
export function createEventHook<T = any>() {
export function createEventHook<T = any>(): EventHook<T> {
const fns: Array<(param: T) => void> = []

const off = (fn: (param: T) => void) => {
Expand Down

0 comments on commit 7b30a8b

Please sign in to comment.