Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}
}
}
Comment thread
cursor[bot] marked this conversation as resolved.

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()
}
Loading
Loading