aantron / repromise_lwt

13 stars 2 forks source link

Remove ~until argument of Repromise_lwt.run #2

Closed aantron closed 6 years ago

aantron commented 6 years ago

Right now, we have

https://github.com/aantron/repromise_lwt/blob/c7f4244799c393ee28b7539fcc4b7e0a9038f490/src/repromise_lwt.rei#L3

Ideally, we would make run behave like Node. It would be run: unit => unit, and it would keep calling the main loop until there are both (1) no callbacks and (2) no pending I/O. We can tell when there are no callbacks by querying Repromise. However, there is currently no easy way to query Lwt for whether there is no pending I/O.

So, I think the next-best thing is to create a global Repromise.t(unit), and have run: unit => unit keep calling the main loop until this global promise is resolved. So, programs would look like:

let p = ...; /* Set up some promises */

/* One of the callbacks that is set up eventually calls Repromise_lwt.exit: */
...
|> Repromise.wait(() => Repromise_lwt.exit(0));

/* This call blocks the program until Repromise_lwt.exit is actually called. */
Repromise_lwt.run();

cc @kennetpostigo I think this API is less confusing than ~until.

kennetpostigo commented 6 years ago

This looks pretty interesting! what if we call run at the beginning and have it return an exit function that will terminate the loop?

aantron commented 6 years ago

The job of run is to block the process from exiting until async code is all done, so if we call it at the beginning, nothing else after run will ever run.

kennetpostigo commented 6 years ago

@aantron gotcha, then your change makes a ton of sense. I can test whenever you want :)

aantron commented 6 years ago

@kennetpostigo I committed it. It's now

Repromise_lwt.run: unit => unit;
Repromise_lwt.stop: unit => unit;