Skip to content

Commit

Permalink
chore: Add some helper methods for caching queries
Browse files Browse the repository at this point in the history
  • Loading branch information
0ffz committed Oct 22, 2024
1 parent 4de4973 commit 0852f67
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class PrefabLoader(
) {
private val readFiles = mutableListOf<PrefabPath>()

private val needsInherit = world.cache(NeedsInherit(world))
private val needsInherit = world.cache(::NeedsInherit)

fun addSource(path: PrefabPath) {
readFiles.add(path)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ data class Addon<Configuration, Instance>(
val defaultConfiguration: Geary.() -> Configuration,
val onInstall: Geary.(Configuration) -> Instance,
) {
operator fun invoke(customConfiguration: Geary.() -> Configuration): Addon<Configuration, Instance> {
fun withConfig(customConfiguration: Geary.() -> Configuration): Addon<Configuration, Instance> {
return copy(defaultConfiguration = customConfiguration)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ interface Geary : KoinComponent {
return queryManager.trackQuery(query)
}

fun <T : Query> cache(
create: (Geary) -> T,
): CachedQuery<T> {
return cache(create(this))
}

fun <T : Query> system(
query: T,
): SystemBuilder<T> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,12 @@ class CachedQuery<T : Query> 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 <R> List<CachedQuery.Deferred<R>>.execOnFinish(run: (data: R, entity: GearyEntity) -> Unit) {
Expand Down

0 comments on commit 0852f67

Please sign in to comment.