|
| 1 | +import type { Client, Integration } from '@sentry/core'; |
| 2 | + |
| 3 | +import { debug } from '@sentry/core'; |
| 4 | + |
| 5 | +import { getReactNavigationIntegration, reactNavigationIntegration } from './reactnavigation'; |
| 6 | + |
| 7 | +export const INTEGRATION_NAME = 'ExpoRouter'; |
| 8 | + |
| 9 | +const POLL_INTERVAL_MS = 50; |
| 10 | +const POLL_MAX_DURATION_MS = 5_000; |
| 11 | + |
| 12 | +interface ExpoRouterNavigationRef { |
| 13 | + current: unknown | null; |
| 14 | +} |
| 15 | + |
| 16 | +interface ExpoRouterStore { |
| 17 | + navigationRef?: ExpoRouterNavigationRef; |
| 18 | +} |
| 19 | + |
| 20 | +type ExpoRouterIntegrationOptions = Parameters<typeof reactNavigationIntegration>[0]; |
| 21 | + |
| 22 | +/** |
| 23 | + * Integration that connects Expo Router with `reactNavigationIntegration` without |
| 24 | + * requiring the user to manually pass a `useNavigationContainerRef()` ref. |
| 25 | + * |
| 26 | + * @example |
| 27 | + * ```ts |
| 28 | + * Sentry.init({ |
| 29 | + * integrations: [Sentry.expoRouterIntegration()], |
| 30 | + * }); |
| 31 | + * ``` |
| 32 | + */ |
| 33 | +export const expoRouterIntegration = (options: ExpoRouterIntegrationOptions = {}): Integration => { |
| 34 | + let pollTimer: ReturnType<typeof setTimeout> | undefined; |
| 35 | + |
| 36 | + const afterAllSetup = (client: Client): void => { |
| 37 | + const store = tryGetExpoRouterStore(); |
| 38 | + if (!store) { |
| 39 | + // expo-router not installed |
| 40 | + return; |
| 41 | + } |
| 42 | + if (!store.navigationRef) { |
| 43 | + debug.warn( |
| 44 | + `${INTEGRATION_NAME} Found expo-router router-store but it does not expose a \`navigationRef\`. ` + |
| 45 | + `This likely means the installed expo-router version is incompatible with this integration.`, |
| 46 | + ); |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + // reuse the user's reactNavigationIntegration if they registered one manually. |
| 51 | + // Otherwise, create and add one. |
| 52 | + let reactNavigation = getReactNavigationIntegration(client); |
| 53 | + if (!reactNavigation) { |
| 54 | + reactNavigation = reactNavigationIntegration(options); |
| 55 | + client.addIntegration(reactNavigation); |
| 56 | + } |
| 57 | + |
| 58 | + const navigationRef = store.navigationRef; |
| 59 | + |
| 60 | + if (navigationRef.current) { |
| 61 | + reactNavigation.registerNavigationContainer(navigationRef); |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + // Otherwise, poll until the Root Layout mounts and Expo Router sets `.current`. |
| 66 | + const startedAt = Date.now(); |
| 67 | + const poll = (): void => { |
| 68 | + if (!navigationRef.current) { |
| 69 | + if (Date.now() - startedAt >= POLL_MAX_DURATION_MS) { |
| 70 | + debug.warn(`${INTEGRATION_NAME} Timed out waiting for Expo Router navigation container.`); |
| 71 | + pollTimer = undefined; |
| 72 | + return; |
| 73 | + } |
| 74 | + pollTimer = setTimeout(poll, POLL_INTERVAL_MS); |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + reactNavigation?.registerNavigationContainer(navigationRef); |
| 79 | + pollTimer = undefined; |
| 80 | + }; |
| 81 | + |
| 82 | + pollTimer = setTimeout(poll, POLL_INTERVAL_MS); |
| 83 | + |
| 84 | + client.on('close', () => { |
| 85 | + if (pollTimer !== undefined) { |
| 86 | + clearTimeout(pollTimer); |
| 87 | + pollTimer = undefined; |
| 88 | + } |
| 89 | + }); |
| 90 | + }; |
| 91 | + |
| 92 | + return { |
| 93 | + name: INTEGRATION_NAME, |
| 94 | + afterAllSetup, |
| 95 | + }; |
| 96 | +}; |
| 97 | + |
| 98 | +function tryGetExpoRouterStore(): ExpoRouterStore | null { |
| 99 | + try { |
| 100 | + // eslint-disable-next-line @typescript-eslint/no-var-requires |
| 101 | + const mod = require('expo-router/build/global-state/router-store') as { |
| 102 | + store?: ExpoRouterStore; |
| 103 | + }; |
| 104 | + return mod?.store ?? null; |
| 105 | + } catch { |
| 106 | + return null; |
| 107 | + } |
| 108 | +} |
0 commit comments