@makano/rew 1.3.9 → 1.4.1
Sign up to get free protection for your applications and to get access to all the features.
- package/lib/rew/functions/future.js +3 -0
- package/lib/rew/functions/wait.js +14 -4
- package/package.json +1 -1
- package/runtime.d.ts +21 -9
@@ -1,4 +1,5 @@
|
|
1
1
|
const emitter = require('./emitter');
|
2
|
+
const { wait } = require('./wait');
|
2
3
|
|
3
4
|
function future(callback, timeout = 0, defData = null) {
|
4
5
|
const listener = emitter();
|
@@ -19,6 +20,8 @@ function future(callback, timeout = 0, defData = null) {
|
|
19
20
|
resolve: (data) => listener.emit('resolve', data),
|
20
21
|
reject: (data) => listener.emit('reject', data),
|
21
22
|
wait: async () => await promise,
|
23
|
+
sync: (fn) => wait(async () => fn ? (await fn(promise)) : await promise),
|
24
|
+
_isfuture: true
|
22
25
|
};
|
23
26
|
};
|
24
27
|
|
@@ -3,11 +3,21 @@ const deasync = require("deasync");
|
|
3
3
|
|
4
4
|
module.exports.wait = (...args) => {
|
5
5
|
const fn = args.shift();
|
6
|
-
if(typeof fn !== "function") throw new TypeError("The first argument must be a function to use wait.");
|
6
|
+
if(typeof fn !== "function" && typeof fn?.then != "function" && !fn?._isfuture) throw new TypeError("The first argument must be a function, future or a Promise to use wait.");
|
7
7
|
const df = deasync(async (cb) => {
|
8
|
-
fn(...args)
|
9
|
-
|
10
|
-
|
8
|
+
let result = typeof fn == "function" ? fn(...args) : fn;
|
9
|
+
|
10
|
+
if(result._isfuture){
|
11
|
+
result = result.wait();
|
12
|
+
}
|
13
|
+
|
14
|
+
if(typeof result.then == "function"){
|
15
|
+
result
|
16
|
+
.then(d => cb(null, d))
|
17
|
+
.catch(d => cb(d));
|
18
|
+
} else {
|
19
|
+
cb(null, result);
|
20
|
+
}
|
11
21
|
});
|
12
22
|
return df();
|
13
23
|
}
|
package/package.json
CHANGED
package/runtime.d.ts
CHANGED
@@ -958,13 +958,7 @@ declare namespace Rew {
|
|
958
958
|
): ReturnType<typeof future>;
|
959
959
|
|
960
960
|
declare function print(...args: any[]): void;
|
961
|
-
declare
|
962
|
-
// @ts-ignore
|
963
|
-
const stdout: WriteStream;
|
964
|
-
// @ts-ignore
|
965
|
-
const stdin: ReadStream;
|
966
|
-
}
|
967
|
-
|
961
|
+
declare function printf(...args: any[]): void;
|
968
962
|
declare function input(prompt: string): string;
|
969
963
|
|
970
964
|
declare const basename: (path: string) => string;
|
@@ -1009,6 +1003,18 @@ declare namespace Rew {
|
|
1009
1003
|
group(...group: any[]): { g: T, with: (props: any) => { g: T, [key: string]: any }, [key: string]: any }
|
1010
1004
|
}
|
1011
1005
|
|
1006
|
+
// @ts-ignore
|
1007
|
+
interface stdout extends WriteStream {
|
1008
|
+
put: typeof print;
|
1009
|
+
write: (str: string) => void;
|
1010
|
+
strace: (...str: string[]) => void;
|
1011
|
+
}
|
1012
|
+
|
1013
|
+
// @ts-ignore
|
1014
|
+
interface stdin extends ReadStream {
|
1015
|
+
read: typeof input
|
1016
|
+
}
|
1017
|
+
|
1012
1018
|
declare const std: {
|
1013
1019
|
curl: typeof curl,
|
1014
1020
|
int: typeof int,
|
@@ -1021,11 +1027,17 @@ declare namespace Rew {
|
|
1021
1027
|
typei: typeof typei,
|
1022
1028
|
|
1023
1029
|
prototype: {
|
1024
|
-
void:
|
1030
|
+
void: void,
|
1025
1031
|
Main: () => [string, () => any],
|
1026
1032
|
define: (name: string, object: any) => any,
|
1027
1033
|
attach: (object: any) => any,
|
1028
|
-
|
1034
|
+
|
1035
|
+
out: stdout;
|
1036
|
+
in: stdin;
|
1037
|
+
|
1038
|
+
ns: () => any;
|
1039
|
+
|
1040
|
+
__: Record<string, any>;
|
1029
1041
|
}
|
1030
1042
|
}
|
1031
1043
|
|