-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinvoke.ts
More file actions
33 lines (28 loc) · 938 Bytes
/
invoke.ts
File metadata and controls
33 lines (28 loc) · 938 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import type { GModJSSafeTypes } from ".";
export type GmodCallback = (_: (result: GModJSSafeTypes) => void) => void;
declare global {
const lua: {
call: (name: string, ...args: [...GModJSSafeTypes[], (_: "__ok__" | "__err__", r: GModJSSafeTypes) => void]) => void;
};
}
export async function invoke<T extends GModJSSafeTypes>(funcName: string, ...args: GModJSSafeTypes[]): Promise<T> {
return new Promise((resolve, reject) => {
const timer = setTimeout(() => reject(new Error(`calling '${funcName}': timed out`)), 3 * 1000);
try {
lua.call(funcName, ...args, (status, result) => {
clearTimeout(timer);
switch (status) {
case "__ok__":
resolve(result as T);
break;
case "__err__":
reject(new Error(String(result)));
break;
}
});
} catch (error) {
clearTimeout(timer);
reject(error);
}
});
}