package fuseau
A simple IO and concurrency library for OCaml 5
Install
Dune Dependency
Authors
Maintainers
Sources
fuseau-0.1.tbz
sha256=8a9339d239aa371d0c4aceb23d7601a1b7da8f42d84542cee30669cc95addb6a
sha512=fa656c7311371344f0c6ebf08c666afc33296558ccc678ed87baf2f9ba54035cd4c5caca4257212416296fcdbdfc1687c46cc2ebea3548c792ea72602b85b832
doc/src/fuseau.unix/timer.ml.html
Source file timer.ml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
open Common_ type instant_s = float type duration_s = float type kind = | Once | Every of duration_s type task = { mutable deadline: instant_s; mutable active: bool; f: cancel_handle -> unit; as_cancel_handle: cancel_handle; kind: kind; } module Task_heap = Heap.Make (struct type t = task let[@inline] leq t1 t2 = t1.deadline <= t2.deadline end) type t = { mutable tasks: Task_heap.t } (** accepted time diff for actions. *) let epsilon_s = 0.000_001 type tick_res = | Wait of float | Run of (cancel_handle -> unit) * cancel_handle | Empty let[@inline] has_tasks self = not (Task_heap.is_empty self.tasks) let[@inline] num_tasks self : int = Task_heap.size self.tasks let[@inline] pop_task_ self : unit = let tasks, _t = Task_heap.take_exn self.tasks in self.tasks <- tasks let run_after self delay f : cancel_handle = let now = Time.monotonic_time_s () in let deadline = now +. delay in let rec task = { deadline; f; kind = Once; active = true; as_cancel_handle = { cancel = (fun () -> task.active <- false) }; } in self.tasks <- Task_heap.insert task self.tasks; task.as_cancel_handle let run_every self delay f : cancel_handle = let now = Time.monotonic_time_s () in let deadline = now +. delay in let rec task = { deadline; f; kind = Every delay; active = true; as_cancel_handle = { cancel = (fun () -> task.active <- false) }; } in self.tasks <- Task_heap.insert task self.tasks; task.as_cancel_handle let rec next (self : t) : tick_res = match Task_heap.find_min self.tasks with | None -> Empty | Some task when not task.active -> pop_task_ self; next self | Some task -> let now = Time.monotonic_time_s () in let remaining_time_s = task.deadline -. now in if remaining_time_s <= epsilon_s then ( pop_task_ self; (match task.kind with | Once -> () | Every dur -> (* schedule the next iteration *) task.deadline <- now +. dur; self.tasks <- Task_heap.insert task self.tasks); Run (task.f, task.as_cancel_handle) ) else Wait remaining_time_s let create () = { tasks = Task_heap.empty }
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>