-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcable.go
141 lines (116 loc) · 3.63 KB
/
cable.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
138
139
140
141
// * cable <https://github.com/jahnestacado/cable>
// * Copyright (c) 2020 Ioannis Tzanellis
// * Licensed under the MIT License (MIT).
// Package cable implements utility functions for scheduling/limiting function calls
package cable
import (
"sync"
"time"
)
type throttleOptions struct {
Immediate bool
}
// Throttle returns a function that no matter how many times it is invoked,
// it will only execute once within the specified interval
func Throttle(fn func(), interval time.Duration) (throttledFunc func()) {
return throttle(fn, interval, throttleOptions{})
}
// ThrottleImmediate behaves as the Throttle function but it will also
// invoke the fn function immediately
func ThrottleImmediate(fn func(), interval time.Duration) (throttledFunc func()) {
return throttle(fn, interval, throttleOptions{Immediate: true})
}
func throttle(fn func(), interval time.Duration, options throttleOptions) (throttledFunc func()) {
invocationChannel := make(chan time.Time)
once := new(sync.Once)
go func() {
lastInvokedAt := time.Now()
timer := time.AfterFunc(interval, func() {})
for invocationOccuredAt := range invocationChannel {
timer.Stop()
once.Do(func() {
if options.Immediate {
fn()
lastInvokedAt = invocationOccuredAt
}
})
delta := invocationOccuredAt.Sub(lastInvokedAt)
if delta > interval || invocationOccuredAt.IsZero() || lastInvokedAt.IsZero() {
lastInvokedAt = invocationOccuredAt
fn()
continue
}
timer = time.AfterFunc(interval, func() {
var zeroTime time.Time
invocationChannel <- zeroTime
})
}
}()
return func() {
invocationChannel <- time.Now()
}
}
type debounceOptions struct {
Immediate bool
}
// Debounce returns a function that no matter how many times it is invoked,
// it will only execute after the specified interval has passed from its last invocation
func Debounce(fn func(), interval time.Duration) (debouncedFunc func()) {
return debounce(fn, interval, debounceOptions{})
}
// DebounceImmediate behaves as the Debounce function but it will also
// invoke the fn function immediately
func DebounceImmediate(fn func(), interval time.Duration) (debouncedFunc func()) {
return debounce(fn, interval, debounceOptions{Immediate: true})
}
func debounce(fn func(), interval time.Duration, options debounceOptions) (debouncedFunc func()) {
once := new(sync.Once)
handleImmediateCall := func() {
if options.Immediate {
fn()
}
}
cancel := func() {}
return func() {
cancel()
once.Do(handleImmediateCall)
stopTimer := time.AfterFunc(interval, fn).Stop
cancel = func() { stopTimer() }
}
}
type executeEveryOptions struct {
Immediate bool
}
// ExecuteEvery executes function fn repeatedly with a fixed time delay(interval) between each call
// until function fn returns false. It returns a cancel function which can be used to cancel as well
// the execution of function
func ExecuteEvery(interval time.Duration, fn func() bool) (cancel func()) {
return executeEvery(interval, fn, executeEveryOptions{})
}
// ExecuteEveryImmediate behaves as the ExecuteEvery function but it will also
// invoke the fn function immediately
func ExecuteEveryImmediate(interval time.Duration, fn func() bool) (cancel func()) {
return executeEvery(interval, fn, executeEveryOptions{Immediate: true})
}
func executeEvery(interval time.Duration, fn func() bool, options executeEveryOptions) (cancel func()) {
once := new(sync.Once)
ticker := time.NewTicker(interval)
cancel = func() {
once.Do(func() {
ticker.Stop()
})
}
go func() {
if options.Immediate {
if !fn() {
ticker.Stop()
}
}
for range ticker.C {
if !fn() {
ticker.Stop()
}
}
}()
return cancel
}