From 982f4c596ef0995bbe3bc5381b93dbdecabbc97a Mon Sep 17 00:00:00 2001 From: Andrew Clark Date: Mon, 7 Dec 2020 11:39:04 -0600 Subject: [PATCH] Remove `catch` from Scheduler build Makes debugging errors harder. In this case, we can use `finally` instead. --- packages/scheduler/src/forks/SchedulerDOM.js | 24 ++++++++++------- .../src/forks/SchedulerPostTaskOnly.js | 26 +++++++++++-------- 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/packages/scheduler/src/forks/SchedulerDOM.js b/packages/scheduler/src/forks/SchedulerDOM.js index 53101a6e6e580..e872b3ae65900 100644 --- a/packages/scheduler/src/forks/SchedulerDOM.js +++ b/packages/scheduler/src/forks/SchedulerDOM.js @@ -519,21 +519,25 @@ const performWorkUntilDeadline = () => { // the message event. deadline = currentTime + yieldInterval; const hasTimeRemaining = true; + + // If a scheduler task throws, exit the current browser task so the + // error can be observed. + // + // Intentionally not using a try-catch, since that makes some debugging + // techniques harder. Instead, if `scheduledHostCallback` errors, then + // `hasMoreWork` will remain true, and we'll continue the work loop. + let hasMoreWork = true; try { - const hasMoreWork = scheduledHostCallback(hasTimeRemaining, currentTime); - if (!hasMoreWork) { - isMessageLoopRunning = false; - scheduledHostCallback = null; - } else { + hasMoreWork = scheduledHostCallback(hasTimeRemaining, currentTime); + } finally { + if (hasMoreWork) { // If there's more work, schedule the next message event at the end // of the preceding one. port.postMessage(null); + } else { + isMessageLoopRunning = false; + scheduledHostCallback = null; } - } catch (error) { - // If a scheduler task throws, exit the current browser task so the - // error can be observed. - port.postMessage(null); - throw error; } } else { isMessageLoopRunning = false; diff --git a/packages/scheduler/src/forks/SchedulerPostTaskOnly.js b/packages/scheduler/src/forks/SchedulerPostTaskOnly.js index ba081d3236e5b..8ee96e2d16e54 100644 --- a/packages/scheduler/src/forks/SchedulerPostTaskOnly.js +++ b/packages/scheduler/src/forks/SchedulerPostTaskOnly.js @@ -512,21 +512,25 @@ const performWorkUntilDeadline = () => { // the message event. deadline = currentTime + yieldInterval; const hasTimeRemaining = true; + + // If a scheduler task throws, exit the current browser task so the + // error can be observed. + // + // Intentionally not using a try-catch, since that makes some debugging + // techniques harder. Instead, if `scheduledHostCallback` errors, then + // `hasMoreWork` will remain true, and we'll continue the work loop. + let hasMoreWork = true; try { - const hasMoreWork = scheduledHostCallback(hasTimeRemaining, currentTime); - if (!hasMoreWork) { + hasMoreWork = scheduledHostCallback(hasTimeRemaining, currentTime); + } finally { + if (hasMoreWork) { + // If there's more work, schedule the next browser task at the end of + // the preceding one. + postTask(performWorkUntilDeadline); + } else { isTaskLoopRunning = false; scheduledHostCallback = null; - } else { - // If there's more work, schedule the next message event at the end - // of the preceding one. - postTask(performWorkUntilDeadline); } - } catch (error) { - // If a scheduler task throws, exit the current browser task so the - // error can be observed. - postTask(performWorkUntilDeadline); - throw error; } } else { isTaskLoopRunning = false;