-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
ObservableDefer.java
59 lines (47 loc) · 1.78 KB
/
ObservableDefer.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
56
57
58
59
package rx.observables.creating;
import org.junit.Test;
import rx.Observable;
/**
* @author Pablo Perez
*/
/**
* Normally when you create an observable with just or create, The observable is created with the value that passed at that point,
* and then once that constantClass observer subscribe, the value it´s just passed through the pipeline.
* Sometimes that´s not the desirable, since maybe we dont want to create the observable at that point, only when an observer subscribe to it.
* Defer it will wait to create the observable with the value when we subscribe our observer.
* Basically create this Observable that wrap the observable that we want to create only when we subscribe to the observable.
*/
public class ObservableDefer {
private String value = "none";
public void setValue(String value) {
this.value = value;
}
public Observable<String> getValue() {
return Observable.just(value);
}
private Observable<String> getDeferValue() {
return Observable.defer(() -> Observable.just(value));
}
/**
* In this example we see how the values are set into the observable once we create instead when we subscribe.
* Shall print
* none
*/
@Test
public void testNotDeferObservable() {
Observable<String> observable = getValue();
setValue("deferred");
observable.subscribe(System.out::println);
}
/**
* In this example we see how the values are set into the observable once we subscribe instead when we create the observable.
* Shall print
* deferred
*/
@Test
public void testDeferObservable() {
Observable<String> observable = getDeferValue();
setValue("deferred");
observable.subscribe(System.out::println);
}
}