-
-
Notifications
You must be signed in to change notification settings - Fork 358
feat(tracing): Correlate deep links with the navigation they trigger #6264
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
alwx
wants to merge
5
commits into
main
Choose a base branch
from
alwx/features/deep-linking
base: main
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.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ff90614
feat(tracing): Correlate deep links with the navigation they trigger
alwx a2c3a6a
fix(tracing): Address PR review on deep-link / navigation correlation
alwx 9341912
Merge branch 'main' into alwx/features/deep-linking
alwx d728832
fix(tracing): Differentiate cold-start vs warm-open deep link attribuโฆ
alwx 2758a72
fix(tracing): Reject warm-open links that arrived before the dispatch
alwx 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
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
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
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
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,138 @@ | ||||||||||||||||||
| /** | ||||||||||||||||||
| * Cross-module hand-off between the {@link deeplinkIntegration} and the | ||||||||||||||||||
| * {@link reactNavigationIntegration} idle navigation span. | ||||||||||||||||||
| * | ||||||||||||||||||
| * Two delivery modes are supported, both of which need to work in practice: | ||||||||||||||||||
| * | ||||||||||||||||||
| * 1. **Pre-navigation (warm open / normal cold start):** the deep link is | ||||||||||||||||||
| * received before any navigation has been dispatched. The URL is stored in | ||||||||||||||||||
| * a single slot here; the next idle navigation span consumes it inside | ||||||||||||||||||
| * `updateLatestNavigationSpanWithCurrentRoute` (within `routeChangeTimeoutMs`). | ||||||||||||||||||
| * | ||||||||||||||||||
| * 2. **Late arrival (Expo Router auto-handled cold start):** Expo Router reads | ||||||||||||||||||
| * `Linking.getInitialURL()` independently and may finish the initial | ||||||||||||||||||
| * navigation *before* our integration's own `getInitialURL().then(...)` | ||||||||||||||||||
| * chain resolves. To still attribute that span, a synchronous listener may | ||||||||||||||||||
| * be registered (by the navigation integration) and receives every link as | ||||||||||||||||||
| * it arrives. If it tags a still-recording span, it returns `true` and the | ||||||||||||||||||
| * slot is left empty โ otherwise the link falls through to the slot. | ||||||||||||||||||
| */ | ||||||||||||||||||
|
|
||||||||||||||||||
| /** | ||||||||||||||||||
| * How the deep link reached the integration: | ||||||||||||||||||
| * - `cold-start` โ from `Linking.getInitialURL()`. The app may have been | ||||||||||||||||||
| * launched by the link; the *initial* navigation is plausibly its target, | ||||||||||||||||||
| * so retroactive attribution to an already-mounted initial span is allowed. | ||||||||||||||||||
| * - `warm-open` โ from the `'url'` event while the app was running. The | ||||||||||||||||||
| * triggered navigation has not happened yet, so the URL must wait in the | ||||||||||||||||||
| * pending slot โ retroactively tagging the *previous* navigation would | ||||||||||||||||||
| * attribute the link to the wrong span. | ||||||||||||||||||
| */ | ||||||||||||||||||
| export type DeepLinkSource = 'cold-start' | 'warm-open'; | ||||||||||||||||||
|
|
||||||||||||||||||
| export interface PendingDeepLink { | ||||||||||||||||||
| /** Raw URL as received from React Native's `Linking` API. */ | ||||||||||||||||||
| url: string; | ||||||||||||||||||
| /** Wall-clock timestamp (ms since epoch) when the URL was received. */ | ||||||||||||||||||
| receivedAtMs: number; | ||||||||||||||||||
| /** How the link arrived โ governs retroactive attribution rules. */ | ||||||||||||||||||
| source: DeepLinkSource; | ||||||||||||||||||
| /** | ||||||||||||||||||
| * Monotonic sequence number shared with `nextEventSeq()`. The navigation | ||||||||||||||||||
| * integration tags every dispatched nav span with a seq from the same | ||||||||||||||||||
| * sequence โ a warm-open link must only consume the slot for spans whose | ||||||||||||||||||
| * seq is strictly greater than the link's, i.e. were dispatched *after* the | ||||||||||||||||||
| * link arrived. | ||||||||||||||||||
| */ | ||||||||||||||||||
| seq: number; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| /** | ||||||||||||||||||
| * Synchronously notified for every deep link as it arrives. A `true` return | ||||||||||||||||||
| * value indicates the listener has already attributed the link to a live span, | ||||||||||||||||||
| * and the value should NOT be stored for a future navigation. | ||||||||||||||||||
| */ | ||||||||||||||||||
| export type PendingDeepLinkListener = (link: PendingDeepLink) => boolean; | ||||||||||||||||||
|
|
||||||||||||||||||
| let pending: PendingDeepLink | undefined; | ||||||||||||||||||
| let listener: PendingDeepLinkListener | undefined; | ||||||||||||||||||
| let seqCounter = 0; | ||||||||||||||||||
|
|
||||||||||||||||||
| /** | ||||||||||||||||||
| * Returns the next value in the shared monotonic sequence. Used by the | ||||||||||||||||||
| * navigation integration to stamp each dispatched nav span, so consumers can | ||||||||||||||||||
| * tell whether a pending link arrived before or after a given dispatch. | ||||||||||||||||||
| */ | ||||||||||||||||||
| export function nextEventSeq(): number { | ||||||||||||||||||
| return ++seqCounter; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| /** | ||||||||||||||||||
| * Stores the most recently received deep link URL together with the current | ||||||||||||||||||
| * timestamp. If a listener is registered and consumes the link synchronously, | ||||||||||||||||||
| * the slot is left empty. | ||||||||||||||||||
| * | ||||||||||||||||||
| * Overwrites any previous unconsumed pending value โ only the latest link | ||||||||||||||||||
| * matters for correlation with the next navigation. | ||||||||||||||||||
| */ | ||||||||||||||||||
| export function setPendingDeepLink(url: string, source: DeepLinkSource): void { | ||||||||||||||||||
| const value: PendingDeepLink = { | ||||||||||||||||||
| url, | ||||||||||||||||||
| receivedAtMs: Date.now(), | ||||||||||||||||||
| source, | ||||||||||||||||||
| seq: nextEventSeq(), | ||||||||||||||||||
| }; | ||||||||||||||||||
| if (listener?.(value)) { | ||||||||||||||||||
| return; | ||||||||||||||||||
| } | ||||||||||||||||||
| pending = value; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| /** | ||||||||||||||||||
| * Returns the pending deep link without clearing the slot, applying the same | ||||||||||||||||||
| * staleness check as {@link consumePendingDeepLink}. A stale value is dropped | ||||||||||||||||||
| * (so subsequent calls do not return it) but no fresh value is returned. | ||||||||||||||||||
| */ | ||||||||||||||||||
| export function peekPendingDeepLink(maxAgeMs: number): PendingDeepLink | undefined { | ||||||||||||||||||
| if (!pending) { | ||||||||||||||||||
| return undefined; | ||||||||||||||||||
| } | ||||||||||||||||||
| if (Date.now() - pending.receivedAtMs > maxAgeMs) { | ||||||||||||||||||
| pending = undefined; | ||||||||||||||||||
| return undefined; | ||||||||||||||||||
| } | ||||||||||||||||||
| return pending; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| /** | ||||||||||||||||||
| * Returns and clears the pending deep link, but only if it was received | ||||||||||||||||||
| * within `maxAgeMs` of "now". Stale entries are discarded and the slot is | ||||||||||||||||||
| * cleared in all cases. | ||||||||||||||||||
| */ | ||||||||||||||||||
| export function consumePendingDeepLink(maxAgeMs: number): PendingDeepLink | undefined { | ||||||||||||||||||
| const value = pending; | ||||||||||||||||||
| pending = undefined; | ||||||||||||||||||
| if (!value) { | ||||||||||||||||||
| return undefined; | ||||||||||||||||||
| } | ||||||||||||||||||
|
Comment on lines
+114
to
+117
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about clearing pending only when it is defined?
Suggested change
|
||||||||||||||||||
| if (Date.now() - value.receivedAtMs > maxAgeMs) { | ||||||||||||||||||
| return undefined; | ||||||||||||||||||
| } | ||||||||||||||||||
| return value; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| /** | ||||||||||||||||||
| * Registers a synchronous listener that is invoked on every {@link setPendingDeepLink} | ||||||||||||||||||
| * call. Pass `undefined` to unregister. Only a single listener is supported โ | ||||||||||||||||||
| * a new registration replaces the previous one. | ||||||||||||||||||
| */ | ||||||||||||||||||
| export function setPendingDeepLinkListener(fn: PendingDeepLinkListener | undefined): void { | ||||||||||||||||||
| listener = fn; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| /** Test helper โ clears the pending value, listener, and sequence counter. */ | ||||||||||||||||||
| export function clearPendingDeepLink(): void { | ||||||||||||||||||
| pending = undefined; | ||||||||||||||||||
| listener = undefined; | ||||||||||||||||||
| seqCounter = 0; | ||||||||||||||||||
| } | ||||||||||||||||||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think of shorting the changelog? Users can see more details about it when opening the PR URL.