Skip to content

Commit

Permalink
feat(createEeventHook): auto dispose (#2518)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
ARZarkesh and antfu committed Dec 16, 2022
1 parent 30c5b09 commit fdd4dbf
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/shared/createEventHook/index.ts
Expand Up @@ -2,6 +2,7 @@
* 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
*/
import { tryOnScopeDispose } from '@vueuse/core'

export type EventHookOn<T = any> = (fn: (param: T) => void) => { off: () => void }
export type EventHookOff<T = any> = (fn: (param: T) => void) => void
Expand Down Expand Up @@ -30,6 +31,10 @@ export function createEventHook<T = any>(): EventHook<T> {
const on = (fn: (param: T) => void) => {
fns.push(fn)

tryOnScopeDispose(() => {
off(fn)
})

return {
off: () => off(fn),
}
Expand Down

0 comments on commit fdd4dbf

Please sign in to comment.