jvmMain.assembly.RaptorLifecycleComponent.kt Maven / Gradle / Ivy
package io.fluidsonic.raptor.lifecycle
import io.fluidsonic.raptor.*
import io.fluidsonic.raptor.di.*
// TODO Add a dependency system for executing actions in the right order instead of using priorities.
// TODO Replace star/stop actions with full observers. Properly call stop/failure callbacks if startup/stop fails.
public class RaptorLifecycleComponent internal constructor() : RaptorComponent.Base(RaptorLifecyclePlugin) {
private val startActions: MutableList> = mutableListOf()
private val stopActions: MutableList> = mutableListOf()
@RaptorDsl
public fun onStart(
priority: Int = 0,
action: suspend RaptorLifecycleStartScope.() -> Unit,
) {
startActions += LifecycleAction(block = action, priority = priority)
}
@RaptorDsl
public fun onStop(
priority: Int = 0,
action: suspend RaptorLifecycleStopScope.() -> Unit,
) {
stopActions += LifecycleAction(block = action, priority = priority)
}
internal fun service(
name: String,
factory: RaptorDI.() -> Service,
): RaptorServiceComponent =
componentRegistry.register(Keys.serviceComponent, RaptorServiceComponent(factory = factory, name = name))
internal fun serviceRegistrations(): Collection> =
componentRegistry.many(Keys.serviceComponent).map { it.registration() }
override fun RaptorComponentConfigurationEndScope.onConfigurationEnded() {
propertyRegistry.register(Keys.lifecycleProperty, DefaultLifecycle(
context = lazyContext,
startActions = startActions.toList(),
stopActions = stopActions.toList()
))
}
override fun toString(): String = "lifecycle"
}
@RaptorDsl
public fun RaptorAssemblyQuery.onStart(
priority: Int = 0,
action: suspend RaptorLifecycleStartScope.() -> Unit,
) {
this {
onStart(priority = priority, action = action)
}
}
@RaptorDsl
public fun RaptorAssemblyQuery.onStop(
priority: Int = 0,
action: suspend RaptorLifecycleStopScope.() -> Unit,
) {
this {
onStop(priority = priority, action = action)
}
}