Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for async EventListener DSL PR #35 from pambrose/AsyncEve…
…ntListenerDsl Add support for async EventListener DSL
- Loading branch information
Showing
5 changed files
with
79 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package org.kalasim.misc | ||
|
||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.GlobalScope | ||
import kotlinx.coroutines.channels.Channel | ||
import kotlinx.coroutines.flow.collect | ||
import kotlinx.coroutines.flow.receiveAsFlow | ||
import kotlinx.coroutines.launch | ||
import org.kalasim.Event | ||
import org.kalasim.EventListener | ||
|
||
class AsyncEventListener() : EventListener { | ||
val eventChannel = Channel<Event>() | ||
|
||
inline fun <reified T : Event> start(scope: CoroutineScope = GlobalScope, crossinline block: (event: T) -> Unit) { | ||
scope.launch { | ||
eventChannel.receiveAsFlow() | ||
.collect { event: Event -> | ||
if (event is T) | ||
block.invoke(event) | ||
} | ||
} | ||
} | ||
|
||
override fun consume(event: Event) { | ||
GlobalScope.launch { | ||
eventChannel.trySend(event) | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/test/kotlin/org/kalasim/examples/analysis/LogChannelConsumerDsl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.kalasim.examples.analysis | ||
|
||
import org.kalasim.Component | ||
import org.kalasim.ComponentGenerator | ||
import org.kalasim.InteractionEvent | ||
import org.kalasim.asDist | ||
import org.kalasim.createSimulation | ||
|
||
// create simulation with no default logging | ||
val sim = createSimulation { | ||
ComponentGenerator(iat = 1.asDist()) { Component("Car.${it}") } | ||
|
||
// add custom log consumer | ||
addEventListener<InteractionEvent> { event -> | ||
if (event.curComponent?.name == "ComponentGenerator.1") | ||
println(event) | ||
} | ||
|
||
// run the simulation | ||
run(100) | ||
} |