js_of_ocaml icon indicating copy to clipboard operation
js_of_ocaml copied to clipboard

[FEATURE REQUEST] WebRTC bindings

Open monstasat opened this issue 6 years ago • 3 comments

Hi!

I've recently done some work on implementing bindings for some browser API not present in js_of_ocaml standard library.

  1. WebRTC API The bindings are not complete and final, but I think they are sufficient for most users. The implementation can be found here.

  2. Promise API During the implementation of WebRTC bindings, I've found that this API heavily uses promises, but there is no support for promises in js_of_ocaml stdlib. Moreover, some existing js_of_ocaml bindings, e.g. for the mediaElement object, do not meet the modern browsers' API, which requires a promise to be returned for the play method. I've found the ocaml-promise-js project developed by @johnelse with Promise API bindings implemented. Unfortunately, it is not in the OPAM repository. I've slightly modified it by adding to_lwt and to_lwt_result functions, as I think that working with Lwt is far more convenient from the OCaml side. Maybe of_lwt and of_lwt_result functions may also be of interest. Here is the code.

  3. ResizeObserver API js_of_ocaml already has bindings for MutationObeserver API, but lacks bindings for ResizeObserver. Here is the code.

Is there any interest in merging this to js_of_ocaml repo? If yes, I can provide a PR. Of course I will accept any advises on how the code can be improved and refactored to meet jsoo conventions.

monstasat avatar Mar 14 '19 08:03 monstasat

There's some existing discussion about promises here: #609. TL;DR: JavaScript Promises are not type-safe and therefore binding directly to the Promise API would be unsound.

kevinji avatar Mar 23 '19 21:03 kevinji

ResizeObserver is now included as part of https://github.com/ocsigen/js_of_ocaml/pull/867

hhugo avatar Dec 12 '19 03:12 hhugo

In order to test stuff related to WebCrypto, I did a very quick wrapper around promises. It doesn't care about failures and it's not practical. But it's short and quick to copy-paste if you find yourself wanting to test something else before diving into promises support in jsoo. The code is below, comes with no guarantee and is remotely usable in practice only if you're doing some kind of FRP.

type 'a promise

let promise_cb =
  let module U = Js.Unsafe in
  let opr = U.js_expr "function(promise, f) { return promise.then(f); }" in
  fun (promise : 'a promise) (cb : 'a -> unit) ->
    ignore @@ U.fun_call opr [|
      U.inject @@ promise;
      U.inject @@ Js.wrap_callback cb;
    |]

adrien-n avatar Dec 20 '19 13:12 adrien-n

I'll document binding availability in https://github.com/ocsigen/js_of_ocaml/issues/1217

hhugo avatar Mar 28 '24 15:03 hhugo