package js_of_ocaml

  1. Overview
  2. Docs
Compiler from OCaml bytecode to JavaScript

Install

dune-project
 Dependency

Authors

Maintainers

Sources

js_of_ocaml-6.4.0.tbz
sha256=7ae22a7e5a6c6aa14a8e2695fa62c66f32cb20db17f1b45dc21e97009342151f
sha512=6cea398d61d4be8e38e912ba7e4bad4f7b81b10d946cdf29026274b5a4b2cc2709fb75e23f7cbe2db335cc65679c262a8aa1b9148effc987d6e79665a7538286

doc/lwt.html

Lwt support

The js_of_ocaml-lwt package provides integration between Lwt and browser APIs. It includes:

  • Lwt_js — basic JavaScript bindings (sleep, yield)
  • Lwt_js_events — DOM event handling with Lwt
  • Lwt_xmlHttpRequest — XMLHttpRequest with Lwt
  • Lwt_jsonp — JSONP requests

Events

The Js_of_ocaml_lwt.Lwt_js_events module provides a concise way to handle DOM events using Lwt threads, as an alternative to Dom_html.addEventListener.

This module defines functions you can call on a DOM element to create an Lwt thread that will return when the event occurs.

Example:

Lwt.ignore_result (Lwt_js_events.click target >>= handler)

The handler receives the JS event as parameter.

Each of these functions has a version (same name with an ending "s") that loops when the handler terminates:

Lwt.ignore_result (Lwt_js_events.clicks target handler)

Cancellation

To remove an event handler, cancel the Lwt thread using Lwt.cancel. You can also use Lwt.pick. For example, the following code waits for a click on either t1 or t2:

Lwt.pick [
  Lwt_js_events.click t1 >>= handler1;
  Lwt_js_events.click t2 >>= handler2
]

Warning: If you use Lwt.pick and your handlers take time, other event listeners will not be cancelled until the handler terminates. It is better to return immediately after launching long-running handlers.

API reference

See Js_of_ocaml_lwt.Lwt_js_events for the full API.

See also