forked from politrons/reactive
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ObservableCache.java
43 lines (34 loc) · 1.57 KB
/
ObservableCache.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
package rx.observables.utils;
import org.junit.Test;
import rx.Observable;
/**
* @author Pablo Perez
*/
/**
* The feature cache it will cache the last emitted items from the last observer and it will return to the next observer that subscribe to the observable.
*/
public class ObservableCache {
/**
* Here we can prove how the first time the items are delayed 100 ms per item emitted but second time because it´s cached we dont have any delay since
* the item emitted are cached
*/
@Test
public void cacheObservable() {
Integer[] numbers = {0, 1, 2, 3, 4, 5};
Observable<Integer> observable = Observable.from(numbers)
.doOnNext(number -> {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
})
.cache();
long time = System.currentTimeMillis();
observable.subscribe(System.out::println);
System.out.println("First time took:" + (System.currentTimeMillis() - time));
time = System.currentTimeMillis();
observable.subscribe(System.out::println);
System.out.println("Second time took:" + (System.currentTimeMillis() - time));
}
}