Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject
import java.util.concurrent.TimeUnit
import kotlin.properties.Delegates
import kotlin.time.Clock
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.onFailure
Expand Down Expand Up @@ -155,7 +156,13 @@ internal constructor(
private fun processEvent(event: Event) {
if (isTerminated()) return

recordLatency(event)
if (event.channel == EventChannel.EXTERNAL) {
val now = Clock.System.now()
val nowNanos = (now.epochSeconds * 1_000_000_000L) + now.nanosecondsOfSecond
val deltaNanos = (nowNanos - event.emittedTime).coerceAtLeast(0L)

eventTimer.update(deltaNanos, TimeUnit.NANOSECONDS)
}

handleEvent(event)?.let { transition -> step(transition) }

Expand Down Expand Up @@ -284,19 +291,15 @@ internal constructor(

private fun stopTimeout(name: String) = timeoutActionManager.stop(name)

private fun recordLatency(event: Event) {
if (event.source == name) return
if (event.target != name) return
if (event.emittedTime == 0L) return

eventTimer.update((System.currentTimeMillis() - event.emittedTime), TimeUnit.MILLISECONDS)
}

override fun toString() = "StateMachine(name='$name')"

inner class StateMachineEventHandler(val eventHandler: EventHandler) {
fun emit(event: Event) =
eventHandler.emit(event.copy(source = name, emittedTime = System.currentTimeMillis()))
fun emit(event: Event) {
val now = Clock.System.now()
val epochNanos = (now.epochSeconds * 1_000_000_000L) + now.nanosecondsOfSecond

eventHandler.emit(event.copy(source = name, emittedTime = epochNanos))
}

fun propagateToParent(event: Event) {
parent?.stateMachineEventHandler?.propagateToParent(event) ?: pushEvent(event)
Expand Down
Loading