If dynamic `import()` `BLOCK` JS thread?
I am trying to modify js_load_file function, with adding some long-time I/O operations inside it, such as requesting module file from remote server , or just sleep(10) for test.
After add sleep(10) into js_load_file, the thread will be blocked by js_dynamic_import_job.
It would be better to make js_load_file a non-blocking function, or inside another thread.
If you use a blocking function it will surely block.
Maybe you could instead run the blocking code to fetch the module asynchronously and then pass it on to load file.
If you use a blocking function it will surely block.
Maybe you could instead run the blocking code to fetch the module asynchronously and then pass it on to load file.
But JS_SetModuleLoaderFunc only can receive a synchronously module_loader. The type of module_loader is:
typedef JSModuleDef *JSModuleLoaderFunc(JSContext *ctx,
const char *module_name, void *opaque);
(I am new to C language, I think that a asynchronously function should have a callback argument, is that right?)
Link this:
typedef void JSModuleLoaderFunc(JSContext *ctx,
const char *module_name, void *opaque, JSModuleLoaderCB cb);
@saghul
BTW, I tried import() in txiki.js, and it also blocked:
setInterval(() => {
console.log(~~(Date.now() / 1000) % 100)
}, 1000)
// a simple http server that won't finish response
// this will block timer tasks
import('http://127.0.0.1:8989/').then(res => console.log(res.test))
Good catch, I should add a timeout for http imports!