Skip to content

Commit

Permalink
chore(core): cleanup layer logic some more
Browse files Browse the repository at this point in the history
  • Loading branch information
LizAinslie committed Nov 8, 2024
1 parent 941334f commit 9077427
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 84 deletions.
60 changes: 7 additions & 53 deletions core/src/main/kotlin/sh/illumi/kraft/layer/ApplicationLayer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,12 @@ import kotlin.reflect.KClass
* todo: provide an example here
*
* @property coroutineScope The coroutine scope for the layer
* @property depth The depth of the layer in the layer tree
* @property handle An identifying handle for the layer
* @property services The services for the layer
*/
interface ApplicationLayer {
val coroutineScope: CoroutineScope
val depth: Int
val handle: Int // todo: come up with a better system for identifying layers
val isRoot: Boolean get() = depth == ROOT_DEPTH

val services: ServiceContainer
val isRoot: Boolean get() = this is RootLayer
val services: ServiceContainer get() = ServiceContainer(this)

/**
* Get the class for a given [index] in the layer tree
Expand All @@ -41,7 +36,6 @@ interface ApplicationLayer {
*/
fun getClassForIndex(index: Int): KClass<out ApplicationLayer> {
val layers = getLayersToRoot().reversed()
// println(layers.map { it.javaClass.kotlin.simpleName })
return layers[index].javaClass.kotlin
}

Expand All @@ -68,51 +62,11 @@ interface ApplicationLayer {
return layers
}

val log: Logger get() = LoggerFactory.getLogger(this::class.java)

companion object {
/**
* The depth of the root layer
*/
const val ROOT_DEPTH = 0

/**
* The handle for the root layer
*/
const val ROOT_HANDLE = 0

/**
* The maximum depth of a layer
* todo: allow this to be set by the caller so that it can be adjusted based on the application's needs
*/
private const val MAX_DEPTH = 16 // a depth of 16 layers should be more than enough for any practical use case

/**
* The maximum number of handles, and consequently layers that may be created
* todo: allow this to be set by the caller so that it can be adjusted based on the application's needs
*/
private const val MAX_HANDLES: Int = Int.MAX_VALUE

private var nextHandle: Int = ROOT_HANDLE + 1

/**
* Get the next handle for a layer
*
* @return The next handle in the auto-incrementing sequence
*
* @throws KraftException If the number of handles exceeds [MAX_HANDLES]
*/
fun nextHandle(): Int {
if (nextHandle - 1 == MAX_HANDLES) {
throw KraftException("ApplicationLayer handles exhausted")
}

val handle = nextHandle
nextHandle++

return handle
}
fun start() {
services.each { it.onStart() }
}

val log: Logger get() = LoggerFactory.getLogger(this::class.java)
}

/**
Expand All @@ -134,7 +88,7 @@ inline fun <reified TTargetLayer : ApplicationLayer> ApplicationLayer.getLayersT
}

if (currentLayer !is TTargetLayer) {
throw KraftException("${this.javaClass.kotlin.simpleName}[handle=$handle,depth=$depth] has no parent of type ${TTargetLayer::class.simpleName}")
throw KraftException("${this.javaClass.kotlin.simpleName} has no parent of type ${TTargetLayer::class.simpleName}")
}

return layers
Expand Down
25 changes: 1 addition & 24 deletions core/src/main/kotlin/sh/illumi/kraft/layer/RootLayer.kt
Original file line number Diff line number Diff line change
@@ -1,29 +1,6 @@
package sh.illumi.kraft.layer

import kotlinx.coroutines.CoroutineScope
import sh.illumi.kraft.service.ServiceContainer

/**
* The root layer in the application layer stack
*
* @property depth The depth of this layer in the tree. Defaults to [ApplicationLayer.ROOT_DEPTH]
*/
abstract class RootLayer(
override val coroutineScope: CoroutineScope,
) : ApplicationLayer {
override val depth: Int = ApplicationLayer.ROOT_DEPTH

@Suppress("LeakingThis")
override val services = ServiceContainer(this)

init {
@Suppress("LeakingThis")
instance = this
}

abstract suspend fun start()

companion object {
lateinit var instance: RootLayer
}
}
interface RootLayer : ApplicationLayer
3 changes: 3 additions & 0 deletions core/src/main/kotlin/sh/illumi/kraft/service/Service.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ interface Service {
val coroutineScope: CoroutineScope
val rootLayer: RootLayer
val log: Logger get() = LoggerFactory.getLogger(this::class.java)

fun onStart() {}
fun onShutdown() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,8 @@ class ServiceContainer(
}

inline operator fun <reified TService : Service> getValue(thisRef: ApplicationLayer, prop: KProperty<*>): TService = get()

fun each(block: (Service) -> Unit) {
services.values.forEach(block)
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1 @@
package sh.illumi.kraft.x.http.extensions

import io.ktor.server.routing.*
import sh.illumi.kraft.layer.RootLayer
import sh.illumi.kraft.service.ServiceContainer

val RoutingContext.services: ServiceContainer get() = RootLayer.instance.services
package sh.illumi.kraft.x.http.extensions

0 comments on commit 9077427

Please sign in to comment.