]> go.fuhry.dev Git - runtime.git/commitdiff
utils/debounce: fix debouncer not always triggering
authorDan Fuhry <dan@fuhry.com>
Thu, 15 May 2025 21:43:41 +0000 (17:43 -0400)
committerDan Fuhry <dan@fuhry.com>
Thu, 15 May 2025 21:43:41 +0000 (17:43 -0400)
utils/debounce/debounce.go

index c620a49198f3786b28c3710b11fdb5cb481687f4..e7366573220f929186400a4a6ac2649e738e8a3a 100644 (file)
@@ -31,7 +31,7 @@ func (d *Debounce) Trigger() {
        if d.ctx != nil {
                d.cancel()
        }
-       d.ctx, d.cancel = context.WithTimeout(context.TODO(), d.timeout)
+       d.ctx, d.cancel = context.WithTimeout(context.TODO(), d.timeout+(100*time.Millisecond))
        d.ticker = *time.NewTicker(d.timeout)
 
        go d.maybeAction()
@@ -39,9 +39,9 @@ func (d *Debounce) Trigger() {
 
 func (d *Debounce) maybeAction() {
        select {
-       case <-d.ctx.Done():
-               return
        case <-d.ticker.C:
                d.action()
+       case <-d.ctx.Done():
+               return
        }
 }