2) coming from rxjava `Observable.just(1).startW...
# coroutines
u
2) coming from rxjava
Observable.just(1).startWith(0)
emits 0,1 flow
flowOf(1).onStart { emit(0) }
emits 1,0 is this by design? a rx fix? pretty foot gunny when mkgrating also, what if the actual flow is replayed? if its "below" the onStart, id assume it emits first which is not good
g
They do different things, so it's expected
onStart is closer to onSubscribe from rxjava
There is no startWith in Flow as I know, but it's easy to write such operator using transform
j
Not sure you'd even need transform. Just emit and then emitAll wrapping an upstream flow
g
Yeah, right,. even easier
u
Okay thats news, .. is there some flow cheat sheet for rx people?
hmm https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/on-start.html
Copy code
flowOf("a", "b", "c")
    .onStart { emit("Begin") }
    .collect { println(it) } // prints Begin, a, b, c
I must be doing something wrong then
Oookay it works as expected, turns out I got punked by .asFlow() 😞
Copy code
repository.foo()
            .asFlow()
            .map<Foo, Signal<Foo>> { Success(it) }
            .catch { emit(Fail(it)) }
            .onStart { emit(Loading) }
            .collect {
                Log.d("Default", "foo=$it")
                setState { copy(fooSignal = it) }
            }

2020-10-24 19:30:06.870 30453-30501/sk.foo.flowplayground D/Default: foo=sk.foo.flowplayground.Loading@f0b3a3c
2020-10-24 19:30:06.872 30453-30501/sk.foo.flowplayground D/Default: foo=Success(value=Foo(foo=1))
2020-10-24 19:30:06.872 30453-30501/sk.foo.flowplayground D/Default: foo=Success(value=Foo(foo=1))

nevermind
so how do I turn a suspend function into a flow 😄
f
I did flow { emit(repository.foo()) } but not sure that's the way to do it
u
yes that