From fa9a2079b259560980a18e074a3a3341ecdab9b6 Mon Sep 17 00:00:00 2001 From: Matthias <18034092+matthias-ccri@users.noreply.github.com> Date: Tue, 31 Mar 2026 10:25:27 -0400 Subject: [PATCH] `on` now returns a deregistration function --- src/index.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) 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; }