s2.spring.automate.executor.S2AutomateExecutorSpring.kt Maven / Gradle / Ivy
The newest version!
package s2.spring.automate.executor
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.onEach
import s2.automate.core.S2AutomateExecutorImpl
import s2.automate.core.appevent.publisher.AppEventPublisher
import s2.dsl.automate.Evt
import s2.dsl.automate.S2Command
import s2.dsl.automate.S2InitCommand
import s2.dsl.automate.S2State
import s2.dsl.automate.model.WithS2Id
import s2.dsl.automate.model.WithS2State
open class S2AutomateExecutorSpring : S2AutomateStoringExecutor where
STATE : S2State,
ENTITY : WithS2State,
ENTITY : WithS2Id {
protected lateinit var automateExecutor: S2AutomateExecutorImpl
private lateinit var publisher: AppEventPublisher
fun withContext(automateExecutor: S2AutomateExecutorImpl, publisher: AppEventPublisher) {
this.automateExecutor = automateExecutor
this.publisher = publisher
}
override suspend fun createWithEvent(
command: S2InitCommand,
buildEvent: suspend ENTITY.() -> EVENT_OUT,
buildEntity: suspend () -> ENTITY,
): EVENT_OUT {
val (_, event) = automateExecutor.create(command) {
val entity = buildEntity()
val event = buildEvent(entity)
entity to event
}
publisher.publish(event)
return event
}
override suspend fun createWithEvent(
command: S2InitCommand,
build: suspend () -> Pair,
): EVENT_OUT {
val (_, domainEvent) = automateExecutor.create(command, build)
publisher.publish(domainEvent)
return domainEvent
}
override suspend fun doTransition(
command: S2Command,
exec: suspend ENTITY.() -> Pair,
): EVENT_OUT {
val (_, event) = automateExecutor.doTransition(command, exec)
publisher.publish(event)
return event
}
override suspend fun doTransition(
id: ID,
command: S2Command,
exec: suspend ENTITY.() -> Pair,
): EVENT_OUT {
return doTransition(command, exec)
}
override suspend fun createWithEventFlow(
commands: Flow,
build: suspend (cmd: COMMAND) -> Pair
): Flow {
return automateExecutor.createInit(commands, build).onEach { event ->
publisher.publish(event)
}
}
override suspend fun , EVENT_OUT : Evt> doTransitionFlow(
command: Flow,
exec: suspend (COMMAND, ENTITY) -> Pair
): Flow {
return automateExecutor.doTransitionFlow(command, exec).onEach {
publisher.publish(it)
}
}
}