-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslow.go
137 lines (126 loc) · 2.6 KB
/
slow.go
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package slow
import (
"time"
)
// Options ...
type Options struct {
Leading bool
MaxWait time.Duration
Trailing bool
}
// Debounce ...
//
// leading := false
//
// maxWait := time.Duration(0)
//
// trailing := true
//
func Debounce(fn func(), wait time.Duration, options *Options) func() {
lastCallTime := time.Time{}
lastInvokeTime := time.Time{}
leading := false
maxWait := time.Duration(0)
trailing := true
maxing := false
var timer *time.Timer
var timerExpired func()
var trailingEdge func(time.Time)
var remainingWait func(time.Time) time.Duration
if options != nil {
leading = options.Leading
maxing = options.MaxWait != 0
if maxing == true {
if wait > options.MaxWait {
maxWait = wait
} else {
maxWait = options.MaxWait
}
}
trailing = options.Trailing
}
invokeFunc := func(t time.Time) {
lastInvokeTime = t
go fn()
}
leadingEdge := func(t time.Time) {
lastInvokeTime = t
timer = time.AfterFunc(remainingWait(t), timerExpired)
if leading {
invokeFunc(t)
}
}
remainingWait = func(t time.Time) time.Duration {
timeSinceLastCall := t.Sub(lastCallTime)
timeSinceLastInvoke := t.Sub(lastInvokeTime)
timeWaiting := wait - timeSinceLastCall
if maxing {
if timeWaiting < maxWait-timeSinceLastInvoke {
return timeWaiting
}
return maxWait - timeSinceLastInvoke
}
return timeWaiting
}
shouldInvoke := func(t time.Time) bool {
timeSinceLastCall := t.Sub(lastCallTime)
timeSinceLastInvoke := t.Sub(lastInvokeTime)
return lastCallTime.IsZero() || timeSinceLastCall > wait ||
timeSinceLastCall < 0 || (maxing && timeSinceLastInvoke >= maxWait)
}
timerExpired = func() {
t := time.Now()
if shouldInvoke(t) {
trailingEdge(t)
return
}
timer = time.AfterFunc(remainingWait(t), timerExpired)
}
trailingEdge = func(t time.Time) {
timer = nil
if trailing {
invokeFunc(t)
return
}
}
debounced := func() {
t := time.Now()
isInvoking := shouldInvoke(t)
lastCallTime = t
if isInvoking {
if timer == nil {
leadingEdge(lastCallTime)
return
}
if maxing {
timer = time.AfterFunc(wait, timerExpired)
invokeFunc(lastCallTime)
return
}
}
if timer == nil {
timer = time.AfterFunc(wait, timerExpired)
}
}
return debounced
}
// Throttle ...
//
// leading := true
//
// maxWait := wait
//
// trailing := true
func Throttle(fn func(), wait time.Duration, options *Options) func() {
leading := true
trailing := true
if options != nil {
leading = options.Leading
trailing = options.Trailing
}
return Debounce(fn, wait, &Options{
Leading: leading,
MaxWait: wait,
Trailing: trailing,
})
}