aboutsummaryrefslogtreecommitdiff
path: root/internal/watcher/debounce.go
diff options
context:
space:
mode:
authorSaiyed Shizain <saiyedshizain@gmail.com>2026-07-17 11:10:53 +0530
committerGitHub <noreply@github.com>2026-07-17 11:10:53 +0530
commit6afb80ab754da137939dd523ee419818026f9fb5 (patch)
treef9f94ec69ac87abe8060cefca8708572bc13bdd6 /internal/watcher/debounce.go
parentbcfb199ac8e5399e1a92cfbbc28af3674f80959d (diff)
parent5ae787be3d1d9de3355f8b5662fea857204e693c (diff)
downloadgalleryd-6afb80ab754da137939dd523ee419818026f9fb5.tar.xz
galleryd-6afb80ab754da137939dd523ee419818026f9fb5.zip
Merge pull request #11 from RanXom/feat/live-gallery
Feat/live-gallery: live gallery with automatic filesystem reloads
Diffstat (limited to 'internal/watcher/debounce.go')
-rw-r--r--internal/watcher/debounce.go62
1 files changed, 62 insertions, 0 deletions
diff --git a/internal/watcher/debounce.go b/internal/watcher/debounce.go
new file mode 100644
index 0000000..b2961bd
--- /dev/null
+++ b/internal/watcher/debounce.go
@@ -0,0 +1,62 @@
+package watcher
+
+import (
+ "context"
+ "time"
+)
+
+// notify schedules a debounced notification.
+//
+// Multiple filesystem events collapse into a single callback.
+func (w *Watcher) notify() {
+ if w.config.OnChange == nil {
+ return
+ }
+
+ select {
+ case w.debounce <- struct{}{}:
+ default:
+ }
+}
+
+func (w *Watcher) debounceLoop(ctx context.Context) {
+ if w.config.OnChange == nil {
+ return
+ }
+
+ timer := time.NewTimer(time.Hour)
+
+ if !timer.Stop() {
+ select {
+ case <-timer.C:
+ default:
+ }
+ }
+
+ var pending bool
+
+ for {
+ select {
+ case <-ctx.Done():
+ return
+
+ case <-w.debounce:
+ pending = true
+
+ if !timer.Stop() {
+ select {
+ case <-timer.C:
+ default:
+ }
+ }
+
+ timer.Reset(w.config.Debounce)
+
+ case <-timer.C:
+ if pending {
+ pending = false
+ w.config.OnChange()
+ }
+ }
+ }
+}