Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,18 @@ export default function mitt<Events extends Record<EventType, unknown>>(
| WildcardHandler<Events>;
all = all || new Map();

return {
const mittInstance = {
/**
* A Map of event names to registered handler functions.
*/
all,

/**
* 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<Key extends keyof Events>(type: Key, handler: GenericEventHandler) {
Expand All @@ -70,6 +72,9 @@ export default function mitt<Events extends Record<EventType, unknown>>(
} else {
all!.set(type, [handler] as EventHandlerList<Events[keyof Events]>);
}
return () => {
mittInstance.off(type, handler);
};
},

/**
Expand Down Expand Up @@ -120,4 +125,6 @@ export default function mitt<Events extends Record<EventType, unknown>>(
}
}
};

return mittInstance;
}