-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
util.go
57 lines (47 loc) · 1.06 KB
/
util.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
package feeds
import (
"github.com/cutechan/cutechan/go/util"
"time"
)
// TickerInterval sets the interval of ticker flushes
const TickerInterval = time.Millisecond * 100
// A time.Ticker that can be "paused"
type ticker struct {
t *time.Ticker
C <-chan time.Time
}
func (t *ticker) start() {
t.t = time.NewTicker(TickerInterval)
t.C = t.t.C
}
func (t *ticker) pause() {
t.t.Stop()
t.C = nil
}
func (t *ticker) startIfPaused() {
if t.C == nil {
t.start()
}
}
// messageBuffer provides bufferring and concatenation for post update messages
type messageBuffer []byte
// Write writes a message to b
func (b *messageBuffer) write(data []byte) {
if len(*b) == 0 {
*b = append(*b, "33"...)
} else {
*b = append(*b, 0)
}
*b = append(*b, data...)
}
// Flush flushes b into into a []byte and returns it.
// If no messages are stored, the returned buffer is nil.
func (b *messageBuffer) flush() []byte {
if len(*b) == 0 {
return nil
}
// Need to copy, because buffer will be sent to multiple threads
buf := util.CloneBytes(*b)
*b = (*b)[:0]
return buf
}