Skip to content

Commit 4ff93d6

Browse files
committed
Improve debouncer documentation
1 parent 9b13deb commit 4ff93d6

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

v2/debouncer.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
11
package debounce
22

3+
// Debouncer wraps a debounced channel of functions,
4+
// allowing callers to submit or wrap functions that will only be executed
5+
// according to the debounce configuration (e.g., delay, limit).
36
type Debouncer struct {
4-
inputCh chan func()
5-
debouncedCh <-chan func()
7+
inputCh chan func() // Channel to receive submitted functions
8+
debouncedCh <-chan func() // Debounced output channel from Chan
69
}
710

8-
// Creates new Debouncer instance that will call provided functions with debounce.
11+
// New creates a new Debouncer instance.
12+
// Submitted functions will be debounced according to the provided options,
13+
// such as WithDelay or WithLimit.
14+
//
15+
// Each debounced function is executed in its own goroutine to avoid blocking the Debouncer.
916
func New(opts ...Option) *Debouncer {
1017
inputCh := make(chan func())
1118
debouncedCh := Chan(inputCh, opts...)
1219

1320
go func() {
1421
for f := range debouncedCh {
15-
go f() // Do not block reading channel for f execution
22+
// Execute function without blocking the debounce processing
23+
go f()
1624
}
1725
}()
1826

@@ -22,12 +30,16 @@ func New(opts ...Option) *Debouncer {
2230
}
2331
}
2432

25-
// Do adds function f to be executed with debounce.
33+
// Do submits a function f to be executed according to the debounce rules.
34+
// Only the most recent function may be executed, depending on delay and limit configuration.
2635
func (d *Debouncer) Do(f func()) {
2736
d.inputCh <- f
2837
}
2938

30-
// Func returns func wrapper of function f, that will execute function f with debounce on call.
39+
// Func returns a debounced wrapper of the given function f.
40+
// Each call to the returned function submits f to the debouncer.
41+
// Depending on debounce configuration, f may not be executed immediately—or at all—
42+
// if subsequent calls override it before the debounce conditions are met.
3143
func (d *Debouncer) Func(f func()) func() {
3244
return func() {
3345
d.inputCh <- f

0 commit comments

Comments
 (0)