Skip to content

Commit

Permalink
Use AbortController, which is recommended in new scheduling APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
MonicaOlejniczak committed Jun 3, 2021
1 parent 565b31f commit ef3d762
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/scheduler/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,16 @@ const scheduleTask = ({
return noopCleanup;
}

const controller = new AbortController();
const { signal } = controller;
const { runEffect, cleanupEffects } = createEffects();

const sleepEffect = runEffect(createSleepEffect(timeout));
const waitForEffect = runEffect(() => waitForEmptyQueue(priority - 1));

// A dispose check is needed for when any of the effects resolve immediately
// TODO The AbortController, or other cancellation techniques, should be used here when they become more standardised
let disposed = false;

Promise.race([sleepEffect, waitForEffect])
.then(() => {
if (!disposed) {
if (!signal.aborted) {
runTask();
}
})
Expand All @@ -44,7 +43,7 @@ const scheduleTask = ({

// Return a cleanup function that allows the scheduled task to be cancelled manually
return () => {
disposed = true;
controller.abort();
cleanupEffects();
};
};
Expand Down

0 comments on commit ef3d762

Please sign in to comment.