8000 change wait to return function · TYPECASTINGSG/rpscript-api-basic@8305547 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8305547

Browse files
committed
change wait to return function
1 parent 54ac4ac commit 8305547

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

src/index.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -149,18 +149,27 @@ export default class RPSBasic {
149149
* @summary wait :: Number → a → a
150150
*
151151
*/
152-
// @rpsAction({verbName:'wait'})
153-
// wait (ctx:RpsContext,opts:{}, period:number, response?:any) : Promise<any>{
154-
// response = response ? response : ctx.$RESULT;
155-
156-
// return new Promise(function (resolve,reject) {
157-
158-
// setTimeout(async function () {
159-
// resolve(response);
160-
// }, period*1000);
161-
162-
// });
163-
// }
152+
@rpsAction({verbName:'wait'})
153+
async wait (ctx:RpsContext,opts:Object, period:number, response?:any) : Promise<any>{
154+
let isFn = opts['function'];
155+
response = response ? response : ctx.$RESULT;
156+
157+
function sleep(res) {
158+
let time = period * 1000;
159+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, time);
160+
161+
return res;
162+
}
163+
164+
if(isFn) return sleep;
165+
else return sleep(response);
166+
167+
// return new Promise(function (resolve,reject) {
168+
// setTimeout(async function () {
169+
// resolve(response);
170+
// }, period*1000);
171+
// });
172+
}
164173

165174
/**
166175
* @function stringify

test/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ m.describe('Basic', () => {
2020
// expect(context.variables['val']).to.be.equals('hello');
2121
expect(context.variables['$val']).to.be.equals('hello');
2222

23+
console.log('hello');
24+
let fn = await basic.wait(context,{function:true},1);
25+
let output = fn('world');
26+
console.log(output);
27+
28+
console.log(await basic.wait(context,{},1,'welcome'));
29+
console.log(await basic.wait(context,{},1,undefined));
30+
console.log(await basic.wait(context,{},1));
31+
2332
}).timeout(0);
2433

2534

0 commit comments

Comments
 (0)
0