1
1
package debounce
2
2
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).
3
6
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
6
9
}
7
10
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.
9
16
func New (opts ... Option ) * Debouncer {
10
17
inputCh := make (chan func ())
11
18
debouncedCh := Chan (inputCh , opts ... )
12
19
13
20
go func () {
14
21
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 ()
16
24
}
17
25
}()
18
26
@@ -22,12 +30,16 @@ func New(opts ...Option) *Debouncer {
22
30
}
23
31
}
24
32
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.
26
35
func (d * Debouncer ) Do (f func ()) {
27
36
d .inputCh <- f
28
37
}
29
38
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.
31
43
func (d * Debouncer ) Func (f func ()) func () {
32
44
return func () {
33
45
d .inputCh <- f
0 commit comments