-
Notifications
You must be signed in to change notification settings - Fork 1
/
observer.go
133 lines (107 loc) · 1.95 KB
/
observer.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package rxgo
type ObserverFn func(v ValueChan, e ErrChan, c CompleteChan) error
type observer struct {
ValueChan
ErrChan
CompleteChan
}
type OnNext func(v Value)
type OnErr func(err error)
type OnComplete func()
func (o OnComplete) Next(Value) {
}
func (o OnComplete) Err(error) {
}
func (o OnComplete) Complete() {
o()
}
func (o OnNext) Next(v Value) {
o(v)
}
func (o OnNext) Err(err error) {}
func (o OnNext) Complete() {}
func (o OnNext) OnErr(oe OnErr) ObserverFuncs {
return ObserverFuncs{
next: o,
err: oe,
}
}
func (o OnNext) OnComplete(oc OnComplete) ObserverFuncs {
return ObserverFuncs{
next: o,
complete: oc,
}
}
func (o ObserverFuncs) Complete() {
if o.complete != nil {
o.complete()
}
}
func (o ObserverFuncs) OnComplete(oc OnComplete) ObserverFuncs {
o.complete = oc
return o
}
func (o ObserverFuncs) Err(err error) {
if o.err != nil {
o.err(err)
}
}
func (o ObserverFuncs) OnErr(oe OnErr) ObserverFuncs {
o.err = oe
return o
}
func (o ObserverFuncs) Next(v Value) {
if o.next != nil {
o.next(v)
}
}
func (o ObserverFuncs) OnNext(on OnNext) ObserverFuncs {
o.next = on
return o
}
type ObserverFuncs struct {
next OnNext
err OnErr
complete OnComplete
}
type chanObserver struct {
ValueChan
ErrChan
CompleteChan
}
func NewChanObserver() chanObserver {
return chanObserver{
make(ValueChan, 1),
make(ErrChan, 1),
make(CompleteChan, 1),
}
}
func (c chanObserver) Next(v Value) {
c.ValueChan.Next(v)
}
func (c chanObserver) Err(err error) {
c.ErrChan.Error(err)
}
func (c chanObserver) Complete() {
c.CompleteChan.Complete()
}
func (co chanObserver) From() Observable {
return Create(func(v ValueChan, e ErrChan, c CompleteChan) TeardownFunc {
go func() {
LOOP:
for {
select {
case val := <-co.ValueChan:
v <- val
case err := <-co.ErrChan:
e <- err
break LOOP
case <-co.CompleteChan:
c <- true
break LOOP
}
}
}()
return func() {}
})
}