aboutsummaryrefslogtreecommitdiff
path: root/events
diff options
context:
space:
mode:
authorSébastien Dailly <sebastien@chimrod.com>2020-12-16 14:39:42 +0100
committerSébastien Dailly <sebastien@chimrod.com>2020-12-16 14:39:42 +0100
commit4f262d6540281487f79870aff589ca92f5d2f6c6 (patch)
tree940e59d943715366d1aa72bb93f248dcd65ab992 /events
Initial commit
Diffstat (limited to 'events')
-rwxr-xr-xevents/dune7
-rwxr-xr-xevents/timer.ml24
-rwxr-xr-xevents/timer.mli7
3 files changed, 38 insertions, 0 deletions
diff --git a/events/dune b/events/dune
new file mode 100755
index 0000000..68e2dd2
--- /dev/null
+++ b/events/dune
@@ -0,0 +1,7 @@
+(library
+ (name events)
+ (libraries
+ brr
+ brr.note
+ )
+)
diff --git a/events/timer.ml b/events/timer.ml
new file mode 100755
index 0000000..def9a81
--- /dev/null
+++ b/events/timer.ml
@@ -0,0 +1,24 @@
+type t = Brr.G.timer_id ref * unit Note.E.send
+
+let create
+ : unit -> (t * unit Note.E.t)
+ = fun () ->
+ let event, send = Note.E.create () in
+ (ref (-1), send), event
+
+let stop
+ : t -> unit
+ = fun (id, _) ->
+ Brr.G.stop_timer !id
+
+let start
+ : t -> float -> unit
+ = fun (id, send) d ->
+
+ Brr.G.stop_timer !id;
+ let timer_id = Brr.G.set_interval
+ ~ms:(int_of_float @@ d *. 1000.)
+ (fun () -> send ()) in
+ ignore @@ Brr.G.set_timeout ~ms:0 send;
+ id:= timer_id;
+
diff --git a/events/timer.mli b/events/timer.mli
new file mode 100755
index 0000000..4bf8a9b
--- /dev/null
+++ b/events/timer.mli
@@ -0,0 +1,7 @@
+type t
+
+val create : unit -> t * unit Note.E.t
+
+val start: t -> float -> unit
+
+val stop: t -> unit