diff --git a/src/index.ts b/src/index.ts index 17672aa..695821e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -51,7 +51,7 @@ export default function mitt>( | WildcardHandler; all = all || new Map(); - return { + const mittInstance = { /** * A Map of event names to registered handler functions. */ @@ -59,8 +59,10 @@ export default function mitt>( /** * Register an event handler for the given type. + * Returns a deregistration function. * @param {string|symbol} type Type of event to listen for, or `'*'` for all events * @param {Function} handler Function to call in response to given event + * @returns {Function} * @memberOf mitt */ on(type: Key, handler: GenericEventHandler) { @@ -70,6 +72,9 @@ export default function mitt>( } else { all!.set(type, [handler] as EventHandlerList); } + return () => { + mittInstance.off(type, handler); + }; }, /** @@ -120,4 +125,6 @@ export default function mitt>( } } }; + + return mittInstance; }