sdeleuze
10/24/2019, 12:11 PMEventSource
to a Flow
that emits the data received by the onmessage
callback ?sdeleuze
10/24/2019, 12:14 PMwindow.onload = {
EventSource("/message/stream").onmessage = {
val message = JSON.parse<Message>(it.data as String)
val li = document.createElement("li").apply {
innerHTML = "Message: ${message.content} from user ${message.user}"
}
document.getElementById("messages")!!.appendChild(li)
}
Unit // Ugly workaround for <https://youtrack.jetbrains.com/issue/KT-22635>
}
sdeleuze
10/24/2019, 12:15 PMEventSource("/message/stream").onmessage
to something that provides a Flow<String>
sdeleuze
10/24/2019, 12:16 PMMessage
and do whatever is needed with the DOMsdeleuze
10/24/2019, 12:18 PMfun EventSource.asFlow() = flow {
onmessage = {
emit(JSON.parse<Message>(it.data as String))
Unit
}
}
But it does not work since onmessage
is not suspendingVsevolod Tolstopyatov [JB]
10/24/2019, 12:38 PMchannel.offer
returns false
, use conflated or unlimited channelVsevolod Tolstopyatov [JB]
10/24/2019, 12:39 PMsdeleuze
10/24/2019, 12:39 PMsdeleuze
10/24/2019, 12:40 PMEventSource.asFlow()
out of the box would increase significantly Kotlin added value for frontend developement.Vsevolod Tolstopyatov [JB]
10/24/2019, 1:03 PMsdeleuze
10/24/2019, 1:03 PMVsevolod Tolstopyatov [JB]
10/24/2019, 1:06 PMsdeleuze
10/24/2019, 1:09 PMsdeleuze
10/24/2019, 1:09 PMsdeleuze
10/24/2019, 1:10 PMsdeleuze
10/24/2019, 1:10 PMsdeleuze
10/24/2019, 1:34 PMwindow.onload
I get compile time errors:
suspend fun Window.awaitLoad() = suspendCoroutine<Unit> { cont ->
onload = {
cont.resume(Unit)
}
}
sdeleuze
10/24/2019, 1:35 PMMethods are absent in coroutines classes since 1.3
sdeleuze
10/24/2019, 1:35 PMsdeleuze
10/24/2019, 1:35 PMimport kotlin.coroutines.suspendCoroutine