aboutsummaryrefslogtreecommitdiff
path: root/script.it/worker.ml
blob: 51fe49c2f8162fdc3803fec7f733c0ee0518995b (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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
open Js_of_ocaml

let (let=?) : 'a option -> ('a -> unit) -> unit
  = fun f opt -> Option.iter opt f

let post_message
  : Worker_messages.from_worker -> unit
  = Worker.post_message

let execute (command: [> Worker_messages.to_worker]) =
  match command with

  (* Full rebuild, evaluate the whole path *)
  | `Complete outline ->
    let path = outline.Outline.path in

    let=? path = Path.Fixed.rebuild path in
    let back = Path.Fixed.map
        path
        (fun pt -> Path.Point.copy pt @@ Path.Point.get_coord' pt) in
    let=? back = Path.Fixed.rebuild back in
    post_message (`Complete {outline with path; back})

  (* Remove the point from the main line, and reevaluate the whole path *)
  | `DeletePoint (point, outline) ->
    let=? path = Path.Fixed.remove_point outline.Outline.path point in
    let back = Path.Fixed.map
        path
        (fun pt -> Path.Point.copy pt @@ Path.Point.get_coord' pt) in
    let=? back = Path.Fixed.rebuild back in
    post_message (`Complete {outline with path; back})

  (* Only evaluate the interior *)
  | `Back outline ->
    let back = Path.Fixed.map
        outline.Outline.path
        (fun pt -> Path.Point.copy pt @@ Path.Point.get_coord' pt) in
    let=? back = Path.Fixed.rebuild back in
    post_message (`Complete {outline with back})

  | _ ->
    post_message (`Other (Js.string "Unknown message received"))

let () =
  Worker.set_onmessage execute