rxjava2-extras icon indicating copy to clipboard operation
rxjava2-extras copied to clipboard

Document Observables/Flowables.cache()

Open thomasnield opened this issue 8 years ago • 2 comments

Following up on #8

thomasnield avatar Jun 28 '17 23:06 thomasnield

I'm a little confused why providing timed arguments doesn't result in automatic resetting at those time intervals. Maybe there is something wrong with my implementation.

import com.github.davidmoten.rx2.Observables;
import com.github.davidmoten.rx2.observable.CloseableObservableWithReset;
import io.reactivex.Observable;
import io.reactivex.schedulers.Schedulers;

import java.time.LocalTime;
import java.util.concurrent.TimeUnit;

public class CachedObservableDemo {

    public static void main(String[] args) {

        // declare source
        Observable<LocalTime> source =
                Observable.fromCallable(LocalTime::now);

        // create CloseableObservableWithReset
        CloseableObservableWithReset<LocalTime> closeableSource =
                Observables.cache(source, 5, TimeUnit.SECONDS, Schedulers.computation());

        //create timed cache
        Observable<LocalTime> timedCache = closeableSource.observable();


        // initial subscription
        timedCache.subscribe(System.out::println);

        // sleep 3 seconds, still prints same value
        sleep(3000);
        timedCache.subscribe(System.out::println);

        //  a new cache is built on next subscription
        sleep(3000);
        timedCache.subscribe(System.out::println);
    }

    public static void sleep(long millis) {
        try {
            Thread.sleep(millis);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

thomasnield avatar Jun 28 '17 23:06 thomasnield

Codecov Report

Merging #9 into master will decrease coverage by 0.38%. The diff coverage is n/a.

Impacted file tree graph

@@             Coverage Diff              @@
##             master       #9      +/-   ##
============================================
- Coverage     88.92%   88.54%   -0.39%     
+ Complexity      461      455       -6     
============================================
  Files            63       62       -1     
  Lines          3260     3124     -136     
  Branches        408      391      -17     
============================================
- Hits           2899     2766     -133     
  Misses          235      235              
+ Partials        126      123       -3
Impacted Files Coverage Δ Complexity Δ
.../java/com/github/davidmoten/rx2/StateMachine2.java 86.2% <0%> (-3.45%) 1% <0%> (ø)
...c/main/java/com/github/davidmoten/rx2/Actions.java 90.9% <0%> (-1.95%) 4% <0%> (-1%)
...main/java/com/github/davidmoten/rx2/Functions.java 90% <0%> (-1.67%) 4% <0%> (-1%)
...main/java/com/github/davidmoten/rx2/RetryWhen.java 76.59% <0%> (-1.19%) 9% <0%> (ø)
...in/java/com/github/davidmoten/rx2/Observables.java 46.66% <0%> (-1.16%) 4% <0%> (ø)
...internal/flowable/FlowableFetchPagesByRequest.java 92.85% <0%> (-1.09%) 2% <0%> (ø)
...rx2/internal/flowable/TransformerStateMachine.java 79.31% <0%> (-1.02%) 6% <0%> (ø)
...main/java/com/github/davidmoten/rx2/Flowables.java 53.84% <0%> (-0.88%) 11% <0%> (ø)
...idmoten/rx2/internal/flowable/FlowableReverse.java 91.66% <0%> (-0.65%) 4% <0%> (ø)
...m/github/davidmoten/rx2/flowable/Transformers.java 89.41% <0%> (-0.48%) 32% <0%> (ø)
... and 6 more

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 19b2c28...30aeded. Read the comment docs.

codecov-io avatar Jun 29 '17 02:06 codecov-io