Skip to content

Provide EventSource.asFlow extension #1633

@qwwdfsad

Description

@qwwdfsad
Member
No description provided.

Activity

qwwdfsad

qwwdfsad commented on Oct 24, 2019

@qwwdfsad
MemberAuthor

Open questions:

  • How to handle backpressure by default? Should we use a conflated channel or an unlimited one?
  • Can multiple flows be constructed from the single EventSource?
sdeleuze

sdeleuze commented on Oct 24, 2019

@sdeleuze
Contributor

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

sdeleuze commented on Oct 24, 2019

@sdeleuze
Contributor

Another example using suspending functions:

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

LouisCAD commented on Oct 24, 2019

@LouisCAD
Contributor

I'd prefer to support cancellation:

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

LouisCAD commented on Oct 24, 2019

@LouisCAD
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @sdeleuze@qwwdfsad@LouisCAD

        Issue actions

          Provide EventSource.asFlow extension · Issue #1633 · Kotlin/kotlinx.coroutines