Skip to content

Commit 90e3981

Browse files
0xadam-browncodex
andcommitted
ref(android-nav3): colocate nav observer constants
Move nav context and transaction constants into the helper classes that own them, while keeping the shared navigation operation name at file scope. This tightens locality without changing behavior. Co-Authored-By: OpenAI GPT-5.4 <noreply@openai.com>
1 parent 24905a3 commit 90e3981

1 file changed

Lines changed: 19 additions & 20 deletions

File tree

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

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ import io.sentry.protocol.TransactionNameSource
2222
import io.sentry.util.IntegrationUtils.addIntegrationToSdkVersion
2323
import java.lang.ref.WeakReference
2424

25-
private const val BACKSTACK_KEY = "backstack"
26-
private const val NAVIGATION_CONTEXT_KEY = "navigation"
2725
private const val NAVIGATION_OP: String = "navigation"
28-
private const val TRANSACTION_ORIGIN = "auto.navigation.nav3"
2926

3027
/**
3128
* Observes the back stack managed by a single [SentryNavEffect] and records Sentry state as the
@@ -61,7 +58,7 @@ internal class BackStackObserver<T : Any>(
6158

6259
private val routeTranslator = RouteTranslator(extractors, scopes.options.logger)
6360

64-
private val navTransaction = NavTransaction(scopes, NAVIGATION_OP, TRANSACTION_ORIGIN)
61+
private val navTransaction = NavTransaction(scopes)
6562
private val navContext = NavContext(scopes, options)
6663
private val navScreen = NavScreen()
6764
private val navBreadcrumbs = NavBreadcrumbs(scopes)
@@ -282,11 +279,11 @@ private data class BackStackData<T>(
282279
)
283280

284281
/** A helper class for managing nav transactions. */
285-
private class NavTransaction(
286-
private val scopes: IScopes,
287-
private val navigationOp: String,
288-
private val transactionOrigin: String,
289-
) {
282+
private class NavTransaction(private val scopes: IScopes) {
283+
284+
private companion object {
285+
private const val TRANSACTION_ORIGIN = "auto.navigation.nav3"
286+
}
290287

291288
private var activeNavTransaction: ITransaction? = null
292289

@@ -296,7 +293,7 @@ private class NavTransaction(
296293
name: String,
297294
arguments: Map<String, Any?>,
298295
): ITransaction? {
299-
clearFinishedTransaction(scope)
296+
clearTransactionIfFinished(scope)
300297

301298
if (scope.transaction != null) {
302299
scopes.options.logger.log(
@@ -315,12 +312,12 @@ private class NavTransaction(
315312
val deadlineTimeoutMillis = scopes.options.deadlineTimeout
316313
it.deadlineTimeout = if (deadlineTimeoutMillis <= 0) null else deadlineTimeoutMillis
317314
it.isTrimEnd = true
318-
it.origin = transactionOrigin
315+
it.origin = TRANSACTION_ORIGIN
319316
}
320317

321318
val transaction =
322319
scopes.startTransaction(
323-
TransactionContext(name, TransactionNameSource.ROUTE, navigationOp),
320+
TransactionContext(name, TransactionNameSource.ROUTE, NAVIGATION_OP),
324321
transactionOptions,
325322
)
326323

@@ -357,7 +354,7 @@ private class NavTransaction(
357354
}
358355

359356
/** Clears a stale finished transaction that's still bound to the default scope. */
360-
private fun clearFinishedTransaction(scope: IScope) {
357+
private fun clearTransactionIfFinished(scope: IScope) {
361358
scope.withTransaction { tx ->
362359
if (tx?.isFinished == true) {
363360
scope.clearTransaction()
@@ -369,6 +366,11 @@ private class NavTransaction(
369366
/** A helper class for updating [nav context][NAVIGATION_CONTEXT_KEY]. */
370367
private class NavContext(private val scopes: IScopes, private val options: SentryNavOptions) {
371368

369+
private companion object {
370+
private const val BACKSTACK_KEY = "backstack"
371+
private const val NAVIGATION_CONTEXT_KEY = "navigation"
372+
}
373+
372374
fun update(scope: IScope, backStackRoutes: List<Route>) {
373375
if (backStackRoutes.isEmpty()) {
374376
clear(scope)
@@ -379,7 +381,9 @@ private class NavContext(private val scopes: IScopes, private val options: Sentr
379381
}
380382

381383
fun clear(scope: IScope) {
382-
removeNavContext(scope)
384+
// We purposefully don't call IScope.removeContexts(), as it doesn't notify IScopeObserver and
385+
// therefore doesn't write its updates to disk ¯\_ (ツ)_/¯.
386+
scope.setContexts(NAVIGATION_CONTEXT_KEY, null as Any?)
383387
}
384388

385389
/**
@@ -409,12 +413,6 @@ private class NavContext(private val scopes: IScopes, private val options: Sentr
409413
}
410414
}
411415

412-
private fun removeNavContext(scope: IScope) {
413-
// We purposefully don't call IScope.removeContexts(), as it doesn't notify IScopeObserver and
414-
// therefore doesn't write its updates to disk ¯\_ (ツ)_/¯.
415-
scope.setContexts(NAVIGATION_CONTEXT_KEY, null as Any?)
416-
}
417-
418416
/** Builds the `{"backstack": [...]}` map bound under [NAVIGATION_CONTEXT_KEY]. */
419417
private fun List<Route>.toBackStackMap(): Map<String, Any?> = mapOf(BACKSTACK_KEY to serialize())
420418
}
@@ -468,6 +466,7 @@ private class NavBreadcrumbs(private val scopes: IScopes) {
468466

469467
val hint = Hint()
470468
hint.set(TypeCheckHint.ANDROID_NAV3_DESTINATION, toEntry)
469+
471470
scopes.addBreadcrumb(breadcrumb, hint)
472471
}
473472
}

0 commit comments

Comments
 (0)