From 0852f6778c736c105f0142ddc18b7c0510141a25 Mon Sep 17 00:00:00 2001 From: Danielle Voznyy Date: Tue, 22 Oct 2024 14:15:43 -0400 Subject: [PATCH] chore: Add some helper methods for caching queries --- .../kotlin/com/mineinabyss/geary/prefabs/PrefabLoader.kt | 2 +- .../kotlin/com/mineinabyss/geary/addons/dsl/GearyAddon.kt | 2 +- .../kotlin/com/mineinabyss/geary/modules/Geary.kt | 6 ++++++ .../com/mineinabyss/geary/systems/query/CachedQuery.kt | 6 ++++++ 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/addons/geary-prefabs/src/commonMain/kotlin/com/mineinabyss/geary/prefabs/PrefabLoader.kt b/addons/geary-prefabs/src/commonMain/kotlin/com/mineinabyss/geary/prefabs/PrefabLoader.kt index 6a80d322..88a1af7b 100644 --- a/addons/geary-prefabs/src/commonMain/kotlin/com/mineinabyss/geary/prefabs/PrefabLoader.kt +++ b/addons/geary-prefabs/src/commonMain/kotlin/com/mineinabyss/geary/prefabs/PrefabLoader.kt @@ -27,7 +27,7 @@ class PrefabLoader( ) { private val readFiles = mutableListOf() - private val needsInherit = world.cache(NeedsInherit(world)) + private val needsInherit = world.cache(::NeedsInherit) fun addSource(path: PrefabPath) { readFiles.add(path) diff --git a/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/addons/dsl/GearyAddon.kt b/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/addons/dsl/GearyAddon.kt index 3400f3fc..dcff0c6b 100644 --- a/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/addons/dsl/GearyAddon.kt +++ b/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/addons/dsl/GearyAddon.kt @@ -22,7 +22,7 @@ data class Addon( val defaultConfiguration: Geary.() -> Configuration, val onInstall: Geary.(Configuration) -> Instance, ) { - operator fun invoke(customConfiguration: Geary.() -> Configuration): Addon { + fun withConfig(customConfiguration: Geary.() -> Configuration): Addon { return copy(defaultConfiguration = customConfiguration) } } diff --git a/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/modules/Geary.kt b/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/modules/Geary.kt index 597dd69d..3b1ca1fe 100644 --- a/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/modules/Geary.kt +++ b/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/modules/Geary.kt @@ -76,6 +76,12 @@ interface Geary : KoinComponent { return queryManager.trackQuery(query) } + fun cache( + create: (Geary) -> T, + ): CachedQuery { + return cache(create(this)) + } + fun system( query: T, ): SystemBuilder { diff --git a/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/systems/query/CachedQuery.kt b/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/systems/query/CachedQuery.kt index 6175673b..399d8317 100644 --- a/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/systems/query/CachedQuery.kt +++ b/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/systems/query/CachedQuery.kt @@ -188,6 +188,12 @@ class CachedQuery internal constructor(val query: T) { forEach { entities.add(Entity(it.unsafeEntity, world)) } return entities } + + fun count(): Int { + var count = 0 + forEach { count++ } + return count + } } inline fun List>.execOnFinish(run: (data: R, entity: GearyEntity) -> Unit) {