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
17 changes: 9 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,14 @@ export default function mitt<Events extends Record<EventType, unknown>>(
* @memberOf mitt
*/
on<Key extends keyof Events>(type: Key, handler: GenericEventHandler) {
const handlers: Array<GenericEventHandler> | undefined = all!.get(type);
if (handlers) {
handlers.push(handler);
}
else {
all!.set(type, [handler] as EventHandlerList<Events[keyof Events]>);
let handlers: Array<GenericEventHandler> | undefined = all!.get(type);
if (!handlers) {
all!.set(type, handlers = []);
}
handlers.push(handler);
return () => {
handlers!.splice(handlers!.indexOf(handler) >>> 0, 1);
};
},

/**
Expand All @@ -75,13 +76,13 @@ export default function mitt<Events extends Record<EventType, unknown>>(
* @memberOf mitt
*/
off<Key extends keyof Events>(type: Key, handler?: GenericEventHandler) {
const handlers: Array<GenericEventHandler> | undefined = all!.get(type);
let handlers: Array<GenericEventHandler> | undefined = all!.get(type);
Copy link
Owner Author

@developit developit Jun 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: this is a non-functional change - it just slightly improves compression gains by making the structure of off() match that of on().

if (handlers) {
if (handler) {
handlers.splice(handlers.indexOf(handler) >>> 0, 1);
}
else {
all!.set(type, []);
all!.set(type, handlers = []);
}
}
},
Expand Down