|
1 | 1 | package io.sentry.compose.navigation3.lifecycle |
2 | 2 |
|
| 3 | +import io.sentry.IScopes |
3 | 4 | import io.sentry.ITransaction |
4 | 5 | import io.sentry.SpanStatus |
| 6 | +import io.sentry.TransactionContext |
| 7 | +import io.sentry.TransactionOptions |
5 | 8 | import io.sentry.protocol.App |
6 | 9 |
|
7 | 10 | /** |
8 | | - * An [INavTransaction] handle backed by a started [ITransaction]. |
| 11 | + * An [INavTransaction] handle backed by an [ITransaction] that's either started right away or, when |
| 12 | + * the nav lease isn't held, [started later][start]. |
| 13 | + * |
| 14 | + * A deferred handle records what's set on it and replays it onto the transaction once started. |
| 15 | + * [Finishing][finish] a deferred handle before it starts discards it, so its transaction never |
| 16 | + * starts. |
9 | 17 | * |
10 | 18 | * **Thread safety** |
11 | 19 | * |
12 | 20 | * This class is ***not*** thread-safe. Apply the thread safety policy from [NavScopes]. |
13 | 21 | */ |
14 | 22 | internal class NavTransaction |
15 | 23 | private constructor( |
16 | | - /** The backing transaction. */ |
17 | | - val transaction: ITransaction |
| 24 | + val name: String, |
| 25 | + private var state: State, |
18 | 26 | ) : INavTransaction { |
19 | 27 |
|
20 | | - val name: String |
21 | | - get() = transaction.name |
| 28 | + /** |
| 29 | + * The backing transaction, once [started][State.Started]; `null` while [pending][State.Pending] |
| 30 | + * or once [discarded][State.Discarded]. |
| 31 | + */ |
| 32 | + val transaction: ITransaction? |
| 33 | + get() = (state as? State.Started)?.transaction |
22 | 34 |
|
23 | 35 | override fun setData(key: String, value: Any) { |
24 | | - transaction.setData(key, value) |
| 36 | + when (val state = state) { |
| 37 | + is State.Pending -> state.data[key] = value |
| 38 | + is State.Started -> state.transaction.setData(key, value) |
| 39 | + State.Discarded -> Unit |
| 40 | + } |
25 | 41 | } |
26 | 42 |
|
27 | 43 | override fun setContext(key: String, value: Any) { |
28 | | - transaction.setContext(key, value) |
| 44 | + when (val state = state) { |
| 45 | + is State.Pending -> state.contexts[key] = value |
| 46 | + is State.Started -> state.transaction.setContext(key, value) |
| 47 | + State.Discarded -> Unit |
| 48 | + } |
29 | 49 | } |
30 | 50 |
|
31 | 51 | override fun setApp(app: App) { |
32 | | - transaction.contexts.setApp(app) |
| 52 | + when (val state = state) { |
| 53 | + is State.Pending -> state.app = app |
| 54 | + is State.Started -> state.transaction.contexts.setApp(app) |
| 55 | + State.Discarded -> Unit |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Starts a deferred handle's transaction via [scopes] and replays what was set on the handle onto |
| 61 | + * it. |
| 62 | + * |
| 63 | + * Returns the started transaction, or `null` if the handle was started already or discarded, or |
| 64 | + * [scopes] declined to start one. |
| 65 | + */ |
| 66 | + fun start(scopes: IScopes): ITransaction? { |
| 67 | + val pending = state as? State.Pending ?: return null |
| 68 | + val transaction = |
| 69 | + scopes.startTransaction(pending.context, pending.options).takeUnless { it.isNoOp } |
| 70 | + ?: return null |
| 71 | + |
| 72 | + state = State.Started(transaction) |
| 73 | + pending.data.forEach { (key, value) -> transaction.setData(key, value) } |
| 74 | + pending.contexts.forEach { (key, value) -> transaction.setContext(key, value) } |
| 75 | + pending.app?.let { transaction.contexts.setApp(it) } |
| 76 | + return transaction |
33 | 77 | } |
34 | 78 |
|
35 | 79 | /** |
36 | 80 | * Finishes the backing transaction with its own status, or [OK][SpanStatus.OK] if it has none, |
37 | 81 | * and then passes it to [onFinished]. |
| 82 | + * |
| 83 | + * A deferred handle that hasn't started is discarded instead, so it never starts, and |
| 84 | + * [onFinished] isn't called. |
38 | 85 | */ |
39 | 86 | fun finish(onFinished: (ITransaction) -> Unit) { |
40 | | - if (!transaction.isFinished) { |
41 | | - transaction.finish(transaction.status ?: SpanStatus.OK) |
| 87 | + when (val state = state) { |
| 88 | + is State.Pending -> this.state = State.Discarded |
| 89 | + is State.Started -> { |
| 90 | + if (!state.transaction.isFinished) { |
| 91 | + state.transaction.finish(state.transaction.status ?: SpanStatus.OK) |
| 92 | + } |
| 93 | + onFinished(state.transaction) |
| 94 | + } |
| 95 | + State.Discarded -> Unit |
42 | 96 | } |
43 | | - onFinished(transaction) |
| 97 | + } |
| 98 | + |
| 99 | + /** Lifecycle state for a [NavTransaction] handle. */ |
| 100 | + private sealed interface State { |
| 101 | + |
| 102 | + /** |
| 103 | + * Not started yet: the transaction will be started from [context] and [options], and what's set |
| 104 | + * on the handle until then is recorded for replay. |
| 105 | + */ |
| 106 | + class Pending(val context: TransactionContext, val options: TransactionOptions) : State { |
| 107 | + val data = linkedMapOf<String, Any>() |
| 108 | + val contexts = linkedMapOf<String, Any>() |
| 109 | + var app: App? = null |
| 110 | + } |
| 111 | + |
| 112 | + /** The backing [transaction] has been started. A terminal [State]. */ |
| 113 | + class Started(val transaction: ITransaction) : State |
| 114 | + |
| 115 | + /** The handle was finished before its backing transaction was started. A terminal [State]. */ |
| 116 | + data object Discarded : State |
44 | 117 | } |
45 | 118 |
|
46 | 119 | companion object { |
47 | 120 | /** A handle backed by the already-started [transaction]. */ |
48 | | - fun started(transaction: ITransaction): NavTransaction = NavTransaction(transaction) |
| 121 | + fun started(transaction: ITransaction): NavTransaction = |
| 122 | + NavTransaction(transaction.name, State.Started(transaction)) |
| 123 | + |
| 124 | + /** A handle whose transaction is started later, via [start]. */ |
| 125 | + fun deferred(context: TransactionContext, options: TransactionOptions): NavTransaction = |
| 126 | + NavTransaction(context.name, State.Pending(context, options)) |
49 | 127 | } |
50 | 128 | } |
51 | 129 |
|
|
0 commit comments