Skip to content

Commit a0031dd

Browse files
committed
ref(android): Let NavScopes own Nav3 effect disposal (JAVA-274)
1 parent f299e75 commit a0031dd

4 files changed

Lines changed: 8 additions & 47 deletions

File tree

‎sentry-android-navigation3/src/main/kotlin/io/sentry/compose/navigation3/BackStackObserver.kt‎

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ private const val NAVIGATION_OP: String = "navigation"
4747
*
4848
* **Thread safety**
4949
*
50-
* This class is ***not*** thread-safe. Clients should serialize calls to [onBackStackChanged] and
51-
* [cleanup] (e.g., via invocation from an `*Effect` or another form of thread confinement).
50+
* This class is ***not*** thread-safe. Clients should serialize calls to [onBackStackChanged]
51+
* (e.g., via invocation from an `*Effect` or another form of thread confinement).
5252
*/
5353
internal class BackStackObserver<T : Any>(
5454
private val scopes: INavScopes,
@@ -111,25 +111,6 @@ internal class BackStackObserver<T : Any>(
111111
scopes.configureScope { scope -> applyChange(scope, change) }
112112
}
113113

114-
internal fun cleanup() {
115-
previousTop = null
116-
previousTopNormalized = null
117-
118-
scopes.configureScope { scope ->
119-
navTransaction.stop(scope)
120-
navScreen.clear(scope)
121-
122-
if (options.captureBackStack) {
123-
// This observer owns the nav context while it's in the composition, and cleanup removes
124-
// it to avoid leaking stale back stack data after observation stops. If the host app
125-
// replaces one observer with another, there may be a brief gap where events lack nav
126-
// context. Apps should keep the observer at the nav root so cleanup only runs when the
127-
// navigation session is ending, not during normal destination changes.
128-
navContext.clear(scope)
129-
}
130-
}
131-
}
132-
133114
private fun prepareChange(backStack: List<T>): PreparedChange<T> {
134115
val topEntry = backStack.lastOrNull() ?: return BackStackIsEmpty
135116
val data = backStack.extractData()

‎sentry-android-navigation3/src/main/kotlin/io/sentry/compose/navigation3/lifecycle/CoordinatedBackStackObserver.kt‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import io.sentry.compose.navigation3.ForwardingBackStackEntryMapper
99
import io.sentry.compose.navigation3.SentryNavOptions
1010

1111
/**
12-
* A [BackStackObserver] that coordinates its updates to a shared [IScope] instance with other
12+
* A [BackStackObserver] whose updates to a shared mutable [IScope] are coordinated with other
1313
* `BackStackObserver`s.
1414
*
1515
* Coordination is achieved via [NavScopes].
@@ -44,10 +44,7 @@ internal class CoordinatedBackStackObserver<T : Any>(
4444
}
4545

4646
fun dispose() {
47-
// Clear only data still belonging to this instance and restore any eligible previous owner.
48-
// Observer cleanup then runs without the lease, so it cannot clear host or restored state.
4947
navScopes.dispose()
50-
observer.cleanup()
5148
}
5249
}
5350

‎sentry-android-navigation3/src/test/kotlin/io/sentry/compose/navigation3/BackStackObserverTest.kt‎

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -741,23 +741,6 @@ class BackStackObserverTest {
741741
)
742742
}
743743

744-
@Test
745-
fun `cleanup clears observer owned tracked state`() {
746-
val fixture = Fixture()
747-
val sut = fixture.getSut()
748-
749-
sut.onBackStackChanged(listOf(HomeScreen()))
750-
val transaction = fixture.startedTransactions.single()
751-
752-
sut.cleanup()
753-
754-
assertThat(transaction.isFinished).isTrue()
755-
assertThat(fixture.scope.transaction).isNull()
756-
assertThat(fixture.scope.screen).isNull()
757-
assertThat(fixture.scope.contexts.app?.viewNames).isNull()
758-
assertThat(fixture.scope.contexts.containsKey(NAVIGATION_CONTEXT_KEY)).isFalse()
759-
}
760-
761744
private fun IScope.navigationBackStack(): List<Map<String, Any?>>? {
762745
val navigationContext = contexts[NAVIGATION_CONTEXT_KEY] as? Map<*, *> ?: return null
763746

‎sentry-android-navigation3/src/test/kotlin/io/sentry/compose/navigation3/lifecycle/CoordinatedBackStackObserverTest.kt‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,20 @@ import io.sentry.compose.navigation3.SentryNavOptions
77
import org.junit.Test
88
import org.mockito.kotlin.inOrder
99
import org.mockito.kotlin.mock
10+
import org.mockito.kotlin.verify
11+
import org.mockito.kotlin.verifyNoInteractions
1012

1113
class CoordinatedBackStackObserverTest {
1214

1315
@Test
14-
fun `dispose() - disposes nav scopes before cleaning up the back stack observer`() {
16+
fun `dispose() - disposes nav scopes without touching the back stack observer`() {
1517
val fixture = Fixture.create()
1618
val coordinatedObserver = fixture.coordinatedObserver
1719

1820
coordinatedObserver.dispose()
1921

20-
inOrder(fixture.navScopes, fixture.wrappedObserver) {
21-
verify(fixture.navScopes).dispose()
22-
verify(fixture.wrappedObserver).cleanup()
23-
}
22+
verify(fixture.navScopes).dispose()
23+
verifyNoInteractions(fixture.wrappedObserver)
2424
}
2525

2626
@Test

0 commit comments

Comments
 (0)