forked from bep/debounce
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
64 lines (49 loc) · 1.08 KB
/
example_test.go
File metadata and controls
64 lines (49 loc) · 1.08 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
package debounce_test
import (
"fmt"
"time"
"github.com/floatdrop/debounce"
)
func ExampleNew() {
// Create a debounced function with 500ms delay
debounced := debounce.New(500 * time.Millisecond)
// This will only execute once, after 500ms
debounced(func() {
fmt.Println("Hello, World!")
})
debounced(func() {
fmt.Println("This will be executed instead")
})
// Wait for execution
time.Sleep(1 * time.Second)
// Output: This will be executed instead
}
func ExampleNewFunc() {
a := 0
// Create a debounced function with 500ms delay
debouncedFunc := debounce.NewFunc(func() {
a += 1
fmt.Println(a)
}, 500*time.Millisecond)
// This will only execute once, after 500ms
debouncedFunc()
debouncedFunc()
// Wait for execution
time.Sleep(1 * time.Second)
// Output: 1
}
func ExampleWithMaxCalls() {
debounced := debounce.New(500*time.Millisecond, debounce.WithMaxCalls(2))
debounced(func() {
fmt.Println("One")
})
debounced(func() {
fmt.Println("Two")
})
debounced(func() {
fmt.Println("Three")
})
time.Sleep(1 * time.Second)
// Output: Two
// Three
}