mirror of
https://github.com/usebruno/bruno.git
synced 2024-11-25 09:23:17 +01:00
feat: added bru.sleep shim (#2858)
* fix: added console fn for cli * wip: added bru.sleep shim --------- Co-authored-by: Anoop M D <anoop.md1421@gmail.com>
This commit is contained in:
parent
0c668c16f7
commit
35376305ae
@ -21,6 +21,10 @@ const { shouldUseProxy, PatchedHttpsProxyAgent } = require('../utils/proxy-util'
|
||||
const path = require('path');
|
||||
const protocolRegex = /^([-+\w]{1,25})(:?\/\/|:)/;
|
||||
|
||||
const onConsoleLog = (type, args) => {
|
||||
console[type](...args);
|
||||
};
|
||||
|
||||
const runSingleRequest = async function (
|
||||
filename,
|
||||
bruJson,
|
||||
@ -83,7 +87,7 @@ const runSingleRequest = async function (
|
||||
envVariables,
|
||||
runtimeVariables,
|
||||
collectionPath,
|
||||
null,
|
||||
onConsoleLog,
|
||||
processEnvVars,
|
||||
scriptingConfig
|
||||
);
|
||||
|
@ -9,7 +9,6 @@ const { newQuickJSWASMModule, memoizePromiseFactory } = require('quickjs-emscrip
|
||||
|
||||
// execute `npm run sandbox:bundle-libraries` if the below file doesn't exist
|
||||
const getBundledCode = require('../bundle-browser-rollup');
|
||||
const addSleepShimToContext = require('./shims/sleep');
|
||||
|
||||
let QuickJSSyncContext;
|
||||
const loader = memoizePromiseFactory(() => newQuickJSWASMModule());
|
||||
@ -103,7 +102,6 @@ const executeQuickJsVmAsync = async ({ script: externalScript, context: external
|
||||
req && addBrunoRequestShimToContext(vm, req);
|
||||
res && addBrunoResponseShimToContext(vm, res);
|
||||
consoleFn && addConsoleShimToContext(vm, consoleFn);
|
||||
addSleepShimToContext(vm);
|
||||
addLocalModuleLoaderShimToContext(vm, collectionPath);
|
||||
|
||||
await addLibraryShimsToContext(vm);
|
||||
@ -113,10 +111,10 @@ const executeQuickJsVmAsync = async ({ script: externalScript, context: external
|
||||
const script = `
|
||||
(async () => {
|
||||
const setTimeout = async(fn, timer) => {
|
||||
v = await sleep(timer);
|
||||
v = await bru.sleep(timer);
|
||||
fn.apply();
|
||||
}
|
||||
await sleep(0);
|
||||
await bru.sleep(0);
|
||||
try {
|
||||
${externalScript}
|
||||
}
|
||||
|
@ -63,6 +63,17 @@ const addBruShimToContext = (vm, bru) => {
|
||||
vm.setProp(bruObject, 'getSecretVar', getSecretVar);
|
||||
getSecretVar.dispose();
|
||||
|
||||
const sleep = vm.newFunction('sleep', (timer) => {
|
||||
const t = vm.getString(timer);
|
||||
const promise = vm.newPromise();
|
||||
setTimeout(() => {
|
||||
promise.resolve(vm.newString('slept'));
|
||||
}, t);
|
||||
promise.settled.then(vm.runtime.executePendingJobs);
|
||||
return promise.handle;
|
||||
});
|
||||
sleep.consume((handle) => vm.setProp(bruObject, 'sleep', handle));
|
||||
|
||||
vm.setProp(vm.global, 'bru', bruObject);
|
||||
bruObject.dispose();
|
||||
};
|
||||
|
@ -1,14 +0,0 @@
|
||||
const addSleepShimToContext = (vm) => {
|
||||
const sleepHandle = vm.newFunction('sleep', (timer) => {
|
||||
const t = vm.getString(timer);
|
||||
const promise = vm.newPromise();
|
||||
setTimeout(() => {
|
||||
promise.resolve(vm.newString('slept'));
|
||||
}, t);
|
||||
promise.settled.then(vm.runtime.executePendingJobs);
|
||||
return promise.handle;
|
||||
});
|
||||
sleepHandle.consume((handle) => vm.setProp(vm.global, 'sleep', handle));
|
||||
};
|
||||
|
||||
module.exports = addSleepShimToContext;
|
Loading…
Reference in New Issue
Block a user