Paul Woitaschek
11/23/2020, 7:46 AMnrobi
11/23/2020, 8:07 AMnrobi
11/23/2020, 8:11 AMwasyl
11/23/2020, 8:34 AMwasyl
11/23/2020, 8:34 AMPaul Woitaschek
11/23/2020, 8:42 AMprivate fun <T> Flow<T>.replayOneRefCount(): Flow<T> {
return map { Optional.ofNullable(it) }
.asFlowable()
.replay(1)
.refCount()
.asFlow()
.map { it.orElse(null) }
}
I'm not sure how I can replace this with the SharedFlow as it's error handling is differentwasyl
11/23/2020, 9:11 AMPaul Woitaschek
11/23/2020, 9:11 AMPaul Woitaschek
11/23/2020, 9:11 AMnrobi
11/23/2020, 9:12 AM.catch{ emit(null) } or you could replace null, with your custom error signalwasyl
11/23/2020, 9:13 AMFlow<T> anymore, if you want an information about the error as well. You have to wrap it in something so that you can emit Value(T) or Error(Throwable)wasyl
11/23/2020, 9:14 AMnrobi
11/23/2020, 9:17 AMrefCount functionality could be translated to coroutines. I’m not sure if you can “stop” a hot observable if there are no subscribers 🤔nrobi
11/23/2020, 9:23 AMwasyl
11/23/2020, 9:23 AMshare() operator is emulated by
.shareIn(
scope = scope,
started = SharingStarted.WhileSubscribed(replayExpirationMillis = 0),
replay = 1
)wasyl
11/23/2020, 9:25 AMshare() is publish().refCount() so the replay parameter is 0. For replay(1).refcount(), which is an equivalent of shared BehaviorSubject it would be replay = 1Paul Woitaschek
11/23/2020, 10:11 AMPaul Woitaschek
11/23/2020, 10:11 AMreplayExpirationMillisPaul Woitaschek
11/23/2020, 10:12 AMprivate fun <T> Flow<T>.replayOneRefCount(): Flow<T> {
return map { Optional.ofNullable(it) }
.asFlowable()
.replay(1)
.refCount()
.asFlow()
.map { it.orElse(null) }
}
Is:
private fun <T> Flow<T>.replayOneRefCount(scope: CoroutineScope): Flow<T> {
return map { Result.success(it) }
.catch {
emit(Result.failure(it))
}
.shareIn(scope, SharingStarted.WhileSubscribed(replayExpirationMillis = 0), replay = 1)
.map {
it.getOrThrow()
}
}wasyl
11/23/2020, 10:16 AMmap { it.getOrThrow() } should be somewhere in downstream handling code 🤔 Otherwise by default you’re cancelling the scope for every flow in itPaul Woitaschek
11/23/2020, 10:18 AMPaul Woitaschek
11/23/2020, 10:18 AMwasyl
11/23/2020, 10:56 AM