@@ -3,28 +3,28 @@ import { logger } from "@sentry/utils";
33
44import { RoutingInstrumentation } from "./router" ;
55
6- interface NavigationRoute {
6+ interface NavigationRouteV4 {
77 routeName : string ;
88 key : string ;
99 params ?: Record < any , any > ;
1010}
1111
12- interface NavigationState {
12+ interface NavigationStateV4 {
1313 index : number ;
1414 key : string ;
1515 isTransitioning : boolean ;
1616 routeName ?: string ;
17- routes : ( NavigationRoute | NavigationState ) [ ] ;
17+ routes : ( NavigationRouteV4 | NavigationStateV4 ) [ ] ;
1818}
1919
2020interface AppContainerInstance {
2121 _navigation : {
22- state : NavigationState ;
22+ state : NavigationStateV4 ;
2323 router : {
2424 getStateForAction : (
2525 action : any ,
26- state : NavigationState
27- ) => NavigationState ;
26+ state : NavigationStateV4
27+ ) => NavigationStateV4 ;
2828 } ;
2929 } ;
3030}
@@ -34,19 +34,19 @@ interface AppContainerRef {
3434}
3535
3636type ReactNavigationV4ShouldAttachTransaction = (
37- prevRoute : NavigationRoute | null ,
38- newRoute : NavigationRoute
37+ route : NavigationRouteV4 ,
38+ previousRoute : NavigationRouteV4 | null
3939) => boolean ;
4040
4141interface ReactNavigationV4InstrumentationOptions {
42- shouldAttachTransaction : ReactNavigationV4ShouldAttachTransaction ;
42+ shouldSendTransaction : ReactNavigationV4ShouldAttachTransaction ;
4343}
4444
4545const defaultShouldAttachTransaction : ReactNavigationV4ShouldAttachTransaction = ( ) =>
4646 true ;
4747
4848const DEFAULT_OPTIONS : ReactNavigationV4InstrumentationOptions = {
49- shouldAttachTransaction : defaultShouldAttachTransaction ,
49+ shouldSendTransaction : defaultShouldAttachTransaction ,
5050} ;
5151
5252/**
@@ -60,7 +60,7 @@ class ReactNavigationV4Instrumentation extends RoutingInstrumentation {
6060
6161 private _options : ReactNavigationV4InstrumentationOptions ;
6262
63- private _prevRoute : NavigationRoute | null = null ;
63+ private _prevRoute : NavigationRouteV4 | null = null ;
6464
6565 constructor ( options : Partial < ReactNavigationV4InstrumentationOptions > ) {
6666 super ( ) ;
@@ -103,17 +103,9 @@ class ReactNavigationV4Instrumentation extends RoutingInstrumentation {
103103 */
104104 private _handleInitialState ( ) : void {
105105 if ( this . _appContainerRef . current ) {
106- const navigationState = this . _appContainerRef . current . _navigation . state ;
106+ const state = this . _appContainerRef . current . _navigation . state ;
107107
108- const currentRoute = this . _getCurrentRouteFromState ( navigationState ) ;
109-
110- if (
111- this . _options . shouldAttachTransaction ( this . _prevRoute , currentRoute )
112- ) {
113- this . onRouteWillChange ( this . _getTransactionContext ( currentRoute ) ) ;
114- }
115-
116- this . _prevRoute = currentRoute ;
108+ this . _onStateChange ( state ) ;
117109 }
118110 }
119111
@@ -130,35 +122,41 @@ class ReactNavigationV4Instrumentation extends RoutingInstrumentation {
130122 action ,
131123 state
132124 ) => {
133- const newNavigationState = originalGetStateForAction ( action , state ) ;
134- const currentRoute = this . _getCurrentRouteFromState ( newNavigationState ) ;
135-
136- // If the route is a different key, this is so we ignore actions that pertain to the same screen.
137- if ( ! this . _prevRoute || currentRoute . key !== this . _prevRoute . key ) {
138- const context = this . _getTransactionContext ( currentRoute ) ;
139-
140- if (
141- this . _options . shouldAttachTransaction ( this . _prevRoute , currentRoute )
142- ) {
143- this . onRouteWillChange ( context ) ;
144- } else {
145- logger . log (
146- `[ReactNavigationV4Instrumentation] Will not send transaction "${ context . name } " due to shouldAttachTransaction.`
147- ) ;
148- }
149-
150- this . _prevRoute = currentRoute ;
151- }
152-
153- return newNavigationState ;
125+ const newState = originalGetStateForAction ( action , state ) ;
126+
127+ this . _onStateChange ( newState ) ;
128+
129+ return newState ;
154130 } ;
155131 }
156132 }
157133
158134 /**
159- * Gets the transaction context for a `NavigationRoute`
135+ * To be called on navigation state changes and creates the transaction.
136+ */
137+ private _onStateChange ( state : NavigationStateV4 ) : void {
138+ const currentRoute = this . _getCurrentRouteFromState ( state ) ;
139+
140+ // If the route is a different key, this is so we ignore actions that pertain to the same screen.
141+ if ( ! this . _prevRoute || currentRoute . key !== this . _prevRoute . key ) {
142+ const context = this . _getTransactionContext ( currentRoute ) ;
143+
144+ if ( this . _options . shouldSendTransaction ( currentRoute , this . _prevRoute ) ) {
145+ this . onRouteWillChange ( context ) ;
146+ } else {
147+ logger . log (
148+ `[ReactNavigationV4Instrumentation] Will not send transaction "${ context . name } " due to shouldSendTransaction.`
149+ ) ;
150+ }
151+
152+ this . _prevRoute = currentRoute ;
153+ }
154+ }
155+
156+ /**
157+ * Gets the transaction context for a `NavigationRouteV4`
160158 */
161- private _getTransactionContext ( route : NavigationRoute ) : TransactionContext {
159+ private _getTransactionContext ( route : NavigationRouteV4 ) : TransactionContext {
162160 return {
163161 name : route . routeName ,
164162 op : "navigation" ,
@@ -174,7 +172,9 @@ class ReactNavigationV4Instrumentation extends RoutingInstrumentation {
174172 /**
175173 * Gets the current route given a navigation state
176174 */
177- private _getCurrentRouteFromState ( state : NavigationState ) : NavigationRoute {
175+ private _getCurrentRouteFromState (
176+ state : NavigationStateV4
177+ ) : NavigationRouteV4 {
178178 const parentRoute = state . routes [ state . index ] ;
179179
180180 if (
@@ -186,7 +186,7 @@ class ReactNavigationV4Instrumentation extends RoutingInstrumentation {
186186 return this . _getCurrentRouteFromState ( parentRoute ) ;
187187 }
188188
189- return parentRoute as NavigationRoute ;
189+ return parentRoute as NavigationRouteV4 ;
190190 }
191191}
192192
0 commit comments