Skip to content

Instantly share code, notes, and snippets.

@thomasnield
Created April 30, 2018 17:36
Show Gist options
  • Select an option

  • Save thomasnield/6205bb2ed1d2eb297343e009e0aa56e0 to your computer and use it in GitHub Desktop.

Select an option

Save thomasnield/6205bb2ed1d2eb297343e009e0aa56e0 to your computer and use it in GitHub Desktop.
fun <T,R> Observable<T>.switchReplaySingle(cachedSingle: (T) -> Single<R>) =
map { cachedSingle(it).toObservable().replay(1).autoConnect().firstOrError() }
.replay(1)
.autoConnect()
.take(1)
.flatMapSingle { it }
.firstOrError()
fun <T,R> Flowable<T>.switchReplaySingle(cachedSingle: (T) -> Single<R>) =
map { cachedSingle(it).toObservable().replay(1).autoConnect().firstOrError() }
.replay(1)
.autoConnect()
.take(1)
.flatMapSingle { it }
.firstOrError()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment