aboutsummaryrefslogtreecommitdiff
path: root/events
diff options
context:
space:
mode:
authorSébastien Dailly <sebastien@chimrod.com>2020-12-17 13:56:00 +0100
committerSébastien Dailly <sebastien@chimrod.com>2020-12-17 13:56:00 +0100
commit20d10a93e5becb41d1145f9d35136782365b0ba4 (patch)
treecb4e78c05ec538a3f47ba37231b705b713219a11 /events
parent4f262d6540281487f79870aff589ca92f5d2f6c6 (diff)
Refactor
Diffstat (limited to 'events')
-rwxr-xr-xevents/timer.ml39
-rwxr-xr-xevents/timer.mli6
2 files changed, 33 insertions, 12 deletions
diff --git a/events/timer.ml b/events/timer.ml
index def9a81..0a75e12 100755
--- a/events/timer.ml
+++ b/events/timer.ml
@@ -1,24 +1,41 @@
-type t = Brr.G.timer_id ref * unit Note.E.send
+open Brr_note_kit
+
+type t =
+ { mutable id : Brr.G.timer_id
+ ; send : float Note.E.send
+ ; mutable counter : Time.counter
+ }
let create
- : unit -> (t * unit Note.E.t)
+ : unit -> (t * Brr_note_kit.Time.span Note.E.t)
= fun () ->
- let event, send = Note.E.create () in
- (ref (-1), send), event
+ let event, send = Note.E.create ()
+ and counter = (Time.counter ()) in
+ {id = (-1); send; counter}, event
let stop
: t -> unit
- = fun (id, _) ->
- Brr.G.stop_timer !id
+ = fun {id; _} ->
+ Brr.G.stop_timer id
let start
: t -> float -> unit
- = fun (id, send) d ->
+ = fun t d ->
+ let {id; send; _} = t in
+ t.counter <- Time.counter ();
+
- Brr.G.stop_timer !id;
+ 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;
+ (fun () ->
+
+ let span = Time.counter_value t.counter in
+ t.counter <- Time.counter ();
+ send span) in
+ ignore @@ Brr.G.set_timeout ~ms:0 (fun () -> send 0.);
+ t.id <- timer_id
+
+let delay : t -> float
+ = fun t -> Time.counter_value t.counter
diff --git a/events/timer.mli b/events/timer.mli
index 4bf8a9b..0509ad0 100755
--- a/events/timer.mli
+++ b/events/timer.mli
@@ -1,7 +1,11 @@
+open Brr_note_kit
+
type t
-val create : unit -> t * unit Note.E.t
+val create : unit -> t * Time.span Note.E.t
val start: t -> float -> unit
val stop: t -> unit
+
+val delay : t -> float