Repository navigation
ref(android): [Nav3 Lifecycle 2] Add lease-aware NavScopes facade #6304
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
Open
0xadam-brown
wants to merge
4
commits into
feat/sentry-nav-effect-lifecycle
Choose a base branch
from
feat/part-2-sentry-nav-effect-lifecycle
base: feat/sentry-nav-effect-lifecycle
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,513
−1
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
23b6ced
ref(android): [Nav3 Lifecycle 2] Add lease-aware navigation scopes
0xadam-brown d12f110
docs(android): Note the cost of reading INavScope.app
0xadam-brown f673e42
perf(android): Copy only the app context when reading INavScope.app
0xadam-brown 6d8b5ae
fix(android): Release the nav transaction when unbinding throws
0xadam-brown File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
151 changes: 151 additions & 0 deletions
151
...y-android-navigation3/src/main/kotlin/io/sentry/compose/navigation3/lifecycle/NavScope.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| package io.sentry.compose.navigation3.lifecycle | ||
|
|
||
| import io.sentry.IScope | ||
| import io.sentry.ITransaction | ||
| import io.sentry.PropagationContext | ||
| import io.sentry.protocol.App | ||
|
|
||
| /** | ||
| * An [INavScope] implementation that writes to and reads from [delegate] only when [owner] holds | ||
| * the nav lease; otherwise it falls back to [stagedNavData]. | ||
| */ | ||
| internal class NavScope( | ||
| private val delegate: IScope, | ||
| private val stagedNavData: StagedNavData, | ||
| private val coordinator: NavLeaseCoordinator, | ||
| private val owner: Any, | ||
| ) : INavScope { | ||
|
|
||
| private val hasLease: Boolean | ||
| get() = coordinator.hasLease(owner) | ||
|
|
||
| override var screen: String? | ||
| get() = if (hasLease) delegate.screen else stagedNavData.screen | ||
| set(value) { | ||
| stagedNavData.stageScreen(value) | ||
| if (hasLease) { | ||
| stagedNavData.publishScreen(delegate) | ||
| } | ||
| } | ||
|
|
||
| override fun setNavigationContext(value: Any?) { | ||
| stagedNavData.stageNavigationContext(value) | ||
| if (hasLease) { | ||
| stagedNavData.publishNavigationContext(delegate) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Copies the scope's [App] directly, leaving the rest of [IScope.getContexts] untouched. | ||
| * | ||
| * Callers mutate the result before binding it to a transaction, so this can't hand out the | ||
| * scope's live `App`. It copies only the `App` rather than the whole `Contexts`, which would | ||
| * deep-copy every other context on the scope - on Android including a fully populated `Device`, | ||
| * with its architecture array and time zone - and then discard all of it. | ||
| */ | ||
| override val app: App? | ||
| get() = delegate.contexts.app?.let(::App) | ||
|
|
||
| /** | ||
| * The transaction bound to the scope. Without the lease, another owner's nav transaction isn't | ||
| * reported, so [owner]'s attempt to start a nav transaction reaches [NavScopes.startTransaction], | ||
| * which declines it and defers the propagation context rotation it implies. Non-nav transactions | ||
| * (e.g., the host's) are always reported, so they still block nav transactions as usual. | ||
| */ | ||
| override val transaction: ITransaction? | ||
| get() = | ||
| delegate.transaction?.takeUnless { current -> | ||
| !hasLease && coordinator.isNavTransaction(current) | ||
| } | ||
|
|
||
| override fun bindTransaction(transaction: ITransaction) { | ||
| if (!hasLease) { | ||
| return | ||
| } | ||
| delegate.withTransaction { current -> | ||
| if (current == null) { | ||
| delegate.transaction = transaction | ||
| } | ||
| } | ||
| } | ||
|
|
||
| override fun unbindTransaction(transaction: ITransaction) { | ||
| // Coordinator is called before clearTransaction() because the latter notifies the host app's | ||
| // scope observer(s), which could throw. Clearing the transaction first would cause the | ||
| // coordinator to retain a stale transaction. | ||
| coordinator.releaseTransaction(owner, transaction) | ||
| delegate.withTransaction { current -> | ||
| if (current === transaction) { | ||
| delegate.clearTransaction() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| override fun clearFinishedTransaction() { | ||
| if (!hasLease) { | ||
| return | ||
| } | ||
| delegate.withTransaction { current -> | ||
| if (current?.isFinished == true) { | ||
| delegate.clearTransaction() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| override fun rotatePropagationContext() { | ||
| if (hasLease) { | ||
| delegate.withPropagationContext { delegate.propagationContext = PropagationContext() } | ||
| } else { | ||
| stagedNavData.deferPropagationContextRotation() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * The subset of the [IScope] interface used to record navigation data. | ||
| * | ||
| * Protects against accidental misuse of what would otherwise be a partially-implemented `IScope` | ||
| * interface. New operations against the underlying `IScope` instance must be added here. Operations | ||
| * not directly delegable to `IScope` should ***not*** be added. | ||
| */ | ||
| internal interface INavScope { | ||
|
|
||
| var screen: String? | ||
|
|
||
| /** | ||
| * A copy of the scope's app context, if any. | ||
| * | ||
| * A copy rather than the live instance, because callers mutate the result before binding it to a | ||
| * transaction, and mutating the scope's own [App] would corrupt shared scope state. | ||
| */ | ||
| val app: App? | ||
|
|
||
| /** | ||
| * The transaction bound to the scope, if any, as far as starting a nav transaction is concerned: | ||
| * see [NavScope.transaction]. | ||
| */ | ||
| val transaction: ITransaction? | ||
|
|
||
| /** Binds [transaction] to the scope if no transaction is bound to it. */ | ||
| fun bindTransaction(transaction: ITransaction) | ||
|
|
||
| /** | ||
| * Unbinds [transaction] from the scope if it's the bound transaction, and ensures it won't be | ||
| * bound to the scope again. | ||
| */ | ||
| fun unbindTransaction(transaction: ITransaction) | ||
|
|
||
| /** Unbinds the scope's transaction if it has finished. */ | ||
| fun clearFinishedTransaction() | ||
|
|
||
| /** | ||
| * Sets or, given `null`, clears the value stored under [NAVIGATION_CONTEXT_KEY]. | ||
| * | ||
| * Implementations should clear via [IScope.setContexts] rather than [IScope.removeContexts], as | ||
| * the latter doesn't notify scope observers and therefore doesn't persist the removal to disk. | ||
| */ | ||
| fun setNavigationContext(value: Any?) | ||
|
|
||
| /** Replaces the scope's propagation context with a new one. */ | ||
| fun rotatePropagationContext() | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.