Skip to content

Commit 123745c

Browse files
committed
ref(nav3): Extract back stack helpers and argument limits
1 parent 4b32d2a commit 123745c

13 files changed

Lines changed: 116 additions & 105 deletions

File tree

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

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package io.sentry.compose.navigation3
22

3-
import androidx.compose.runtime.snapshots.Snapshot
4-
import io.sentry.compose.navigation3.internal.backstack.BackStackObserver
53
import org.jetbrains.annotations.ApiStatus
64

75
/** Info about a given back stack entry, suitable for display in Sentry. */
@@ -151,24 +149,3 @@ public class SentryBackStackEntry(
151149
public fun interface BackStackEntryMapper<T : Any> {
152150
public fun map(backStackEntry: T): SentryBackStackEntry?
153151
}
154-
155-
/**
156-
* A Compose-compatible version of [BackStackEntryMapper] that generates [SentryBackStackEntry]s by
157-
* forwarding the request to the host app mapper in effect at the time [map] is called.
158-
*
159-
* Dynamically determining the current mapper lets us separate two concerns:
160-
*
161-
* 1. the lifetime of a consumer that tracks navigation state over time (e.g., [BackStackObserver]);
162-
* and
163-
* 2. the lifetime of the mapper used to generate `SentryBackStackEntry`s.
164-
*
165-
* Without that separation, a long-lived consumer would have to choose between holding stale mapping
166-
* logic or recreating its own state whenever the mapper changed.
167-
*/
168-
internal class ForwardingBackStackEntryMapper<T : Any>(
169-
private val currentMapper: () -> BackStackEntryMapper<T>,
170-
) {
171-
fun map(backStackEntry: T): SentryBackStackEntry? = Snapshot.withoutReadObservation {
172-
currentMapper().map(backStackEntry)
173-
}
174-
}

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

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import androidx.lifecycle.compose.LocalLifecycleOwner
99
import io.sentry.IScopes
1010
import io.sentry.ScopesAdapter
1111
import io.sentry.SentryOptions
12+
import io.sentry.compose.navigation3.internal.backstack.BackStackKey
1213
import io.sentry.compose.navigation3.internal.backstack.CoordinatedBackStackObserver
14+
import io.sentry.compose.navigation3.internal.backstack.ForwardingBackStackEntryMapper
1315
import io.sentry.compose.navigation3.internal.backstack.defaultNavLeaseCoordinator
1416
import io.sentry.compose.navigation3.internal.lifecycle.NavLeaseCoordinator
1517
import io.sentry.compose.navigation3.internal.lifecycle.NavRootInfo
@@ -144,37 +146,3 @@ internal fun <T : Any> SentryNavEffect(
144146
onDispose {}
145147
}
146148
}
147-
148-
/**
149-
* A key for distinguishing back stacks over time.
150-
*
151-
* Lets `*Effect`s restart when either the identity of a stack entry changes or the stack's entries
152-
* are reordered.
153-
*/
154-
internal class BackStackKey<T : Any>(private val backStack: List<T>) {
155-
156-
override fun equals(other: Any?): Boolean {
157-
// Use of identity rather than structural equality frees us from entries' equals() and
158-
// hashCode() implementations, which are provided by the host app and may be incomplete,
159-
// expensive, or incorrect for our purposes.
160-
if (this === other) {
161-
return true
162-
}
163-
if (other !is BackStackKey<*>) {
164-
return false
165-
}
166-
if (backStack.size != other.backStack.size) {
167-
return false
168-
}
169-
170-
return backStack.indices.all { index -> backStack[index] === other.backStack[index] }
171-
}
172-
173-
override fun hashCode(): Int {
174-
var result = backStack.size
175-
for (entry in backStack) {
176-
result = 31 * result + System.identityHashCode(entry)
177-
}
178-
return result
179-
}
180-
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package io.sentry.compose.navigation3.internal.backstack
2+
3+
internal object ArgumentConstraints {
4+
5+
/**
6+
* Max combined length of map keys and text values visited in one back stack update.
7+
*
8+
* If exceeded, the entry that overflows loses its arguments, as do older entries; newer entries
9+
* are preserved.
10+
*
11+
* Caps payload size in the presence of large individual arguments.
12+
*/
13+
const val MAX_ARGUMENT_CHARACTERS = 4_096
14+
15+
/**
16+
* Max depth of an argument value within one back stack entry. Root arguments have depth 1; each
17+
* nested map or list adds one level.
18+
*
19+
* For instance, `mapOf("id" to 123)` reaches a depth of 1; `mapOf("items" to listOf("apple",
20+
* "banana"))` reaches a depth of 2.
21+
*
22+
* If exceeded, all arguments for that back stack entry are dropped.
23+
*/
24+
const val MAX_ARGUMENT_DEPTH = 10
25+
26+
/**
27+
* Max number of argument values visited in one back stack update. Each scalar or nested container
28+
* counts as one value; map keys and each entry's root argument map are excluded.
29+
*
30+
* For instance, `mapOf("id" to 123)` consumes 1 value; `mapOf("profile" to mapOf("id" to 123,
31+
* "name" to "Ada"))` consumes 3 values.
32+
*
33+
* If exceeded, the entry that overflows loses its arguments, as do older entries; newer entries
34+
* are preserved.
35+
*
36+
* For instance, suppose we have the following back stack:
37+
*
38+
* - /Checkout -> Top of the stack and processed first
39+
* - /ProductDetail -> Processed second and exceeds the `MAX_ARGUMENT_VALUES` budget
40+
* - /Home
41+
*
42+
* Then /ProductDetail and /Home will have no arguments, but /Checkout will.
43+
*/
44+
const val MAX_ARGUMENT_VALUES = 200
45+
}

‎sentry-android-navigation3/src/main/kotlin/io/sentry/compose/navigation3/internal/backstack/BackStackConverter.kt‎

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ package io.sentry.compose.navigation3.internal.backstack
33
import io.sentry.ILogger
44
import io.sentry.SentryLevel.WARNING
55
import io.sentry.compose.navigation3.BackStackEntryMapper
6-
import io.sentry.compose.navigation3.ForwardingBackStackEntryMapper
76
import io.sentry.compose.navigation3.SentryBackStackEntry
7+
import io.sentry.compose.navigation3.internal.backstack.ArgumentConstraints.MAX_ARGUMENT_CHARACTERS
8+
import io.sentry.compose.navigation3.internal.backstack.ArgumentConstraints.MAX_ARGUMENT_DEPTH
9+
import io.sentry.compose.navigation3.internal.backstack.ArgumentConstraints.MAX_ARGUMENT_VALUES
810
import io.sentry.compose.navigation3.internal.backstack.ArgumentDropReason.Companion.ARGUMENT_DROP_REASON_KEY
911
import io.sentry.compose.navigation3.internal.backstack.NormalizedSentryBackStackEntry.Companion.UNKNOWN_ENTRY_NAME
1012
import io.sentry.util.ExceptionUtils
@@ -144,7 +146,7 @@ internal class BackStackConverter<T : Any>(
144146
) {
145147

146148
private val activeContainers = IdentityHashMap<Any, Unit>()
147-
private var remainingValues = MAX_ARGUMENT_COUNT
149+
private var remainingValues = MAX_ARGUMENT_VALUES
148150
private var remainingCharacters = MAX_ARGUMENT_CHARACTERS
149151
private var dropReason: ArgumentDropReason? = null
150152

@@ -289,44 +291,6 @@ internal class BackStackConverter<T : Any>(
289291

290292
private companion object {
291293

292-
/**
293-
* Max nesting depth allowed while sanitizing a single argument value for a given back stack
294-
* entry.
295-
*
296-
* For instance, `mapOf("id" to 123)` has a depth of 1; `mapOf("items" to listOf("apple",
297-
* "banana"))` has a depth of 2.
298-
*
299-
* If exceeded, all arguments for that back stack entry are dropped.
300-
*/
301-
private const val MAX_ARGUMENT_DEPTH = 10
302-
303-
/**
304-
* Max number of argument values visited while sanitizing all entries in a given back stack
305-
* update.
306-
*
307-
* For instance, `mapOf("id" to 123)` consumes 1 value; `mapOf("profile" to mapOf("id" to 123,
308-
* "name" to "Ada"))` consumes 3 values.
309-
*
310-
* If exceeded, the entry that overflows loses its arguments, as do older entries; newer
311-
* entries are preserved.
312-
*
313-
* For instance, suppose we have the following back stack:
314-
*
315-
* - /Checkout -> Top of the stack and processed first
316-
* - /ProductDetail -> Processed second and overflows the `MAX_ARGUMENT_VALUES` budget
317-
* - /Home
318-
*
319-
* Then /ProductDetail and /Home will have no arguments, but /Checkout will.
320-
*/
321-
private const val MAX_ARGUMENT_COUNT = 200
322-
323-
/**
324-
* Max number of characters visited while sanitizing all entries in a given back stack update.
325-
*
326-
* Caps payload size in the presence of large individual arguments.
327-
*/
328-
private const val MAX_ARGUMENT_CHARACTERS = 4_096
329-
330294
private const val MAX_CHARACTER_WARNING =
331295
"Nav3 arguments exceeded the maximum total character count for one backstack update. Skipping " +
332296
"arguments for this and older captured entries."
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package io.sentry.compose.navigation3.internal.backstack
2+
3+
/**
4+
* A key for distinguishing back stacks over time.
5+
*
6+
* Lets `*Effect`s restart when either the identity of a stack entry changes or the stack's entries
7+
* are reordered.
8+
*/
9+
internal class BackStackKey<T : Any>(private val backStack: List<T>) {
10+
11+
override fun equals(other: Any?): Boolean {
12+
// Use of identity rather than structural equality frees us from entries' equals() and
13+
// hashCode() implementations, which are provided by the host app and may be incomplete,
14+
// expensive, or incorrect for our purposes.
15+
if (this === other) {
16+
return true
17+
}
18+
if (other !is BackStackKey<*>) {
19+
return false
20+
}
21+
if (backStack.size != other.backStack.size) {
22+
return false
23+
}
24+
25+
return backStack.indices.all { index -> backStack[index] === other.backStack[index] }
26+
}
27+
28+
override fun hashCode(): Int {
29+
var result = backStack.size
30+
for (entry in backStack) {
31+
result = 31 * result + System.identityHashCode(entry)
32+
}
33+
return result
34+
}
35+
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ import io.sentry.Hint
55
import io.sentry.SentryIntegrationPackageStorage
66
import io.sentry.SentryLevel.INFO
77
import io.sentry.SentryOptions
8-
import io.sentry.compose.navigation3.BackStackKey
98
import io.sentry.compose.navigation3.BuildConfig
10-
import io.sentry.compose.navigation3.ForwardingBackStackEntryMapper
119
import io.sentry.compose.navigation3.SentryNavEffect
1210
import io.sentry.compose.navigation3.SentryNavOptions
1311
import io.sentry.compose.navigation3.internal.backstack.BackStackConverter.RetentionPolicy

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package io.sentry.compose.navigation3.internal.backstack
33
import androidx.lifecycle.LifecycleEventObserver
44
import io.sentry.IScope
55
import io.sentry.IScopes
6-
import io.sentry.compose.navigation3.ForwardingBackStackEntryMapper
76
import io.sentry.compose.navigation3.SentryNavOptions
87
import io.sentry.compose.navigation3.internal.lifecycle.NavLeaseCoordinator
98
import io.sentry.compose.navigation3.internal.lifecycle.NavRootInfo
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package io.sentry.compose.navigation3.internal.backstack
2+
3+
import androidx.compose.runtime.snapshots.Snapshot
4+
import io.sentry.compose.navigation3.BackStackEntryMapper
5+
import io.sentry.compose.navigation3.SentryBackStackEntry
6+
7+
/**
8+
* A Compose-compatible version of [BackStackEntryMapper] that generates [SentryBackStackEntry]s by
9+
* forwarding the request to the host app mapper in effect at the time [map] is called.
10+
*
11+
* Dynamically determining the current mapper lets us separate two concerns:
12+
*
13+
* 1. the lifetime of a consumer that tracks navigation state over time (e.g., [BackStackObserver]);
14+
* and
15+
* 2. the lifetime of the mapper used to generate `SentryBackStackEntry`s.
16+
*
17+
* Without that separation, a long-lived consumer would have to choose between holding stale mapping
18+
* logic or recreating its own state whenever the mapper changed.
19+
*/
20+
internal class ForwardingBackStackEntryMapper<T : Any>(
21+
private val currentMapper: () -> BackStackEntryMapper<T>,
22+
) {
23+
fun map(backStackEntry: T): SentryBackStackEntry? = Snapshot.withoutReadObservation {
24+
currentMapper().map(backStackEntry)
25+
}
26+
}

‎sentry-android-navigation3/src/test/kotlin/io/sentry/compose/navigation3/internal/backstack/BackStackConverterTest.kt‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import com.google.common.truth.Truth.assertThat
44
import io.sentry.ILogger
55
import io.sentry.SentryLevel.WARNING
66
import io.sentry.compose.navigation3.BackStackEntryMapper
7-
import io.sentry.compose.navigation3.ForwardingBackStackEntryMapper
87
import io.sentry.compose.navigation3.SentryBackStackEntry
98
import io.sentry.compose.navigation3.internal.backstack.ArgumentDropReason.Companion.ARGUMENT_DROP_REASON_KEY
109
import io.sentry.compose.navigation3.internal.backstack.BackStackConverter.RetentionPolicy

sentry-android-navigation3/src/test/kotlin/io/sentry/compose/navigation3/BackStackKeyTest.kt renamed to sentry-android-navigation3/src/test/kotlin/io/sentry/compose/navigation3/internal/backstack/BackStackKeyTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.sentry.compose.navigation3
1+
package io.sentry.compose.navigation3.internal.backstack
22

33
import com.google.common.truth.Truth.assertThat
44
import org.junit.Test

0 commit comments

Comments
 (0)