Use private event loop where necessary
cderv opened this issue · comments
This seems important to not conflict with other later loop while in !async
mode like maybe in wait
function.
private event loop are used in chromote
and also will be pagedown::chrome_print
Here's a summary of how chromote uses a private event loop. I think you should be able to adapt it for crrri.
- On initialization, a private loop is created.
- The WebSocket object is create, using the private loop. https://github.com/rstudio/chromote/blob/f53a70b/R/chromote.R#L35-L44
- Chromote schedules a callback on the main loop which runs the private loop, and then reschedules itself. https://github.com/rstudio/chromote/blob/f53a70b/R/chromote.R#L358-L388
With this setup, whenever you call run_now()
(on the main/global loop), or when the R console is idle, the behavior will be essentially the same as the case where no private loop is used. However, it is also possible to run the private loop by itself, with something like run_now(loop = private_loop)
, and that lets you wait for something to happen without accidentally running a callback that belongs to someone else.
Note that currently when Chromote schedules the private loop to be run from the main loop, it does so every .01 seconds. This is not ideal, and in the future we would like to make it unnecessary by providing an option when the private loop is created.
Chromote also has a function called synchronize
, which will block until a promise resolves. Note that this code is a bit experimental. Also, much of the complexity is present because it tries to gracefully handle interrupts.
https://github.com/rstudio/chromote/blob/f53a70b/R/synchronize.R#L99-L148
Look also at new feature of private loop autorun rstudio/chromote#24