Module Eio_js_backend

Eio backend for JavaScript environments.

You will normally not use this module directly. Instead, use Eio_brr.start (package eio_brr) or Js_of_ocaml_eio.Eio_js.start (package js_of_ocaml-eio) and then use the API in the Eio module.

val start : (unit -> unit) -> unit

start fn executes function fn in an environment where Eio effects can be performed. The function is executed asynchronously: start fn returns when all fibers are suspended or wait for their turn (see set_wakeup), without waiting for them to complete.

If fn () raises an exception, the exception is passed to a handler (see set_uncaught_exception_handler) that by default outputs the exception to stderr and exits the program. Eio_brr and Js_of_ocaml_eio replace this default by a handler that only exits the program when the JavaScript environment provides a way to do so (quit, or process.exit in Node.js); in a browser, the exception is output and the other fibers keep running.

You should not expect effect handlers set-up outside of start fn to be available during the execution of function fn.

val await : setup:(resolve:('a -> unit) -> reject:(exn -> unit) -> 'handle) -> cancel:('handle -> unit) -> 'a

await setup cancel suspends the current fiber and calls setup resolve reject. The fiber is resumed normally with some value v when resolve v is called. It is resumed with exception exn when reject exn is called. resolve and reject are meant to be called from JavaScript callbacks registered by setup: the fiber is then resumed right away, in the context of the callback.

If the fiber is cancelled, function cancel is called. When the cancellation comes from a fiber of the same group (see set_wakeup), cancel is called right away, and an exception it raises goes to the canceller. Otherwise, cancel is called in the fiber's context once the fiber is resumed, and an exception it raises is raised in the fiber in place of the cancellation exception.

Only the first call to resolve or reject has an effect, and only if the fiber has not been cancelled; later calls are ignored. They can be called by setup itself: the fiber is then resumed once setup returns. If setup raises an exception, the fiber is resumed with this exception, unless resolve or reject was already called (the exception is then ignored) or the fiber was cancelled meanwhile (the fiber then gets the cancellation exception and cancel is not called, since there is no handle). cancel is called at most once, and never once the fiber has been resumed by resolve or reject: any cleanup needed after completion must be done by the JavaScript callbacks.

val set_wakeup : ((unit -> unit) -> unit -> unit) -> unit

set_wakeup make lets fibers be resumed in their own JavaScript execution context. Each call to start makes a group of fibers (the fibers it forks belong to it). A fiber resumed from a JavaScript callback it registered (see await) runs right away. A fiber woken otherwise (e.g. through an Eio.Promise, from a fiber of another group or from plain JavaScript code) is not run right away: when its group last became idle, in its own context, the scheduler called make resume, and it now calls the function this returned, which must call resume later, in that context (e.g. from a callback of a JavaScript promise created by make). Exceptions raised by make or by the function it returned are passed to the uncaught exception handler.

This matters where I/O is tied to the context that performs it, such as requests in Cloudflare Workers. Without a hook, such a fiber is run in the waker's turn, after the current group. Groups take turns: while other groups wait to run, a group gives way after each step of its fibers, through its own doorbell if needed.

Js_of_ocaml_eio and Eio_brr set a hook as soon as one of their modules is used.

val set_uncaught_exception_handler : (exn -> Stdlib.Printexc.raw_backtrace -> unit) -> unit

set_uncaught_exception_handler fn registers fn as the handler for uncaught exceptions. Eio_brr and Js_of_ocaml_eio register their own handler (see start) when their modules are initialized, so a handler registered by the program afterwards takes precedence.