Repository navigation
Expand file tree
/
Copy pathexample_test.go
More file actions
85 lines (68 loc) · 1.7 KB
/
Copy pathexample_test.go
File metadata and controls
85 lines (68 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package debounce_test
import (
"fmt"
"time"
"github.com/floatdrop/debounce/v3"
)
func Example() {
d := debounce.New(
debounce.WithTrailing(100*time.Millisecond),
debounce.WithTimeLimit(time.Second),
)
defer d.Stop()
for _, query := range []string{"g", "go", "gol", "gola", "golan", "golang"} {
d.Do(func() { fmt.Println("search:", query) })
}
time.Sleep(300 * time.Millisecond)
// Output: search: golang
}
func ExampleNewFunc() {
saves := 0
save, stop := debounce.NewFunc(func() {
saves++
fmt.Println("saved", saves)
}, debounce.WithTrailing(100*time.Millisecond))
defer stop()
save()
save()
save()
time.Sleep(300 * time.Millisecond)
// Output: saved 1
}
func ExampleWithLeading() {
d := debounce.New(
debounce.WithLeading(true),
debounce.WithTrailing(100*time.Millisecond),
)
d.Do(func() { fmt.Println("first") }) // runs immediately
d.Do(func() { fmt.Println("second") })
d.Do(func() { fmt.Println("third") }) // runs 100ms later
time.Sleep(300 * time.Millisecond)
// Output:
// first
// third
}
func ExampleWithDebounceLimit() {
d := debounce.New(
debounce.WithTrailing(100*time.Millisecond),
debounce.WithDebounceLimit(2),
)
d.Do(func() { fmt.Println("one") })
d.Do(func() { fmt.Println("two") }) // the 2nd call runs immediately
d.Do(func() { fmt.Println("three") }) // runs 100ms later
time.Sleep(300 * time.Millisecond)
// Output:
// two
// three
}
func ExampleDebouncer_DoAsync() {
done := make(chan struct{})
d := debounce.New(debounce.WithLeading(true))
// With WithLeading, Do would run this in the calling goroutine.
d.DoAsync(func() {
fmt.Println("hello from another goroutine")
close(done)
})
<-done
// Output: hello from another goroutine
}