Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add this: void to all methods #201

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
32 changes: 25 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,27 @@ export type EventHandlerMap<Events extends Record<EventType, unknown>> = Map<
export interface Emitter<Events extends Record<EventType, unknown>> {
all: EventHandlerMap<Events>;

on<Key extends keyof Events>(type: Key, handler: Handler<Events[Key]>): void;
on(type: '*', handler: WildcardHandler<Events>): void;
on<Key extends keyof Events>(
this: void,
type: Key,
handler: Handler<Events[Key]>
): void;
on(this: void, type: '*', handler: WildcardHandler<Events>): void;

off<Key extends keyof Events>(
this: void,
type: Key,
handler?: Handler<Events[Key]>
): void;
off(type: '*', handler: WildcardHandler<Events>): void;
off(this: void, type: '*', handler: WildcardHandler<Events>): void;

emit<Key extends keyof Events>(type: Key, event: Events[Key]): void;
emit<Key extends keyof Events>(
this: void,
type: Key,
event: Events[Key]
): void;
emit<Key extends keyof Events>(
this: void,
type: undefined extends Events[Key] ? Key : never
): void;
}
Expand Down Expand Up @@ -63,7 +73,11 @@ export default function mitt<Events extends Record<EventType, unknown>>(
* @param {Function} handler Function to call in response to given event
* @memberOf mitt
*/
on<Key extends keyof Events>(type: Key, handler: GenericEventHandler) {
on<Key extends keyof Events>(
this: void,
type: Key,
handler: GenericEventHandler
) {
const handlers: Array<GenericEventHandler> | undefined = all!.get(type);
if (handlers) {
handlers.push(handler);
Expand All @@ -79,7 +93,11 @@ export default function mitt<Events extends Record<EventType, unknown>>(
* @param {Function} [handler] Handler function to remove
* @memberOf mitt
*/
off<Key extends keyof Events>(type: Key, handler?: GenericEventHandler) {
off<Key extends keyof Events>(
this: void,
type: Key,
handler?: GenericEventHandler
) {
const handlers: Array<GenericEventHandler> | undefined = all!.get(type);
if (handlers) {
if (handler) {
Expand All @@ -100,7 +118,7 @@ export default function mitt<Events extends Record<EventType, unknown>>(
* @param {Any} [evt] Any value (object is recommended and powerful), passed to each handler
* @memberOf mitt
*/
emit<Key extends keyof Events>(type: Key, evt?: Events[Key]) {
emit<Key extends keyof Events>(this: void, type: Key, evt?: Events[Key]) {
let handlers = all!.get(type);
if (handlers) {
(handlers as EventHandlerList<Events[keyof Events]>)
Expand Down