Skip to content

Commit

Permalink
refactor: fix the errors indicated by eslint
Browse files Browse the repository at this point in the history
  • Loading branch information
MuXiu1997 committed Nov 26, 2023
1 parent eea6d46 commit cf4537b
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import mitt, { Emitter, EventType, Handler, WildcardHandler } from 'mitt'
import { getCurrentInstance, inject, InjectionKey, onUnmounted, provide } from 'vue-demi'
import mitt from 'mitt'
import { getCurrentInstance, inject, onUnmounted, provide } from 'vue-demi'

import type { Emitter, EventType, Handler, WildcardHandler } from 'mitt'
import type { InjectionKey } from 'vue-demi'

/**
* Extends the Emitter interface to include an `autoOff` method.
Expand All @@ -21,23 +24,23 @@ export function wrapAutoOff<Events extends Record<EventType, unknown>>(
type GenericEventHandler = Handler<Events[keyof Events]> | WildcardHandler<Events>
return Object.assign(emitter, {
autoOff<Key extends keyof Events>(type: Key, handler: GenericEventHandler) {
/* @ts-ignore */
/* @ts-expect-error - they are handled at runtime */
emitter.on(type, handler)
if (getCurrentInstance()) {
onUnmounted(() => {
/* @ts-ignore */
/* @ts-expect-error - they are handled at runtime */
emitter.off(type, handler)
})
}
},
})
}

export type WithInjectDefault<Events extends Record<EventType, unknown>> = {
export interface WithInjectDefault<Events extends Record<EventType, unknown>> {
injectDefault: AutoOffEmitter<Events> | (() => AutoOffEmitter<Events>)
}

export type WithThrowOnNoProvider = {
export interface WithThrowOnNoProvider {
throwOnNoProvider: () => Error
}

Expand Down

0 comments on commit cf4537b

Please sign in to comment.