Skip to content

Commit 0272195

Browse files
committed
Examples. Use launchIn in event flow example
1 parent 4e99d31 commit 0272195

File tree

1 file changed

+14
-12
lines changed
  • examples/event-flow/src/webMain/kotlin/example/eventflow

1 file changed

+14
-12
lines changed

examples/event-flow/src/webMain/kotlin/example/eventflow/main.kt

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package example.eventflow
22

3+
import kotlinx.coroutines.Dispatchers
34
import kotlinx.coroutines.coroutineScope
45
import kotlinx.coroutines.flow.filter
6+
import kotlinx.coroutines.flow.launchIn
57
import kotlinx.coroutines.flow.onEach
6-
import kotlinx.coroutines.launch
8+
import kotlinx.coroutines.plus
79
import web.dom.clickEvent
810
import web.dom.document
911
import web.events.invoke
@@ -26,21 +28,21 @@ suspend fun main(): Unit = coroutineScope {
2628
val parent = document.createElement(div)
2729
document.body.append(parent)
2830

31+
parent.clickEvent()
32+
// or `subscribe` as shorthand
33+
.onEach { println("Click is propagated to parent!") }
34+
.launchIn(this + Dispatchers.Unconfined)
35+
2936
val child = document.createElement(button)
3037
child.innerText = "Click me!"
3138
parent.append(child)
3239

33-
launch {
34-
parent.clickEvent()
35-
.collect { println("Click is propagated to parent!") }
36-
}
37-
38-
launch {
39-
child.clickEvent()
40-
.onEach { println("Button is clicked!") }
41-
.filter { propagationToggle.checked }
42-
.collect { it.stopPropagation() }
43-
}
40+
child.clickEvent()
41+
.onEach { println("Button is clicked!") }
42+
.filter { propagationToggle.checked }
43+
// or `subscribe` as shorthand
44+
.onEach { it.stopPropagation() }
45+
.launchIn(this + Dispatchers.Unconfined)
4446

4547
eventFlowTest()
4648
}

0 commit comments

Comments
 (0)