Skip to content

Commit 8d11552

Browse files
authored
Merge pull request #358 from Secreto31126/emitters-fix
Emitters fix
2 parents 5a6f8ef + ac3a164 commit 8d11552

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

src/emitters.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type {
99
PostData
1010
} from "./types";
1111
import type { WhatsAppAPI } from "./index";
12+
import type { MaybePromise } from "./utils";
1213

1314
/**
1415
* Callback for "sent" event
@@ -74,7 +75,7 @@ export type OnSentArgs = {
7475
* @template Returns - The return type of the callback, defined by WhatsAppAPI generic parameter
7576
* @param args - The arguments object
7677
*/
77-
export type OnMessage<Returns> = (args: OnMessageArgs) => Returns;
78+
export type OnMessage<Returns> = (args: OnMessageArgs) => MaybePromise<Returns>;
7879

7980
/**
8081
* @public
@@ -131,7 +132,7 @@ export type OnMessageArgs = {
131132
* @template Returns - The return type of the callback, defined by WhatsAppAPI generic parameter
132133
* @param args - The arguments object
133134
*/
134-
export type OnStatus<Returns> = (args: OnStatusArgs) => Returns;
135+
export type OnStatus<Returns> = (args: OnStatusArgs) => MaybePromise<Returns>;
135136

136137
/**
137138
* @public

src/index.ts

+10-6
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,12 @@ export class WhatsAppAPI<EmittersReturnType = void> {
267267
Whatsapp: this
268268
};
269269

270-
this.on?.sent?.(args);
270+
try {
271+
await this.on?.sent?.(args);
272+
} catch (error) {
273+
// Eh... I don't like it nor hate it
274+
console.error(error);
275+
}
271276

272277
return response ?? promise;
273278
}
@@ -799,7 +804,7 @@ export class WhatsAppAPI<EmittersReturnType = void> {
799804
};
800805

801806
try {
802-
return this.on?.message?.(args);
807+
return await this.on?.message?.(args);
803808
} catch (error) {
804809
throw 500;
805810
}
@@ -831,7 +836,7 @@ export class WhatsAppAPI<EmittersReturnType = void> {
831836
};
832837

833838
try {
834-
return this.on?.status?.(args);
839+
return await this.on?.status?.(args);
835840
} catch (error) {
836841
throw 500;
837842
}
@@ -991,10 +996,9 @@ export class WhatsAppAPI<EmittersReturnType = void> {
991996
* Offload a function to the next tick of the event loop
992997
*
993998
* @param f - The function to offload from the main thread
994-
* @param a - The arguments to pass to the function
995999
*/
996-
static offload<A, F extends (...a: A[]) => unknown>(f: F, ...a: A[]) {
1000+
static offload(f: () => unknown) {
9971001
// Thanks @RahulLanjewar93
998-
Promise.resolve().then(() => f(...a));
1002+
Promise.resolve().then(f);
9991003
}
10001004
}

src/utils.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
export type AtLeastOne<T> = [T, ...T[]];
22

3+
export type MaybePromise<T> = T | Promise<T> | PromiseLike<T>;
4+
35
export function isInteger(n: unknown): n is number {
46
return Number.isInteger(n);
57
}

0 commit comments

Comments
 (0)