Skip to content

Commit fa9a207

Browse files
on now returns a deregistration function
1 parent 6b41670 commit fa9a207

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

src/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,18 @@ export default function mitt<Events extends Record<EventType, unknown>>(
5151
| WildcardHandler<Events>;
5252
all = all || new Map();
5353

54-
return {
54+
const mittInstance = {
5555
/**
5656
* A Map of event names to registered handler functions.
5757
*/
5858
all,
5959

6060
/**
6161
* Register an event handler for the given type.
62+
* Returns a deregistration function.
6263
* @param {string|symbol} type Type of event to listen for, or `'*'` for all events
6364
* @param {Function} handler Function to call in response to given event
65+
* @returns {Function}
6466
* @memberOf mitt
6567
*/
6668
on<Key extends keyof Events>(type: Key, handler: GenericEventHandler) {
@@ -70,6 +72,9 @@ export default function mitt<Events extends Record<EventType, unknown>>(
7072
} else {
7173
all!.set(type, [handler] as EventHandlerList<Events[keyof Events]>);
7274
}
75+
return () => {
76+
mittInstance.off(type, handler);
77+
};
7378
},
7479

7580
/**
@@ -120,4 +125,6 @@ export default function mitt<Events extends Record<EventType, unknown>>(
120125
}
121126
}
122127
};
128+
129+
return mittInstance;
123130
}

0 commit comments

Comments
 (0)