aboutsummaryrefslogtreecommitdiff
path: root/viz.js/process/process.ml
blob: 8d37788a0148f98498552bd8db16963148dc1b33 (plain)
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
module Js = Js_of_ocaml.Js

class type viz = object
  (* SVG output *)
  method renderSVGElement :
       Js.js_string Js.t
    -> (Js_of_ocaml.Dom_svg.svgElement Js.t, unit) Promise.promise Js.meth

  method renderString :
    Js.js_string Js.t -> (Js.js_string Js.t, unit) Promise.promise Js.meth
end

type t = Js.js_string Js.t * Js.js_string Js.t

let do_action : viz Js.t -> Js.js_string Js.t -> (unit, unit) Promise.promise =
 fun v text ->
  if text##.length != 0 then
    let gv = Formatter.convert text in
    let promise =
      try v##renderString gv with
      | _ -> Promise.reject ()
    in
    Promise.then_map
      ~on_fulfilled:(fun svg -> Js_of_ocaml.Worker.post_message ((gv, svg) : t))
      ~on_rejected:(fun _ -> ())
      promise
  else Promise.reject ()