craigfe / progress

Progress bar library for OCaml

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Debounce rendering based on elapsed time

Ngoguey42 opened this issue · comments

It is currently possible to use sampling_interval to avoid rendering too often. Having the same concept but based on a minimum elapsed time between renders would be great too.

Nice idea.

A clean implementation of this would probably involve giving users access to the Cond segment state as a combinator in Segment, then Progress_unix can supply the appropriate pre-constructed segment w/ a dependency on mtime. For reference, here is the sampling_interval implementation:

(** [ticker n] is a function [f] that returns [true] on every [n]th call. *)
let ticker interval : unit -> bool = (* ... *)

let periodic interval t =
  match interval with
  | n when n <= 0 -> Format.kasprintf invalid_arg "Non-positive interval: %d" n
  | 1 -> t
  | _ ->
      stateful (fun () ->
          let should_update = ticker interval in   (* TODO: replace [ticker] with a time-based implementation. *)
          Cond { if_ = should_update; then_ = t }) (* TODO: provide a way to use [Cond] externally. *)

This is now provided by Progress 0.2.0 🎉 Thanks!