https://kotlinlang.org logo
#coroutines
Title
# coroutines
u

ursus

03/23/2021, 8:57 PM
I'm confused as how to to plug a suspending function into a flow I notice the flowOf builder, but that requires value, so that means its evaluated beforehand I'd expect something like this
Copy code
flowOf {
   someSuspendFunction()
}
am I missing something, or is it left to clients to create such extension comming from rx, I have it mapped in by brain as such
Copy code
Observable.just == flowOf(value)
Observable.fromCallable == flowOf { .. }
e

ephemient

03/23/2021, 8:59 PM
Copy code
flow { emit(someSuspendFunction()) }
u

ursus

03/23/2021, 9:00 PM
yea but thats too much crap 😄 I'd expect it in the builders file, since it has like 20 of these, but the actual 2 you would most often use, one is missing?
so I created the
Copy code
fun <T> flowOf(action: suspend () -> T): Flow<T> {
    return flow { emit(action()) }
}
but imports get messed up if I need something like this
Copy code
foo
.flatMapLatest {
    if (!it) {
        flowOf(Uninitialized)
    } else {
        my.package.name.flowOf { repository.syncDocuments() }
    }
}
obviously thats a naming issue but .. still odd design choice
e

ephemient

03/23/2021, 9:03 PM
you could name it
flowOf
and import both, but seems like a bad idea to me, due to overload resolution
u

ursus

03/23/2021, 9:03 PM
btw what's this one?
Copy code
@FlowPreview
public fun <T> (suspend () -> T).asFlow(): Flow<T> = flow {
    emit(invoke())
}
Copy code
{ repository.syncDocuments() }                          .asFlow()
complains
Suspension functions can be called only within coroutine body
e

ephemient

03/23/2021, 9:05 PM
type inference isn't smart enough, maybe
perhaps one of
Copy code
val lambda: suspend () -> Unit = { repository.syncDocuments() }
lambda.asFlow()
or
repository::syncDocuments.asFlow()
u

ursus

03/23/2021, 9:06 PM
yea function reference works but thats just a coincidence it has no params
u

ursus

03/23/2021, 9:11 PM
yea, well
Copy code
suspend { repository.syncDocuments() }
                            .asFlow()
that suspend literal is news to me
a

Adam Powell

03/23/2021, 11:51 PM
a one emit and close flow is better modeled as a
suspend fun
that returns a value instead
u

ursus

03/23/2021, 11:53 PM
yea I know, its Flow logic, just need essentially this
observable.switchMap { single.toObservable() }
the toObservable extension
a

Adam Powell

03/24/2021, 12:09 AM
isn't that the same as
Copy code
flow.mapLatest { mySuspendFun(it) }
?
u

ursus

03/24/2021, 12:10 AM
not sure, im new, esentially I was looking for
flowOf(suspend { ...})
I never knew of the syntax a builder function
a

Arslan Armanuly

03/24/2021, 11:49 AM
In cases where you need flow with single object can be solved like this
Copy code
foo
.flatMapLatest {
    flow {
        if (!it) {
            emit(Uninitialized)
        } else {
            emitAll(repository.syncDocuments())
        }
    }
}
👍 1
e

Erik

03/24/2021, 8:37 PM
Since you're used to Rx, you might know https://rxmarbles.com/ Likewise, there is https://flowmarbles.com/
3 Views