Skip to content

Commit 5f2ccef

Browse files
sealicehndzhongfx
authored andcommitted
Add new features of emitter.off. Fixes #123
If emitter.off provides only the event, remove all listeners for that event.
1 parent 22c5dcb commit 5f2ccef

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export interface Emitter {
1818
on<T = any>(type: EventType, handler: Handler<T>): void;
1919
on(type: '*', handler: WildcardHandler): void;
2020

21-
off<T = any>(type: EventType, handler: Handler<T>): void;
21+
off<T = any>(type: EventType, handler?: Handler<T>): void;
2222
off(type: '*', handler: WildcardHandler): void;
2323

2424
emit<T = any>(type: EventType, event?: T): void;
@@ -60,10 +60,14 @@ export default function mitt(all?: EventHandlerMap): Emitter {
6060
* @param {Function} handler Handler function to remove
6161
* @memberOf mitt
6262
*/
63-
off<T = any>(type: EventType, handler: Handler<T>) {
63+
off<T = any>(type: EventType, handler?: Handler<T>) {
6464
const handlers = all.get(type);
6565
if (handlers) {
66+
if(handler){
6667
handlers.splice(handlers.indexOf(handler) >>> 0, 1);
68+
} else {
69+
all.delete(type);
70+
}
6771
}
6872
},
6973

0 commit comments

Comments
 (0)