Skip to content

Instantly share code, notes, and snippets.

@anitaa1990
Created August 19, 2018 08:11
Show Gist options
  • Save anitaa1990/f4fcece4b0159e387be722e14836b903 to your computer and use it in GitHub Desktop.
Save anitaa1990/f4fcece4b0159e387be722e14836b903 to your computer and use it in GitHub Desktop.
Observable
/* This is the outer observable. Items emitted here will be used to control the inner observable.
* Whenever it emits an item, the inner observable will stop its emission
* and a new one will be created.
*/
.switchOnNext(Observable.interval(600, TimeUnit.MILLISECONDS)
.map((aLong) -> {
/* This is the inner observable. It will emit items every 180ms.
* When the outer observable emits a new item (which is supposed to happen after 600ms)
* this one will be discarded and a new one will be taken in place.
* Since outer observable will emit items each 600ms, inner observable will have a chance to emit 3 items and
* then be discarded. */
return Observable.interval(180, TimeUnit.MILLISECONDS);
}))
.subscribe(item -> System.out.println("onNext: " + item));
Thread.sleep(5000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment