-
Notifications
You must be signed in to change notification settings - Fork 1
/
take.go
47 lines (44 loc) · 1.19 KB
/
take.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
package rxgo
func Take(num int) OperatorFunc {
return func(o Observable) Observable {
return Create(func(v ValueChan, e ErrChan, c CompleteChan) TeardownFunc {
count := 0
sub := o.Subscribe(OnNext(func(val Value) {
if count < num {
v <- val
}
if count == num-1 {
c.Complete()
}
count++
}).OnErr(e.Error).OnComplete(c.Complete))
return sub.Unsubscribe
})
}
}
func TakeUntil(observable Observable) OperatorFunc {
return func(o Observable) Observable {
return Create(func(v ValueChan, e ErrChan, c CompleteChan) TeardownFunc {
sub := o.Subscribe(OnNext(v.Next).OnErr(e.Error).OnComplete(c.Complete))
observable.Subscribe(OnNext(func(Value) { c.Complete() }).OnErr(e.Error).OnComplete(c.Complete))
return sub.Unsubscribe
})
}
}
func TakeWhile(fn func(v Value) bool, inclusive bool) OperatorFunc {
return func(o Observable) Observable {
return Create(func(v ValueChan, e ErrChan, c CompleteChan) TeardownFunc {
sub := o.Subscribe(OnNext(func(val Value) {
if !fn(val) {
if inclusive {
v.Next(val)
}
c.Complete()
} else {
v.Next(val)
}
}).OnErr(e.Error).OnComplete(c.Complete))
return sub.Unsubscribe
})
}
}