Skip to content

Commit

Permalink
Add ability to reset hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
kylecorry31 committed Mar 15, 2024
1 parent a577701 commit bbb37de
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group = "com.kylecorry"
version = "0.3.0"
version = "0.3.1"

afterEvaluate {
publishing {
Expand Down
19 changes: 19 additions & 0 deletions src/main/kotlin/com/kylecorry/luna/cache/Hooks.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ class Hooks {
effect.runIfChanged(*values, action = action)
}

/**
* Memoize a value
* @param key The key for the memo (should be unique for each memo)
* @param values The values that the memo depends on
* @param value The value function that should be memoized
* @return The memoized value
*/
@Suppress("UNCHECKED_CAST")
fun <T> memo(key: String, vararg values: Any?, value: () -> T): T {
val memo = synchronized(memoLock) {
Expand All @@ -29,4 +36,16 @@ class Hooks {
return memo.getOrPut(*values, value = value)
}

fun resetEffects(except: List<String> = emptyList()) {
synchronized(effectLock) {
effects.keys.removeAll { it !in except }
}
}

fun resetMemos(except: List<String> = emptyList()) {
synchronized(memoLock) {
memos.keys.removeAll { it !in except }
}
}

}
5 changes: 5 additions & 0 deletions src/main/kotlin/com/kylecorry/luna/cache/MemoizedValue.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@ class MemoizedValue<T> {
}
cachedValue!!
}

fun reset(): Unit = synchronized(lock) {
cachedValue = null
cachedHash = null
}
}
4 changes: 4 additions & 0 deletions src/main/kotlin/com/kylecorry/luna/cache/StateEffect.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@ class StateEffect {
}
}

fun reset(): Unit = synchronized(lock) {
cachedHash = null
}

}

0 comments on commit bbb37de

Please sign in to comment.