7
7
* @flow
8
8
*/
9
9
10
- import {
11
- enableInteractionTracking ,
12
- enableInteractionTrackingObserver ,
13
- } from 'shared/ReactFeatureFlags' ;
10
+ import { enableInteractionTracking } from 'shared/ReactFeatureFlags' ;
14
11
15
12
export type Interaction = { |
16
13
__count : number ,
@@ -71,18 +68,15 @@ let threadIDCounter: number = 0;
71
68
let interactionsRef : InteractionsRef = ( null : any ) ;
72
69
73
70
// Listener(s) to notify when interactions begin and end.
74
- // Note that subscribers are only supported when enableInteractionTrackingObserver is enabled.
75
71
let subscriberRef : SubscriberRef = ( null : any ) ;
76
72
77
73
if ( enableInteractionTracking ) {
78
74
interactionsRef = {
79
75
current : new Set ( ) ,
80
76
} ;
81
- if ( enableInteractionTrackingObserver ) {
82
- subscriberRef = {
83
- current : null ,
84
- } ;
85
- }
77
+ subscriberRef = {
78
+ current : null ,
79
+ } ;
86
80
}
87
81
88
82
// These values are exported for libraries with advanced use cases (i.e. React).
@@ -127,7 +121,7 @@ export function track(
127
121
}
128
122
129
123
const interaction : Interaction = {
130
- __count : 0 ,
124
+ __count : 1 ,
131
125
id : interactionIDCounter ++ ,
132
126
name,
133
127
timestamp,
@@ -142,53 +136,42 @@ export function track(
142
136
interactions . add ( interaction ) ;
143
137
interactionsRef . current = interactions ;
144
138
145
- if ( enableInteractionTrackingObserver ) {
146
- // Update before calling callback in case it schedules follow-up work.
147
- interaction . __count = 1 ;
148
-
149
- let returnValue ;
150
- const subscriber = subscriberRef . current ;
139
+ const subscriber = subscriberRef . current ;
140
+ let returnValue ;
151
141
142
+ try {
143
+ if ( subscriber !== null ) {
144
+ subscriber . onInteractionTracked ( interaction ) ;
145
+ }
146
+ } finally {
152
147
try {
153
148
if ( subscriber !== null ) {
154
- subscriber . onInteractionTracked ( interaction ) ;
149
+ subscriber . onWorkStarted ( interactions , threadID ) ;
155
150
}
156
151
} finally {
157
152
try {
158
- if ( subscriber !== null ) {
159
- subscriber . onWorkStarted ( interactions , threadID ) ;
160
- }
153
+ returnValue = callback ( ) ;
161
154
} finally {
155
+ interactionsRef . current = prevInteractions ;
156
+
162
157
try {
163
- returnValue = callback ( ) ;
158
+ if ( subscriber !== null ) {
159
+ subscriber . onWorkStopped ( interactions , threadID ) ;
160
+ }
164
161
} finally {
165
- interactionsRef . current = prevInteractions ;
162
+ interaction . __count -- ;
166
163
167
- try {
168
- if ( subscriber !== null ) {
169
- subscriber . onWorkStopped ( interactions , threadID ) ;
170
- }
171
- } finally {
172
- interaction . __count -- ;
173
-
174
- // If no async work was scheduled for this interaction,
175
- // Notify subscribers that it's completed.
176
- if ( subscriber !== null && interaction . __count === 0 ) {
177
- subscriber . onInteractionScheduledWorkCompleted ( interaction ) ;
178
- }
164
+ // If no async work was scheduled for this interaction,
165
+ // Notify subscribers that it's completed.
166
+ if ( subscriber !== null && interaction . __count === 0 ) {
167
+ subscriber . onInteractionScheduledWorkCompleted ( interaction ) ;
179
168
}
180
169
}
181
170
}
182
171
}
183
-
184
- return returnValue ;
185
- } else {
186
- try {
187
- return callback ( ) ;
188
- } finally {
189
- interactionsRef . current = prevInteractions ;
190
- }
191
172
}
173
+
174
+ return returnValue ;
192
175
}
193
176
194
177
export function wrap (
@@ -201,89 +184,77 @@ export function wrap(
201
184
202
185
const wrappedInteractions = interactionsRef . current ;
203
186
204
- if ( enableInteractionTrackingObserver ) {
205
- const subscriber = subscriberRef . current ;
206
- if ( subscriber !== null ) {
207
- subscriber . onWorkScheduled ( wrappedInteractions , threadID ) ;
208
- }
209
-
210
- // Update the pending async work count for the current interactions.
211
- // Update after calling subscribers in case of error.
212
- wrappedInteractions . forEach ( interaction => {
213
- interaction . __count ++ ;
214
- } ) ;
187
+ let subscriber = subscriberRef . current ;
188
+ if ( subscriber !== null ) {
189
+ subscriber . onWorkScheduled ( wrappedInteractions , threadID ) ;
215
190
}
216
191
217
- const wrapped = ( ) => {
192
+ // Update the pending async work count for the current interactions.
193
+ // Update after calling subscribers in case of error.
194
+ wrappedInteractions . forEach ( interaction => {
195
+ interaction . __count ++ ;
196
+ } ) ;
197
+
198
+ function wrapped ( ) {
218
199
const prevInteractions = interactionsRef . current ;
219
200
interactionsRef . current = wrappedInteractions ;
220
201
221
- if ( enableInteractionTrackingObserver ) {
222
- const subscriber = subscriberRef . current ;
202
+ subscriber = subscriberRef . current ;
223
203
224
- try {
225
- let returnValue ;
204
+ try {
205
+ let returnValue ;
226
206
227
- try {
228
- if ( subscriber !== null ) {
229
- subscriber . onWorkStarted ( wrappedInteractions , threadID ) ;
230
- }
231
- } finally {
232
- try {
233
- returnValue = callback . apply ( undefined , arguments ) ;
234
- } finally {
235
- interactionsRef . current = prevInteractions ;
236
-
237
- if ( subscriber !== null ) {
238
- subscriber . onWorkStopped ( wrappedInteractions , threadID ) ;
239
- }
240
- }
207
+ try {
208
+ if ( subscriber !== null ) {
209
+ subscriber . onWorkStarted ( wrappedInteractions , threadID ) ;
241
210
}
242
-
243
- return returnValue ;
244
211
} finally {
245
- // Update pending async counts for all wrapped interactions.
246
- // If this was the last scheduled async work for any of them,
247
- // Mark them as completed.
248
- wrappedInteractions . forEach ( interaction => {
249
- interaction . __count -- ;
212
+ try {
213
+ returnValue = callback . apply ( undefined , arguments ) ;
214
+ } finally {
215
+ interactionsRef . current = prevInteractions ;
250
216
251
- if ( subscriber !== null && interaction . __count === 0 ) {
252
- subscriber . onInteractionScheduledWorkCompleted ( interaction ) ;
217
+ if ( subscriber !== null ) {
218
+ subscriber . onWorkStopped ( wrappedInteractions , threadID ) ;
253
219
}
254
- } ) ;
255
- }
256
- } else {
257
- try {
258
- return callback . apply ( undefined , arguments ) ;
259
- } finally {
260
- interactionsRef . current = prevInteractions ;
220
+ }
261
221
}
262
- }
263
- } ;
264
-
265
- if ( enableInteractionTrackingObserver ) {
266
- wrapped . cancel = ( ) => {
267
- const subscriber = subscriberRef . current ;
268
222
269
- try {
270
- if ( subscriber !== null ) {
271
- subscriber . onWorkCanceled ( wrappedInteractions , threadID ) ;
223
+ return returnValue ;
224
+ } finally {
225
+ // Update pending async counts for all wrapped interactions.
226
+ // If this was the last scheduled async work for any of them,
227
+ // Mark them as completed.
228
+ wrappedInteractions . forEach ( interaction => {
229
+ interaction . __count -- ;
230
+
231
+ if ( subscriber !== null && interaction . __count === 0 ) {
232
+ subscriber . onInteractionScheduledWorkCompleted ( interaction ) ;
272
233
}
273
- } finally {
274
- // Update pending async counts for all wrapped interactions.
275
- // If this was the last scheduled async work for any of them,
276
- // Mark them as completed.
277
- wrappedInteractions . forEach ( interaction => {
278
- interaction . __count -- ;
234
+ } ) ;
235
+ }
236
+ }
279
237
280
- if ( subscriber && interaction . __count === 0 ) {
281
- subscriber . onInteractionScheduledWorkCompleted ( interaction ) ;
282
- }
283
- } ) ;
238
+ wrapped . cancel = function cancel ( ) {
239
+ subscriber = subscriberRef . current ;
240
+
241
+ try {
242
+ if ( subscriber !== null ) {
243
+ subscriber . onWorkCanceled ( wrappedInteractions , threadID ) ;
284
244
}
285
- } ;
286
- }
245
+ } finally {
246
+ // Update pending async counts for all wrapped interactions.
247
+ // If this was the last scheduled async work for any of them,
248
+ // Mark them as completed.
249
+ wrappedInteractions . forEach ( interaction => {
250
+ interaction . __count -- ;
251
+
252
+ if ( subscriber && interaction . __count === 0 ) {
253
+ subscriber . onInteractionScheduledWorkCompleted ( interaction ) ;
254
+ }
255
+ } ) ;
256
+ }
257
+ } ;
287
258
288
259
return wrapped ;
289
260
}
0 commit comments