forked from cevian/go-stream
/
util.go
79 lines (63 loc) · 1.35 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package stream
type HardStopChannelCloser struct {
StopNotifier chan bool
}
func (op *HardStopChannelCloser) Stop() error {
//prevents double close
select {
case <-op.StopNotifier:
default:
close(op.StopNotifier)
}
return nil
}
func NewHardStopChannelCloser() *HardStopChannelCloser {
return &HardStopChannelCloser{make(chan bool)}
}
type BaseIn struct {
in chan Object
}
func (o *BaseIn) In() chan Object {
return o.in
}
func (o *BaseIn) GetInDepth() int {
return len(o.in)
}
func (o *BaseIn) SetIn(c chan Object) {
o.in = c
}
func NewBaseIn(slack int) *BaseIn {
return &BaseIn{make(chan Object, slack)}
}
type BaseOut struct {
out chan Object
shouldClose bool
}
func (o *BaseOut) Out() chan Object {
return o.out
}
func (o *BaseOut) SetOut(c chan Object) {
o.out = c
//the only real reason to set out is to have a shared output, in which case shouldn't close
o.shouldClose = false
}
func (o *BaseOut) SetCloseOnExit(flag bool) {
o.shouldClose = flag
}
func (o *BaseOut) CloseOutput() {
if o.shouldClose {
close(o.out)
}
}
func NewBaseOut(slack int) *BaseOut {
return &BaseOut{make(chan Object, slack), true}
}
type BaseInOutOp struct {
*HardStopChannelCloser
*BaseIn
*BaseOut
}
func NewBaseInOutOp(slack int) *BaseInOutOp {
obj := &BaseInOutOp{NewHardStopChannelCloser(), NewBaseIn(slack), NewBaseOut(slack)}
return obj
}