-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskip.go
40 lines (36 loc) · 931 Bytes
/
skip.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
package reactive
// Skip will ignore a specified amount of updates
// and will pass through all following
func Skip(count int) func(Observable, Subjectable) {
return func(subject Observable, newSubject Subjectable) {
_, err := subject.Subscribe(func(args ...interface{}) {
if count == 0 {
newSubject.Next(args...)
} else {
count--
}
})
// This error will never happen. But for gamma rays sake.
if err != nil {
panic(err)
}
}
}
// SkipEvery will skip every {count} update and will pass all others
func SkipEvery(count int) func(Observable, Subjectable) {
return func(subject Observable, newSubject Subjectable) {
var current = 0
_, err := subject.Subscribe(func(args ...interface{}) {
current++
if count != current {
newSubject.Next(args...)
} else {
current = 0
}
})
// This error will never happen. But for gamma rays sake.
if err != nil {
panic(err)
}
}
}