-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStartWithOperator.java
55 lines (48 loc) · 1.45 KB
/
StartWithOperator.java
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
package rxjava.combining;
import common.CommonIntegerObserver;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
/**
* Author: andy.xwt
* Date: 2019-02-08 14:45
* Description:startWith操作符会在被观察者发送的事件流开始的位置添加事件。
*/
public class StartWithOperator {
/**
* 输出0,1,2,3,4,5
*/
static void test() {
Observable.range(1, 5)
.startWith(0)
.subscribe(new CommonIntegerObserver());
}
/**
* 当有多个startWith时按照顺序排列,最后的在最前
* 输出-1,0,1,2,3,4,5
*/
static void test2() {
Observable.range(1, 5)
.startWith(0)
.startWith(-1)
.subscribe(new CommonIntegerObserver());
}
/**
* 到抛出异常时,会终止输出。
*/
static void test3() {
Observable.create(new ObservableOnSubscribe<Integer>() {
@Override
public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
emitter.onError(new Throwable("error"));
emitter.onNext(1);
emitter.onNext(2);
}
}).startWith(0).startWith(-1).subscribe(new CommonIntegerObserver());
}
public static void main(String[] args) {
// test();
// test2();
test3();
}
}