File tree 3 files changed +15
-8
lines changed
3 files changed +15
-8
lines changed Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ import type {
9
9
PostData
10
10
} from "./types" ;
11
11
import type { WhatsAppAPI } from "./index" ;
12
+ import type { MaybePromise } from "./utils" ;
12
13
13
14
/**
14
15
* Callback for "sent" event
@@ -74,7 +75,7 @@ export type OnSentArgs = {
74
75
* @template Returns - The return type of the callback, defined by WhatsAppAPI generic parameter
75
76
* @param args - The arguments object
76
77
*/
77
- export type OnMessage < Returns > = ( args : OnMessageArgs ) => Returns ;
78
+ export type OnMessage < Returns > = ( args : OnMessageArgs ) => MaybePromise < Returns > ;
78
79
79
80
/**
80
81
* @public
@@ -131,7 +132,7 @@ export type OnMessageArgs = {
131
132
* @template Returns - The return type of the callback, defined by WhatsAppAPI generic parameter
132
133
* @param args - The arguments object
133
134
*/
134
- export type OnStatus < Returns > = ( args : OnStatusArgs ) => Returns ;
135
+ export type OnStatus < Returns > = ( args : OnStatusArgs ) => MaybePromise < Returns > ;
135
136
136
137
/**
137
138
* @public
Original file line number Diff line number Diff line change @@ -267,7 +267,12 @@ export class WhatsAppAPI<EmittersReturnType = void> {
267
267
Whatsapp : this
268
268
} ;
269
269
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
+ }
271
276
272
277
return response ?? promise ;
273
278
}
@@ -799,7 +804,7 @@ export class WhatsAppAPI<EmittersReturnType = void> {
799
804
} ;
800
805
801
806
try {
802
- return this . on ?. message ?.( args ) ;
807
+ return await this . on ?. message ?.( args ) ;
803
808
} catch ( error ) {
804
809
throw 500 ;
805
810
}
@@ -831,7 +836,7 @@ export class WhatsAppAPI<EmittersReturnType = void> {
831
836
} ;
832
837
833
838
try {
834
- return this . on ?. status ?.( args ) ;
839
+ return await this . on ?. status ?.( args ) ;
835
840
} catch ( error ) {
836
841
throw 500 ;
837
842
}
@@ -991,10 +996,9 @@ export class WhatsAppAPI<EmittersReturnType = void> {
991
996
* Offload a function to the next tick of the event loop
992
997
*
993
998
* @param f - The function to offload from the main thread
994
- * @param a - The arguments to pass to the function
995
999
*/
996
- static offload < A , F extends ( ... a : A [ ] ) => unknown > ( f : F , ... a : A [ ] ) {
1000
+ static offload ( f : ( ) => unknown ) {
997
1001
// Thanks @RahulLanjewar 93
998
- Promise . resolve ( ) . then ( ( ) => f ( ... a ) ) ;
1002
+ Promise . resolve ( ) . then ( f ) ;
999
1003
}
1000
1004
}
Original file line number Diff line number Diff line change 1
1
export type AtLeastOne < T > = [ T , ...T [ ] ] ;
2
2
3
+ export type MaybePromise < T > = T | Promise < T > | PromiseLike < T > ;
4
+
3
5
export function isInteger ( n : unknown ) : n is number {
4
6
return Number . isInteger ( n ) ;
5
7
}
You can’t perform that action at this time.
0 commit comments