Skip to content

Commit 69ab7c7

Browse files
author
Brian Vaughn
committed
Replaced multi try/catch with nested try/finally
1 parent 69fd285 commit 69ab7c7

File tree

2 files changed

+36
-107
lines changed

2 files changed

+36
-107
lines changed

packages/interaction-tracking/src/InteractionTracking.js

Lines changed: 36 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -157,65 +157,42 @@ export function track(
157157
// Update before calling callback in case it schedules follow-up work.
158158
interaction.__count = 1;
159159

160-
let caughtError;
161-
let didCatch = false;
162160
let returnValue;
163161
const subscriber = subscriberRef.current;
164162

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-
177163
try {
178-
returnValue = callback();
179-
} catch (error) {
180-
if (!didCatch) {
181-
didCatch = true;
182-
caughtError = error;
164+
if (subscriber !== null) {
165+
subscriber.onInteractionTracked(interaction);
183166
}
184-
}
185-
186-
interactionsRef.current = prevInteractions;
187-
188-
if (subscriber !== null) {
167+
} finally {
189168
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);
195171
}
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+
}
210191
}
211192
}
212193
}
213194

214-
if (didCatch) {
215-
throw caughtError;
216-
} else {
217-
return returnValue;
218-
}
195+
return returnValue;
219196
} else {
220197
try {
221198
return callback();
@@ -262,54 +239,31 @@ export function wrap(
262239
const subscriber = subscriberRef.current;
263240

264241
try {
265-
let caughtError;
266-
let didCatch = false;
267242
let returnValue;
268243

269244
try {
270245
if (subscriber !== null) {
271246
subscriber.onWorkStarted(wrappedInteractions, threadID);
272247
}
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+
}
277257
}
278258
}
279259

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;
307261
} finally {
308262
// Update pending async counts for all wrapped interactions.
309263
// If this was the last scheduled async work for any of them,
310264
// Mark them as completed.
311265
wrappedInteractions.forEach(interaction => {
312-
interaction.__count--;
266+
interaction.__count = interaction.__count - 1;
313267

314268
if (subscriber !== null && interaction.__count === 0) {
315269
subscriber.onInteractionScheduledWorkCompleted(interaction);

packages/interaction-tracking/src/__tests__/InteractionTracking-test.internal.js

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -486,17 +486,6 @@ describe('InteractionTracking', () => {
486486
done();
487487
});
488488

489-
it('should always re-throw the first error from track', () => {
490-
throwInOnWorkStarted = true;
491-
throwInOnWorkStopped = true;
492-
493-
expect(() => {
494-
InteractionTracking.track(firstEvent.name, currentTime, () => {
495-
throw Error('Expected error track');
496-
});
497-
}).toThrow('Expected error onWorkStarted');
498-
});
499-
500489
it('should cover onWorkScheduled within wrap', done => {
501490
InteractionTracking.track(firstEvent.name, currentTime, () => {
502491
const interaction = Array.from(
@@ -621,20 +610,6 @@ describe('InteractionTracking', () => {
621610
onInteractionScheduledWorkCompleted,
622611
).toHaveBeenLastNotifiedOfInteraction(firstEvent);
623612
});
624-
625-
it('should always re-throw the first error from wrap', () => {
626-
let wrapped;
627-
InteractionTracking.track(firstEvent.name, currentTime, () => {
628-
wrapped = InteractionTracking.wrap(() => {
629-
throw Error('Expected error wrap');
630-
});
631-
});
632-
633-
throwInOnWorkStarted = true;
634-
throwInOnWorkStopped = true;
635-
636-
expect(wrapped).toThrow('Expected error onWorkStarted');
637-
});
638613
});
639614

640615
it('calls lifecycle methods for track', () => {

0 commit comments

Comments
 (0)