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.5.0.tbz
sha256=a991ce25175a5a13240c8bf64354b8f35e0a8d7223b90bef01ba32bc6f929cf4
sha512=ce476986cb2c78990f29b1d7a01bb173a0d30ad0e0a80ba7bde9aa8365952ed01eda4935d92a323b982b0c1c897b20ef01eabe75fe80e4da858f73f6a2d25232

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