-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbuffer.go
66 lines (53 loc) · 957 Bytes
/
buffer.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
package ephemerald
import "github.com/boz/ephemerald/ui"
type poolItemBuffer interface {
get() <-chan poolItem
put(c poolItem)
stop()
}
type pibuffer struct {
outch chan poolItem
inch chan poolItem
buf []poolItem
uie ui.PoolEmitter
}
func newPoolItemBuffer(uie ui.PoolEmitter) poolItemBuffer {
b := &pibuffer{
outch: make(chan poolItem),
inch: make(chan poolItem),
uie: uie,
}
go b.run()
return b
}
func (b *pibuffer) get() <-chan poolItem {
return b.outch
}
func (b *pibuffer) put(c poolItem) {
b.inch <- c
}
func (b *pibuffer) stop() {
close(b.inch)
}
func (b *pibuffer) run() {
defer close(b.outch)
for {
b.uie.EmitNumReady(len(b.buf))
var next poolItem
var out chan poolItem
if len(b.buf) > 0 {
next = b.buf[0]
out = b.outch
}
select {
case c, ok := <-b.inch:
if !ok {
b.uie.EmitNumReady(0)
return
}
b.buf = append(b.buf, c)
case out <- next:
b.buf = b.buf[1:]
}
}
}