@@ -157,65 +157,42 @@ export function track(
157
157
// Update before calling callback in case it schedules follow-up work.
158
158
interaction . __count = 1 ;
159
159
160
- let caughtError ;
161
- let didCatch = false ;
162
160
let returnValue ;
163
161
const subscriber = subscriberRef . current ;
164
162
165
- if ( subscriber !== null ) {
166
- try {
167
- subscriber . onInteractionTracked ( interaction ) ;
168
- subscriber . onWorkStarted ( interactions , threadID ) ;
169
- } catch ( error ) {
170
- if ( ! didCatch ) {
171
- didCatch = true ;
172
- caughtError = error ;
173
- }
174
- }
175
- }
176
-
177
163
try {
178
- returnValue = callback ( ) ;
179
- } catch ( error ) {
180
- if ( ! didCatch ) {
181
- didCatch = true ;
182
- caughtError = error ;
164
+ if ( subscriber !== null ) {
165
+ subscriber . onInteractionTracked ( interaction ) ;
183
166
}
184
- }
185
-
186
- interactionsRef . current = prevInteractions ;
187
-
188
- if ( subscriber !== null ) {
167
+ } finally {
189
168
try {
190
- subscriber . onWorkStopped ( interactions , threadID ) ;
191
- } catch ( error ) {
192
- if ( ! didCatch ) {
193
- didCatch = true ;
194
- caughtError = error ;
169
+ if ( subscriber !== null ) {
170
+ subscriber . onWorkStarted ( interactions , threadID ) ;
195
171
}
196
- }
197
- }
198
-
199
- interaction . __count -- ;
200
-
201
- // If no async work was scheduled for this interaction,
202
- // Notify subscribers that it's completed.
203
- if ( subscriber !== null && interaction . __count === 0 ) {
204
- try {
205
- subscriber . onInteractionScheduledWorkCompleted ( interaction ) ;
206
- } catch ( error ) {
207
- if ( ! didCatch ) {
208
- didCatch = true ;
209
- caughtError = error ;
172
+ } finally {
173
+ try {
174
+ returnValue = callback ( ) ;
175
+ } finally {
176
+ interactionsRef . current = prevInteractions ;
177
+
178
+ try {
179
+ if ( subscriber !== null ) {
180
+ subscriber . onWorkStopped ( interactions , threadID ) ;
181
+ }
182
+ } finally {
183
+ interaction . __count = interaction . __count - 1 ;
184
+
185
+ // If no async work was scheduled for this interaction,
186
+ // Notify subscribers that it's completed.
187
+ if ( subscriber !== null && interaction . __count === 0 ) {
188
+ subscriber . onInteractionScheduledWorkCompleted ( interaction ) ;
189
+ }
190
+ }
210
191
}
211
192
}
212
193
}
213
194
214
- if ( didCatch ) {
215
- throw caughtError ;
216
- } else {
217
- return returnValue ;
218
- }
195
+ return returnValue ;
219
196
} else {
220
197
try {
221
198
return callback ( ) ;
@@ -262,54 +239,31 @@ export function wrap(
262
239
const subscriber = subscriberRef . current ;
263
240
264
241
try {
265
- let caughtError ;
266
- let didCatch = false ;
267
242
let returnValue ;
268
243
269
244
try {
270
245
if ( subscriber !== null ) {
271
246
subscriber . onWorkStarted ( wrappedInteractions , threadID ) ;
272
247
}
273
- } catch ( error ) {
274
- if ( ! didCatch ) {
275
- didCatch = true ;
276
- caughtError = error ;
248
+ } finally {
249
+ try {
250
+ returnValue = callback . apply ( undefined , arguments ) ;
251
+ } finally {
252
+ interactionsRef . current = prevInteractions ;
253
+
254
+ if ( subscriber !== null ) {
255
+ subscriber . onWorkStopped ( wrappedInteractions , threadID ) ;
256
+ }
277
257
}
278
258
}
279
259
280
- try {
281
- returnValue = callback . apply ( undefined , arguments ) ;
282
- } catch ( error ) {
283
- if ( ! didCatch ) {
284
- didCatch = true ;
285
- caughtError = error ;
286
- }
287
- }
288
-
289
- interactionsRef . current = prevInteractions ;
290
-
291
- try {
292
- if ( subscriber !== null ) {
293
- subscriber . onWorkStopped ( wrappedInteractions , threadID ) ;
294
- }
295
- } catch ( error ) {
296
- if ( ! didCatch ) {
297
- didCatch = true ;
298
- caughtError = error ;
299
- }
300
- }
301
-
302
- if ( didCatch ) {
303
- throw caughtError ;
304
- } else {
305
- return returnValue ;
306
- }
260
+ return returnValue ;
307
261
} finally {
308
262
// Update pending async counts for all wrapped interactions.
309
263
// If this was the last scheduled async work for any of them,
310
264
// Mark them as completed.
311
265
wrappedInteractions . forEach ( interaction => {
312
- interaction . __count -- ;
266
+ interaction . __count = interaction . __count - 1 ;
313
267
314
268
if ( subscriber !== null && interaction . __count === 0 ) {
315
269
subscriber . onInteractionScheduledWorkCompleted ( interaction ) ;
0 commit comments