From 253fa19bb5678798c35deedba632d60558828187 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ulysse=20G=C3=A9rard?= Date: Sat, 7 Dec 2024 13:49:40 +0100 Subject: [PATCH] Throttle sync reports --- lib/db/sync.ml | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/db/sync.ml b/lib/db/sync.ml index 583e9e3..6500b3b 100644 --- a/lib/db/sync.ml +++ b/lib/db/sync.ml @@ -518,10 +518,29 @@ let sync_v2 ~report ~(source : Source.connexion) idb = let+ () = sync_all ~threads:5 in Console.log [ "Sync finished. Added "; !count_tracks; " tracks" ] +let throttle () = + (* We use [last_update] to have regular debounced updates and the + [timeout] to ensure that the last event is always taken into + account even it it happens during the debouncing interval. *) + let last_update = ref 0. in + let timer = ref (-1) in + fun ~delay_ms f -> + let now = Performance.now_ms G.performance in + if !timer >= 0 then G.stop_timer !timer; + timer := G.set_timeout ~ms:delay_ms (fun () -> f ()); + if now -. !last_update >. float_of_int delay_ms then ( + last_update := now; + f ()) + let check_and_sync ?(report = fun _ -> ()) ~source idb = let open Fut.Result_syntax in let initial = initial_report in let () = (* Send a first report *) report initial in - let report' sync_progress = report { status = Syncing; sync_progress } in + let sync_report_throttler = throttle () ~delay_ms:250 in + let report' = + fun sync_progress -> + sync_report_throttler (fun () -> report { status = Syncing; sync_progress }) + in let+ () = sync_v2 ~report:report' ~source idb in - report { status = In_sync; sync_progress = None } + sync_report_throttler (fun () -> + report { status = In_sync; sync_progress = None })