slidev icon indicating copy to clipboard operation
slidev copied to clipboard

support for asynchronicity in monaco-run

Open QuentinRoy opened this issue 10 months ago • 3 comments

Is your feature request related to a problem? Please describe.

When using async code in monaco run, later lines are not added. For example the following snippets only show 1 and 3 because the output is built synchronously.

console.log("1");
setTimeout(() => {
  console.log("2");
}, 2000);
console.log("3");

Describe the solution you'd like I'd like the code output to be updated when the new log shows up.

Describe alternatives you've considered Currently, I have successfully patched the ts-runner to return an element that mimics what <CodeRunner /> (from internals) outputs, but imperatively.

I am willing to make my patch a PR if the maintainers are interested. However I believe the best way to handle this would be to supports async generators as CodeRunnerOutputs. This way it would provide the functionality to all runners, and they won't have to manually do the little work (highlighting, etc) I am currently doing (since I am bypassing and replicating the usual rendering). I don't think it would be too hard to do, and am willing to give it a crack if the maintainers are interested (though expect delays).

QuentinRoy avatar Apr 07 '24 20:04 QuentinRoy

The code is runned in a async context currently. You can use the await keyword on top level. However, setTimeout isn't working because the program finished before the callback, then the results aren't shown.

It is possible to override setTimeout in the running context, so that the program will only be regarded finished after all timeouts got triggered.

kermanx avatar Apr 08 '24 01:04 kermanx

It is probably a better approach then, because my solution currently has issues when exporting. But would it mean the program would only show the result once all setTimeout are done? I was currently thinking about overriding setTimeout in print mode and so have a fully different behavior in this case. But it starts to feel rather hacky.

QuentinRoy avatar Apr 08 '24 08:04 QuentinRoy

But would it mean the program would only show the result once all setTimeout are done?

Yes. And we can improve this in the future.

kermanx avatar Apr 08 '24 08:04 kermanx