blob: a62a420566cd1dd2549d6b87d2b0e9911b8f6a39 (
plain) (
blame)
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 irc
import (
"sync"
"time"
)
type Typing struct {
Target string
Name string
}
type Typings struct {
l sync.Mutex
targets map[Typing]time.Time
timeouts chan Typing
stops chan Typing
}
func NewTypings() *Typings {
ts := &Typings{
targets: map[Typing]time.Time{},
timeouts: make(chan Typing, 16),
stops: make(chan Typing, 16),
}
go func() {
for {
t := <-ts.timeouts
now := time.Now()
ts.l.Lock()
oldT, ok := ts.targets[t]
if ok && 6.0 < now.Sub(oldT).Seconds() {
delete(ts.targets, t)
ts.l.Unlock()
ts.stops <- t
} else {
ts.l.Unlock()
}
}
}()
return ts
}
func (ts *Typings) Stops() <-chan Typing {
return ts.stops
}
func (ts *Typings) Active(target, name string) {
t := Typing{target, name}
ts.l.Lock()
ts.targets[t] = time.Now()
ts.l.Unlock()
go func() {
time.Sleep(6 * time.Second)
ts.timeouts <- t
}()
}
func (ts *Typings) Done(target, name string) {
ts.l.Lock()
delete(ts.targets, Typing{target, name})
ts.l.Unlock()
}
|