Skip to content

Commit

Permalink
fix(asapScheduler): No longer stops after scheduling twice during flu…
Browse files Browse the repository at this point in the history
…sh (#7198)

Fixes an issue where trying to share a microtask cause the scheduler to trip over itself.

resolves #7196
  • Loading branch information
trxcllnt authored Mar 1, 2023
1 parent 3c4b2ca commit 1b52405
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
24 changes: 24 additions & 0 deletions spec/schedulers/AsapScheduler-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,4 +288,28 @@ describe('Scheduler.asap', () => {
done();
});
});

it('scheduling inside of an executing action more than once should work', (done) => {
const results: any[] = [];

let resolve: () => void;
let promise = new Promise<void>((r) => resolve = r);

asapScheduler.schedule(() => {
results.push(1)
asapScheduler.schedule(() => {
results.push(2);
});
asapScheduler.schedule(() => {
results.push(3);
resolve();
});
});

promise.then(() => {
// This should always fire after two recursively scheduled microtasks.
expect(results).to.deep.equal([1, 2, 3]);
done();
});
});
});
4 changes: 3 additions & 1 deletion src/internal/scheduler/AsapAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ export class AsapAction<T> extends AsyncAction<T> {
const { actions } = scheduler;
if (id != null && actions[actions.length - 1]?.id !== id) {
immediateProvider.clearImmediate(id);
scheduler._scheduled = undefined;
if (scheduler._scheduled === id) {
scheduler._scheduled = undefined;
}
}
// Return undefined so the action knows to request a new async id if it's rescheduled.
return undefined;
Expand Down

0 comments on commit 1b52405

Please sign in to comment.