linrongbin16L
Denoβ€’17mo ago
linrongbin16

Is it possible to embed `deno_core` with a self-managed event loop?

And the 2nd question is: does the API
deno_core::JsRuntime::run_event_loop
only run 1 tick? or it try to wait everything inside its internal queue to complete?

Because after reading some tutorials and source code of
deno_core
, I found there's already some queues inside
deno_core
, I guess they're for the
queueMicrotask
and built-in
async
/
await
functions.

For example, if we have below sample code:

async function readFile(filename) {
   // Read a file in async mode
}

async function sleepFor(seconds) {
  // Sleep for seconds
}

const content = await readFile("README.md");
console.log(`We have read: ${content}`);
await sleepFor(10);
console.log(`We have sleep for 10s`);


Once the
deno_core::JsRuntime
execute it, I guess the 1st
console.log
will not be printed, instead it's been put in the internal queue and wait for the
async readFile
function done. Correct?

Then we call the
js_runtime.run_event_loop().await
, once it's completed, is the only 1st
console.log
been printed? Or both two
console.log
are been printed? i.e. the
run_event_loop
API runs for only 1 tick, or wait for all the tasks done?

If it only runs for 1 tick, then I can embed it into my command line TUI application. Because the TUI application is using tokio runtime with the crossterm library. I want to use
tokio::select!
on both the crossterm's
EventStream
(it receives terminal keyboard/mouse events, such as key pressed, mouse clicked, etc) and the
deno_core::JsRuntime::run_event_loop
. Only 1 tick will never block the terminal events.

If it waits for all async tasks done, then it means it cannot work along with the tokio runtime managed by my TUI application. Because it will block the terminal.
Was this page helpful?