Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ConcurrentSkipListSet instead of CopyOnWriteArrayList for holding state keys #92

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import cafe.adriel.voyager.navigator.internal.NavigatorBackHandler
import cafe.adriel.voyager.navigator.internal.NavigatorDisposableEffect
import cafe.adriel.voyager.navigator.internal.StepDisposableEffect
import cafe.adriel.voyager.navigator.internal.rememberNavigator
import java.util.concurrent.ConcurrentSkipListSet

public typealias NavigatorContent = @Composable (navigator: Navigator) -> Unit

Expand Down Expand Up @@ -106,7 +107,7 @@ public class Navigator internal constructor(
lastItemOrNull ?: error("Navigator has no screen")
}

private val stateKeys = ThreadSafeList<String>()
private val stateKeys = ConcurrentSkipListSet<String>()

@Deprecated(
message = "Use 'lastItem' instead. Will be removed in 1.0.0.",
Expand Down Expand Up @@ -139,20 +140,29 @@ public class Navigator internal constructor(
}
}

internal fun dispose(
screen: Screen
) {
internal fun dispose(screen: Screen) {
val keyStart = screen.key
val keyEndExcl = screen.key.successor
val keysToRemove = HashSet(stateKeys.subSet(keyStart, keyEndExcl))
ScreenModelStore.remove(screen)
ScreenLifecycleStore.remove(screen)
stateKeys
.asSequence()
.filter { it.startsWith(screen.key) }
.forEach { key ->
stateHolder.removeState(key)
stateKeys -= key
}
stateKeys.removeAll(keysToRemove)
for (key in keysToRemove) {
stateHolder.removeState(key)
}
}
}
/**
* @return the successor of a given string
*/
private val String.successor: String get() = if(isEmpty()) {
// Special case
"${Char.MIN_VALUE}" // \uFFFF
} else {
// Assumes [Char.MAX_VALUE] is never used, since \uFFFF is not a valid Unicode character. A more thorough implementation would add a character on overflow.
"${substring(0, length - 1)}${get(length - 1) + 1}"
}


public data class NavigatorDisposeBehavior(
val disposeNestedNavigators: Boolean = true,
Expand Down