Skip to content

Commit a56486b

Browse files
committed
feat(command-flow): use microtask for scheduling
1 parent 7e3c259 commit a56486b

1 file changed

Lines changed: 25 additions & 1 deletion

File tree

packages/command-flow/src/scheduler.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
import { forwardRef, inject, Injectable, PendingTasks } from '@angular/core';
22

3+
/**
4+
* Service that schedules a function to be executed after the current
5+
* execution context is completed.
6+
*
7+
* Uses `MicrotaskCommandFlowScheduler` by default.
8+
*
9+
* This is usually used to schedule the dispatch of command events.
10+
*/
311
@Injectable({
412
providedIn: 'root',
5-
useExisting: forwardRef(() => SetTimeoutCommandFlowScheduler),
13+
useExisting: forwardRef(() => MicrotaskCommandFlowScheduler),
614
})
715
export abstract class CommandFlowScheduler {
816
abstract next(fn: () => void): void;
@@ -26,6 +34,22 @@ export class SetTimeoutCommandFlowScheduler implements CommandFlowScheduler {
2634
}
2735
}
2836

37+
/**
38+
* Implementation of {@link CommandFlowScheduler} that uses
39+
* `queueMicrotask` to schedule the next execution.
40+
* The scheduled function is added to {@link PendingTask} for SSR support.
41+
*/
42+
export class MicrotaskCommandFlowScheduler implements CommandFlowScheduler {
43+
#tasks = inject(PendingTasks);
44+
next(fn: () => void): void {
45+
const done = this.#tasks.add();
46+
queueMicrotask(() => {
47+
fn();
48+
done();
49+
});
50+
}
51+
}
52+
2953
/**
3054
* Implementation of {@link CommandFlowScheduler} that uses
3155
* `requestAnimationFrame` to schedule the next execution.

0 commit comments

Comments
 (0)