Skip to content

Commit 24905a3

Browse files
0xadam-browncodex
andcommitted
ref(android-nav3): clarify nav context clearing
Expose an explicit NavContext.clear helper for removing the navigation context instead of signaling deletion by passing an empty route list through update(). Also move previous-top state updates into the top-change handlers so each change path owns its own state mutation, matching the empty-back-stack path. This keeps the observer control flow easier to read without changing behavior. Co-Authored-By: OpenAI GPT-5.4 <noreply@openai.com>
1 parent 3110f44 commit 24905a3

2 files changed

Lines changed: 33 additions & 34 deletions

File tree

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

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ internal class BackStackObserver<T : Any>(
127127
// replaces one observer with another, there may be a brief gap where events lack nav
128128
// context. Apps should keep the observer at the nav root so cleanup only runs when the
129129
// navigation session is ending, not during normal destination changes.
130-
navContext.update(scope, emptyList())
130+
navContext.clear(scope)
131131
}
132132
}
133133
}
@@ -146,16 +146,8 @@ internal class BackStackObserver<T : Any>(
146146
private fun applyChange(scope: IScope, change: PreparedChange<T>) {
147147
when (change) {
148148
is BackStackIsEmpty -> handleEmptyBackStack(scope)
149-
150-
is BackStackHasNewTop -> {
151-
handleNewTop(scope, change.previousTop, change.backStack)
152-
storeAsPreviousTop(change.backStack.topEntry, change.backStack.topRoute)
153-
}
154-
155-
is BackStackHasSameTop -> {
156-
handleSameTop(scope, change.backStack)
157-
storeAsPreviousTop(change.backStack.topEntry, change.backStack.topRoute)
158-
}
149+
is BackStackHasNewTop -> handleNewTop(scope, change.previousTop, change.backStack)
150+
is BackStackHasSameTop -> handleSameTop(scope, change.backStack)
159151
}
160152
}
161153

@@ -219,35 +211,38 @@ internal class BackStackObserver<T : Any>(
219211
navTransaction.stop(scope)
220212

221213
if (areNavigationTransactionsEnabled) {
222-
navTransaction
223-
.start(
224-
scope,
225-
currentTopRoute.name,
226-
currentTopRoute.arguments,
227-
)
228-
?.let { transaction -> navContext.updateTransaction(transaction, scope, currentBackStack) }
214+
navTransaction.start(scope, currentTopRoute.name, currentTopRoute.arguments)?.let {
215+
navContext.updateTransaction(it, scope, currentBackStack)
216+
}
229217
} else {
230218
// Rotate the propagation context.
231219
scope.withPropagationContext { scope.setPropagationContext(PropagationContext()) }
232220
}
221+
222+
storeAsPreviousTop(currentBackStack.topEntry, currentBackStack.topRoute)
233223
}
234224

235225
private fun handleSameTop(scope: IScope, backStack: BackStackData<T>) {
236226
navContext.update(scope, backStack.capturedRoutes)
227+
storeAsPreviousTop(backStack.topEntry, backStack.topRoute)
237228
}
238229

239230
private fun handleEmptyBackStack(scope: IScope) {
240231
navTransaction.stop(scope)
232+
navContext.clear(scope)
241233
navScreen.clear(scope)
242-
navContext.update(scope, emptyList())
243-
previousTopEntry = null
244-
previousTopRoute = null
234+
clearPreviousTop()
245235
}
246236

247237
private fun storeAsPreviousTop(topEntry: T, topRoute: Route) {
248238
previousTopEntry = WeakReference(topEntry)
249239
previousTopRoute = topRoute
250240
}
241+
242+
private fun clearPreviousTop() {
243+
previousTopEntry = null
244+
previousTopRoute = null
245+
}
251246
}
252247

253248
/**
@@ -298,16 +293,16 @@ private class NavTransaction(
298293
/** Starts an idle navigation transaction, or no-ops if another transaction is already active. */
299294
fun start(
300295
scope: IScope,
301-
routeName: String,
296+
name: String,
302297
arguments: Map<String, Any?>,
303298
): ITransaction? {
304-
clearFinishedScopeTransaction(scope)
299+
clearFinishedTransaction(scope)
305300

306301
if (scope.transaction != null) {
307302
scopes.options.logger.log(
308303
DEBUG,
309304
"Nav3 transaction for route %s won't be created because another transaction is active.",
310-
routeName,
305+
name,
311306
)
312307

313308
return null
@@ -325,7 +320,7 @@ private class NavTransaction(
325320

326321
val transaction =
327322
scopes.startTransaction(
328-
TransactionContext(routeName, TransactionNameSource.ROUTE, navigationOp),
323+
TransactionContext(name, TransactionNameSource.ROUTE, navigationOp),
329324
transactionOptions,
330325
)
331326

@@ -362,7 +357,7 @@ private class NavTransaction(
362357
}
363358

364359
/** Clears a stale finished transaction that's still bound to the default scope. */
365-
private fun clearFinishedScopeTransaction(scope: IScope) {
360+
private fun clearFinishedTransaction(scope: IScope) {
366361
scope.withTransaction { tx ->
367362
if (tx?.isFinished == true) {
368363
scope.clearTransaction()
@@ -376,10 +371,15 @@ private class NavContext(private val scopes: IScopes, private val options: Sentr
376371

377372
fun update(scope: IScope, backStackRoutes: List<Route>) {
378373
if (backStackRoutes.isEmpty()) {
379-
removeNavContext(scope)
380-
} else {
381-
scope.setContexts(NAVIGATION_CONTEXT_KEY, backStackRoutes.toNavigationContext())
374+
clear(scope)
375+
return
382376
}
377+
378+
scope.setContexts(NAVIGATION_CONTEXT_KEY, backStackRoutes.toBackStackMap())
379+
}
380+
381+
fun clear(scope: IScope) {
382+
removeNavContext(scope)
383383
}
384384

385385
/**
@@ -405,7 +405,7 @@ private class NavContext(private val scopes: IScopes, private val options: Sentr
405405
}
406406

407407
if (options.captureBackStack && backStack.capturedRoutes.isNotEmpty()) {
408-
transaction.setContext(NAVIGATION_CONTEXT_KEY, backStack.capturedRoutes.toNavigationContext())
408+
transaction.setContext(NAVIGATION_CONTEXT_KEY, backStack.capturedRoutes.toBackStackMap())
409409
}
410410
}
411411

@@ -416,8 +416,7 @@ private class NavContext(private val scopes: IScopes, private val options: Sentr
416416
}
417417

418418
/** Builds the `{"backstack": [...]}` map bound under [NAVIGATION_CONTEXT_KEY]. */
419-
private fun List<Route>.toNavigationContext(): Map<String, Any?> =
420-
mapOf(BACKSTACK_KEY to serialize())
419+
private fun List<Route>.toBackStackMap(): Map<String, Any?> = mapOf(BACKSTACK_KEY to serialize())
421420
}
422421

423422
/** A helper class for updating the tracked screen name. */

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,14 @@ internal fun <T : Any> SentryNavEffect(
9898
options: SentryNavOptions = SentryNavOptions(),
9999
scopes: IScopes,
100100
) {
101-
val routeResolvers = rememberUpdatedState(RouteResolvers(nameExtractor, argumentsExtractor))
101+
val routeExtractors = rememberUpdatedState(RouteExtractors(nameExtractor, argumentsExtractor))
102102

103103
val observer =
104104
remember(scopes, options) {
105105
BackStackObserver(
106106
scopes = scopes,
107107
options = options,
108-
resolvers = { routeResolvers.value },
108+
extractors = { routeExtractors.value },
109109
)
110110
}
111111

0 commit comments

Comments
 (0)