diff options
Diffstat (limited to 'events')
-rwxr-xr-x | events/timer.ml | 39 | ||||
-rwxr-xr-x | events/timer.mli | 6 |
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 |