Skip to content

Commit a50817c

Browse files
authored
feat: Add tryReveal() function (#53)
1 parent c9cedae commit a50817c

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

reveal-core/src/main/kotlin/com/svenjacobs/reveal/RevealState.kt

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,35 @@ public class RevealState internal constructor(
6262
/**
6363
* Reveals revealable with given [key]
6464
*
65+
* Might throw [IllegalArgumentException] if the revealable item is not known to Reveal. This
66+
* might happen if for example the item is in a lazy container and is currently not part of the
67+
* visible area. It is the duty of the developer to ensure that a revealable item is currently
68+
* visible (known to Reveal) before calling this function. Additionally [containsRevealable] or
69+
* [revealableKeys] can be used to ensure this.
70+
*
71+
* @see tryReveal
6572
* @see containsRevealable
73+
* @see revealableKeys
6674
* @throws IllegalArgumentException if revealable with given key was not found
6775
*/
6876
public suspend fun reveal(key: Key) {
69-
require(revealables.containsKey(key)) { "Revealable with key \"$key\" not found" }
77+
require(containsRevealable(key)) { "Revealable with key \"$key\" not found" }
78+
internalReveal(key)
79+
}
80+
81+
/**
82+
* Like [reveal] but doesn't throw exception if revealable was not found.
83+
* Instead returns `false`.
84+
*
85+
* @see reveal
86+
*/
87+
public suspend fun tryReveal(key: Key): Boolean {
88+
if (!containsRevealable(key)) return false
89+
internalReveal(key)
90+
return true
91+
}
92+
93+
private suspend fun internalReveal(key: Key) {
7094
mutex.withLock {
7195
previousRevealable = currentRevealable
7296
currentRevealable = revealables[key]

0 commit comments

Comments
 (0)