Skip to content

Commit 9c9c46f

Browse files
committed
feat: use scheduler postTask if possible
1 parent cc8e526 commit 9c9c46f

3 files changed

Lines changed: 27 additions & 4 deletions

File tree

src/nextTick/browser.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
1+
interface SchedulerPostTaskOptions {
2+
priority?: "user-blocking" | "user-visible" | "background";
3+
delay?: number;
4+
signal?: AbortSignal;
5+
}
6+
7+
interface Scheduler {
8+
postTask<T>(
9+
callback: () => T | Promise<T>,
10+
options?: SchedulerPostTaskOptions,
11+
): Promise<T>;
12+
13+
yield?(): Promise<void>;
14+
}
15+
16+
declare global {
17+
const scheduler: Scheduler | undefined;
18+
}
19+
120
/**
221
* @private
322
*
423
* Continues with the callback on the next tick.
524
*/
6-
export const nextTick: (callback: () => void) => void = setTimeout;
25+
export const nextTick: (callback: () => void) => unknown =
26+
typeof scheduler === "object" && typeof scheduler.postTask === "function"
27+
? scheduler.postTask.bind(scheduler)
28+
: setTimeout;

src/shims.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
declare module "random" {
22
export const random: (length: number) => number[] | Buffer;
33
}
4+
5+
declare module "nextTick" {
6+
export const nextTick: (callback: () => void) => unknown;
7+
}

src/shims2.d.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)