Source file transferrable_intf.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
open! Core
open Js_of_ocaml

(** Helpers for constructing types that can be sent between web workers via
    {!With_transfer}. *)

module type S = sig
  type t
  type message

  val parse : message -> t
  val serialize : t -> message With_transfer.Packed.t
end

module type From_worker = sig
  include S

  val post_message : t -> unit
end

module type To_worker = sig
  include S

  type 'a response

  val post_message
    :  t
    -> worker:(message, 'a response) Js_of_ocaml.Worker.worker Js_of_ocaml.Js.t
    -> unit
end

module type Transferrable = sig
  module type S = S
  module type From_worker = From_worker
  module type To_worker = To_worker

  (** Convenience wrapper for
      [Binable.to_bigstring m x |> Typed_array.Bigstring.to_arrayBuffer]. *)
  val to_arraybuf : 'a Binable.m -> 'a -> Typed_array.arrayBuffer Js.t

  (** Convenience wrapper for
      [Typed_array.Bigstring.of_arrayBuffer x |> Binable.of_bigstring m]. *)
  val of_arraybuf : 'a Binable.m -> Typed_array.arrayBuffer Js.t -> 'a

  (** Construct a protocol where each message allocates a new bigstring and fills it via
      [to_arraybuf], then transfers the buffer without further copies. The recipient
      copies back to its heap with [of_arraybuf].

      This is generally more performant than structured cloning, but not quite as nice as
      using a type which can be transferred directly without serialization, such as
      {!Immediate_iarray}. *)
  module Of_binable (T : sig
      type t [@@deriving bin_io]
    end) : S with type t := T.t and type message = Typed_array.arrayBuffer Js.t
end