Kotlin / kotlinx.coroutines

Library support for Kotlin coroutines
Apache License 2.0
13.09k stars 1.85k forks source link

Provide EventSource.asFlow extension #1633

Open qwwdfsad opened 5 years ago

qwwdfsad commented 5 years ago

Open questions:

sdeleuze commented 5 years ago

I will think about those questions.

Since my use case of EventSource is pretty anectdotical, I would suggest to turn this issue into a more global one like "Provide suspending function and Flow based extensions on top of Web APIs".

The more I use Kotlin/JS, the more I think you could do for frontend what you did on Android with carefully crafted Kotlin extensions on top of Web APIs.

sdeleuze commented 5 years ago

Another example using suspending functions:

suspend fun Window.awaitLoad() = suspendCoroutine<Unit> { cont ->
    onload = {
        cont.resume(Unit)
    }
}
LouisCAD commented 5 years ago

I'd prefer to support cancellation:

suspend fun Window.awaitLoad() {
    try {
        suspendCancellableCoroutine<Unit> { cont ->
            onload = {
                onload = null
                cont.resume(Unit)
            }
        }
    } finally {
        onload = null
    }
}
LouisCAD commented 5 years ago

Even if in the case of waiting for window loading, cancellation is unlikely at this point, it might help some coroutines being cancelled earlier if the case applies.