js_of_ocaml
js_of_ocaml copied to clipboard
[FEATURE REQUEST] WebRTC bindings
Hi!
I've recently done some work on implementing bindings for some browser API not present in js_of_ocaml standard library.
-
WebRTC API The bindings are not complete and final, but I think they are sufficient for most users. The implementation can be found here.
-
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
mediaElementobject, do not meet the modern browsers' API, which requires a promise to be returned for theplaymethod. 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 addingto_lwtandto_lwt_resultfunctions, as I think that working with Lwt is far more convenient from the OCaml side. Maybeof_lwtandof_lwt_resultfunctions may also be of interest. Here is the code. -
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.
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.
ResizeObserver is now included as part of https://github.com/ocsigen/js_of_ocaml/pull/867
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;
|]
I'll document binding availability in https://github.com/ocsigen/js_of_ocaml/issues/1217