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

Refactor 'on' and 'off' callback type definitions #16

Merged
merged 1 commit into from Aug 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 15 additions & 2 deletions lib/emitter.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ export declare class Emitter {
/**
* Hooks an event to the client.
*/
on(event: EmitterEvents | string, callback: (args?: any) => void): Emitter;
on<K extends keyof EmitterEventsArgsMap>(event: K, callback: (args: EmitterEventsArgsMap[K]) => void): Emitter;
/**
* Unhooks an event from the client.
*/
off(event: EmitterEvents | string, callback: (args?: any) => void): Emitter;
off<K extends keyof EmitterEventsArgsMap>(event: K, callback: (args: EmitterEventsArgsMap[K]) => void): Emitter;
private _checkEvent;
/**
* Invokes the callback with a specific name.
Expand Down Expand Up @@ -117,6 +117,19 @@ export declare enum EmitterEvents {
presence = "presence",
me = "me"
}
/**
* Represents the argument type of each event callback.
*/
export declare interface EmitterEventsArgsMap {
[EmitterEvents.connect]: Emitter,
[EmitterEvents.disconnect]: Emitter,
[EmitterEvents.message]: EmitterMessage,
[EmitterEvents.offline]: Emitter,
[EmitterEvents.error]: unknown,
[EmitterEvents.keygen]: KeyGenEvent,
[EmitterEvents.presence]: PresenceEvent,
[EmitterEvents.me]: MeEvent
}
/**
* Represents connection options.
*
Expand Down
20 changes: 17 additions & 3 deletions lib/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class Emitter {
this._callbacks = {"connect": [handler]};
this._mqtt = mqtt.connect(brokerUrl, request);

this._mqtt.on(EmitterEvents.connect, () => this._tryInvoke(EmitterEvents.connect, this));
this._mqtt.on("connect", () => this._tryInvoke(EmitterEvents.connect, this));
this._mqtt.on("close", () => this._tryInvoke(EmitterEvents.disconnect, this));
this._mqtt.on("offline", () => this._tryInvoke(EmitterEvents.offline, this));
this._mqtt.on("error", error => this._tryInvoke(EmitterEvents.error, error));
Expand Down Expand Up @@ -226,7 +226,7 @@ export class Emitter {
/**
* Hooks an event to the client.
*/
public on(event: EmitterEvents | string, callback: (args?: any) => void): Emitter {
public on<K extends keyof EmitterEventsArgsMap>(event: K, callback: (args: EmitterEventsArgsMap[K]) => void): Emitter {
this._checkEvent('off', event);
if (!this._callbacks) {
this._throwError("emitter.on: called before connecting");
Expand All @@ -244,7 +244,7 @@ export class Emitter {
/**
* Unhooks an event from the client.
*/
public off(event: EmitterEvents | string, callback: (args?: any) => void): Emitter {
public off<K extends keyof EmitterEventsArgsMap>(event: K, callback: (args: EmitterEventsArgsMap[K]) => void): Emitter {
this._checkEvent('off', event);
if (!this._callbacks) {
this._throwError("emitter.off: called before connecting");
Expand Down Expand Up @@ -400,6 +400,20 @@ export enum EmitterEvents {
me = "me"
}

/**
* Represents the argument type of each event callback.
*/
export interface EmitterEventsArgsMap {
[EmitterEvents.connect]: Emitter,
[EmitterEvents.disconnect]: Emitter,
[EmitterEvents.message]: EmitterMessage,
[EmitterEvents.offline]: Emitter,
[EmitterEvents.error]: unknown,
[EmitterEvents.keygen]: KeyGenEvent,
[EmitterEvents.presence]: PresenceEvent,
[EmitterEvents.me]: MeEvent
}

/**
* Represents connection options.
*
Expand Down