From b78c73a2fe6b9b80bd39198d801683ba497d0a7a Mon Sep 17 00:00:00 2001 From: Liyang Date: Sat, 12 Jun 2021 16:36:36 +0800 Subject: [PATCH] Update index.ts --- src/index.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index ae85607..773b034 100644 --- a/src/index.ts +++ b/src/index.ts @@ -18,7 +18,7 @@ export interface Emitter { on(type: EventType, handler: Handler): void; on(type: '*', handler: WildcardHandler): void; - off(type: EventType, handler: Handler): void; + off(type: EventType, handler?: Handler): void; off(type: '*', handler: WildcardHandler): void; emit(type: EventType, event?: T): void; @@ -63,7 +63,12 @@ export default function mitt(all?: EventHandlerMap): Emitter { off(type: EventType, handler: Handler) { const handlers = all.get(type); if (handlers) { - handlers.splice(handlers.indexOf(handler) >>> 0, 1); + if (!handler) { + handlers.splice(0, handlers.length); + } + else { + handlers.splice(handlers.indexOf(handler) >>> 0, 1); + } } },