-
Notifications
You must be signed in to change notification settings - Fork 0
/
multirepeater.go
64 lines (56 loc) · 1.63 KB
/
multirepeater.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
package repeater
import (
"sync"
"time"
)
// Holds any amount of repeaters. Uses StartRepeater. Use this to manage several repeaters by ids. Use NewMultiRepeater to instance MultiRepeater.
type MultiRepeater[ID comparable] struct {
repeaters map[ID]func()
mux sync.Mutex
}
// Instances MultiRepeater. Pass ID type you'd like to use to manage repeaters.
func NewMultiRepeater[ID comparable]() *MultiRepeater[ID] {
return &MultiRepeater[ID]{
repeaters: make(map[ID]func()),
mux: sync.Mutex{},
}
}
// Works almost same as original StartRepeater, but delegates stop function managment to MultiRepeater and requires id for repeater.
// Returns `true` on success, and `false` if passed id is occupied.
func (mr *MultiRepeater[ID]) StartRepeater(id ID, frequency time.Duration, fnToCall func()) bool {
mr.mux.Lock()
if mr.repeaters[id] != nil {
mr.mux.Unlock()
return false
}
mr.repeaters[id] = StartRepeater(frequency, fnToCall)
mr.mux.Unlock()
return true
}
// Stops repeater by ID. Return `true` on success, and `false` if passed id is not found.
func (mr *MultiRepeater[ID]) StopRepeater(id ID) bool {
mr.mux.Lock()
if mr.repeaters[id] == nil {
mr.mux.Unlock()
return false
}
mr.repeaters[id]()
delete(mr.repeaters, id)
mr.mux.Unlock()
return true
}
// Stops all repeaters managed by this MultiRepeater. Waits until all repeaters are stopped.
func (mr *MultiRepeater[ID]) StopAllRepeaters() {
wg := &sync.WaitGroup{}
mr.mux.Lock()
wg.Add(len(mr.repeaters))
for _, stop := range mr.repeaters {
go func(stop func()) {
stop()
wg.Done()
}(stop)
}
mr.repeaters = make(map[ID]func())
mr.mux.Unlock()
wg.Wait()
}