11package example.eventflow
22
3+ import kotlinx.coroutines.Dispatchers
34import kotlinx.coroutines.coroutineScope
45import kotlinx.coroutines.flow.filter
6+ import kotlinx.coroutines.flow.launchIn
57import kotlinx.coroutines.flow.onEach
6- import kotlinx.coroutines.launch
8+ import kotlinx.coroutines.plus
79import web.dom.clickEvent
810import web.dom.document
911import 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