aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsealice <[email protected]>2021-01-04 10:02:11 +0800
committerhndzhongfx <hndzhongfx>2021-01-04 10:14:53 +0800
commit5f2ccef47bb1d8656433b529573212273b82bf83 (patch)
tree19928aafdd7eda073e8a04fda12e879df5d96c34
parent22c5dcba10736aecb1f39ee88d9f85278108c988 (diff)
Add new features of emitter.off. Fixes #123
If emitter.off provides only the event, remove all listeners for that event.
-rw-r--r--src/index.ts8
1 files changed, 6 insertions, 2 deletions
diff --git a/src/index.ts b/src/index.ts
index ae85607..394a5ed 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -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;
@@ -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);
+ }
}
},