Skip to content

Commit

Permalink
Add new features of emitter.off. Fixes developit#123
Browse files Browse the repository at this point in the history
If mitt.off provides only the event, remove all listeners for that event.
  • Loading branch information
sealice committed Jan 4, 2021
1 parent 22c5dcb commit 18b687e
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/index.ts
Expand Up @@ -18,7 +18,7 @@ export interface Emitter {
on<T = any>(type: EventType, handler: Handler<T>): void;
on(type: '*', handler: WildcardHandler): void;

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

emit<T = any>(type: EventType, event?: T): void;
Expand Down Expand Up @@ -60,10 +60,14 @@ export default function mitt(all?: EventHandlerMap): Emitter {
* @param {Function} handler Handler function to remove
* @memberOf mitt
*/
off<T = any>(type: EventType, handler: Handler<T>) {
off<T = any>(type: EventType, handler?: Handler<T>) {
const handlers = all.get(type);
if (handlers) {
if(handler){
handlers.splice(handlers.indexOf(handler) >>> 0, 1);
} else {
all.delete(type);
}
}
},

Expand Down

0 comments on commit 18b687e

Please sign in to comment.