aboutsummaryrefslogtreecommitdiff
path: root/script.it
diff options
context:
space:
mode:
authorSébastien Dailly <sebastien@chimrod.com>2021-01-09 11:35:40 +0100
committerSébastien Dailly <sebastien@chimrod.com>2021-01-09 11:35:40 +0100
commit329b774e315b41bc0d5b7daf8737222768c8d1f3 (patch)
tree94464c12dfa48d5fdb1508b9e8a115b4596d0e34 /script.it
parentc734c1b30fd1c58a0d42020859be31d89b92bcd0 (diff)
Formalized exchanges between worker and app
Diffstat (limited to 'script.it')
-rwxr-xr-xscript.it/dune2
-rwxr-xr-xscript.it/script.ml21
-rwxr-xr-xscript.it/state.ml14
-rwxr-xr-xscript.it/worker.ml13
-rwxr-xr-xscript.it/worker_messages/dune6
-rwxr-xr-xscript.it/worker_messages/worker_messages.ml11
6 files changed, 35 insertions, 32 deletions
diff --git a/script.it/dune b/script.it/dune
index 84b91f6..c51c43b 100755
--- a/script.it/dune
+++ b/script.it/dune
@@ -7,6 +7,7 @@
elements
blog
layer
+ worker_messages
)
(modes js)
(modules script state selection)
@@ -27,6 +28,7 @@
js_of_ocaml
shapes
path
+ worker_messages
)
(modes js)
(preprocess (pps ppx_hash js_of_ocaml-ppx))
diff --git a/script.it/script.ml b/script.it/script.ml
index ca831ba..3133269 100755
--- a/script.it/script.ml
+++ b/script.it/script.ml
@@ -4,14 +4,6 @@ open Brr
open Brr_note
-module Mouse = Brr_note_kit.Mouse
-
-let get_height el =
- match El.at (Jstr.v "height") el with
- | None -> 0
- | Some att ->
- Option.value ~default:0 (Jstr.to_int att)
-
(** Create the element in the page, and the event handler *)
let canva
: Brr.El.t -> [> State.canva_events] Note.E.t * (float * float) option Note.S.t * Brr_canvas.Canvas.t
@@ -60,14 +52,14 @@ let canva
Brr_note_kit.Mouse.left_up mouse
|> E.map (fun c -> `Out c) in
- let position = Mouse.pos mouse in
+ let position = Brr_note_kit.Mouse.pos mouse in
let pos = S.l2 (fun b pos ->
if b then
Some pos
else
None
- ) (Mouse.left mouse) position in
+ ) (Brr_note_kit.Mouse.left mouse) position in
E.select [click; up], pos, c
@@ -293,15 +285,6 @@ let on_change canva mouse_position timer state =
~w:10.
~h:10.
context;
-
-(*
- Cd2d.stroke_text
- context
- (Jstr.of_float @@ Path.Point.get_stamp point)
- ~x:(x +. 15.)
- ~y;
-*)
-
| _ -> ()
in
diff --git a/script.it/state.ml b/script.it/state.ml
index da97b13..cc199d1 100755
--- a/script.it/state.ml
+++ b/script.it/state.ml
@@ -1,8 +1,6 @@
open StdLabels
open Brr
-let backgroundColor = Blog.Nord.nord0
-
type mode =
| Edit
| Selection of Selection.t
@@ -51,6 +49,10 @@ type state =
; rendering : Layer.Paths.printer
}
+let post
+ : Brr_webworkers.Worker.t -> Worker_messages.to_worker -> unit
+ = Brr_webworkers.Worker.post
+
let insert_or_replace state ((x, y) as p) stamp path =
let width = state.width
and angle = state.angle in
@@ -140,7 +142,7 @@ let delete state worker =
| false -> ()
| true ->
(* Send the job to the worker *)
- Brr_webworkers.Worker.post worker (`DeletePoint (point, p))
+ post worker (`DeletePoint (point, p))
);
{ state with mode = Selection (Path id) }
| _ ->
@@ -266,7 +268,7 @@ let do_action
current
in
- let () = Brr_webworkers.Worker.post worker (`Complete last) in
+ let () = post worker (`Complete last) in
last::state.paths
and current = Path.Path_Builder.empty in
@@ -302,7 +304,7 @@ let do_action
| false -> ()
| true ->
Option.iter
- (fun p -> Brr_webworkers.Worker.post worker (`Complete p))
+ (fun p -> post worker (`Complete p))
(Path.Fixed.replace_point path point')
);
@@ -322,7 +324,7 @@ let do_action
~f:(fun path ->
Layer.Paths.to_svg
- ~color:backgroundColor
+ ~color:Blog.Nord.nord0
(module Path.Fixed)
path
state.rendering
diff --git a/script.it/worker.ml b/script.it/worker.ml
index 4ea9220..00e4595 100755
--- a/script.it/worker.ml
+++ b/script.it/worker.ml
@@ -1,11 +1,10 @@
open Js_of_ocaml
-type message = [
- | `Complete of Path.Fixed.t
- | `DeletePoint of (Path.Point.t * Path.Fixed.t)
-]
+let post_message
+ : Worker_messages.from_worker -> unit
+ = Worker.post_message
-let execute (command: [> message]) =
+let execute (command: [> Worker_messages.to_worker]) =
match command with
| `Complete path ->
begin match Path.Fixed.rebuild path with
@@ -17,8 +16,8 @@ let execute (command: [> message]) =
| Some path -> Worker.post_message (`Complete path)
| None -> ()
end
- | any ->
- Worker.post_message (`Other any)
+ | _ ->
+ post_message (`Other (Js.string "Unknown message received"))
let () =
Worker.set_onmessage execute
diff --git a/script.it/worker_messages/dune b/script.it/worker_messages/dune
new file mode 100755
index 0000000..d1511a6
--- /dev/null
+++ b/script.it/worker_messages/dune
@@ -0,0 +1,6 @@
+(library
+ (name worker_messages)
+ (libraries
+ js_of_ocaml
+ path)
+ )
diff --git a/script.it/worker_messages/worker_messages.ml b/script.it/worker_messages/worker_messages.ml
new file mode 100755
index 0000000..992ec29
--- /dev/null
+++ b/script.it/worker_messages/worker_messages.ml
@@ -0,0 +1,11 @@
+open Js_of_ocaml
+
+type to_worker = [
+ | `Complete of Path.Fixed.t
+ | `DeletePoint of (Path.Point.t * Path.Fixed.t)
+]
+
+type from_worker = [
+ | `Complete of Path.Fixed.t
+ | `Other of Js.js_string Js.t
+]