-
Notifications
You must be signed in to change notification settings - Fork 1
/
observable.go
168 lines (136 loc) · 2.75 KB
/
observable.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package rxgo
type observable struct {
Subscriber
}
func Create(subscriber Subscriber) Observable {
return observable{
subscriber,
}
}
func (c observable) Subscribe(obs Observer) Subscription {
valueCh := make(ValueChan)
errCh := make(ErrChan, 1)
completeCh := make(CompleteChan, 1)
td := c.Subscriber(valueCh, errCh, completeCh)
sub := NewSubscription(td)
go func() {
defer sub.Unsubscribe()
LOOP:
for {
select {
case v := <-valueCh:
if obs != nil {
obs.Next(v)
}
case e := <-errCh:
if obs != nil {
obs.Err(e)
}
break LOOP
case <-completeCh:
if obs != nil {
obs.Complete()
}
break LOOP
}
}
}()
return sub
}
func (c observable) Pipe(ops ...OperatorFunc) Observable {
return Pipe(ops...)(c)
}
type TeardownFunc func()
type Subscriber func(ValueChan, ErrChan, CompleteChan) TeardownFunc
type cancellableObserver struct {
Observer
cancel chan bool
}
func (c cancellableObserver) Cancel() {
c.cancel <- true
}
type Observable interface {
Subscribable
Pipeable
}
type Observables []Observable
func (o Observables) Map(fn func(Observable) Observable) Observables {
mapped := make([]Observable, len(o))
for i, observable := range o {
mapped[i] = fn(observable)
}
return mapped
}
type Subscribable interface {
Subscribe(Observer) Subscription
}
type Observer interface {
NextObserver
ErrObserver
CompletionObserver
}
type ValueChan chan Value
func (v ValueChan) Next(val Value) {
v <- val
}
type ErrChan chan error
func (e ErrChan) Error(err error) {
e <- err
}
type CompleteChan chan bool
func (c CompleteChan) Complete() {
select {
case c <- true:
default:
}
}
type NextObserver interface {
Next(Value)
}
type ErrObserver interface {
Err(error)
}
type CompletionObserver interface {
Complete()
}
type Pipeable interface {
Pipe(...OperatorFunc) Observable
}
type OperatorFunc func(o Observable) Observable
func (o OperatorFunc) Pipe(fns ...OperatorFunc) OperatorFunc {
return Pipe(append([]OperatorFunc{o}, fns...)...)
}
func Pipe(fns ...OperatorFunc) OperatorFunc {
return func(observable Observable) Observable {
for _, fn := range fns {
observable = fn(observable)
}
return observable
}
}
func Range(start, end int) Observable {
return Create(func(v ValueChan, e ErrChan, c CompleteChan) TeardownFunc {
go func() {
for i := start; i <= end; i++ {
v <- i
}
c <- true
}()
return c.Complete
})
}
func Of(values ...Value) Observable {
return Create(func(v ValueChan, e ErrChan, c CompleteChan) TeardownFunc {
go func() {
for _, value := range values {
v.Next(value)
}
c.Complete()
}()
return nil
})
}
type noOpObserver struct{}
func (noOpObserver) Next(Value) {}
func (noOpObserver) Err(error) {}
func (noOpObserver) Complete() {}