Last change
on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1.0 KB
|
Line | |
---|
1 | let nextHandle = 1;
|
---|
2 | const RESOLVED = (() => Promise.resolve())();
|
---|
3 | const activeHandles: { [key: number]: any } = {};
|
---|
4 |
|
---|
5 | /**
|
---|
6 | * Finds the handle in the list of active handles, and removes it.
|
---|
7 | * Returns `true` if found, `false` otherwise. Used both to clear
|
---|
8 | * Immediate scheduled tasks, and to identify if a task should be scheduled.
|
---|
9 | */
|
---|
10 | function findAndClearHandle(handle: number): boolean {
|
---|
11 | if (handle in activeHandles) {
|
---|
12 | delete activeHandles[handle];
|
---|
13 | return true;
|
---|
14 | }
|
---|
15 | return false;
|
---|
16 | }
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * Helper functions to schedule and unschedule microtasks.
|
---|
20 | */
|
---|
21 | export const Immediate = {
|
---|
22 | setImmediate(cb: () => void): number {
|
---|
23 | const handle = nextHandle++;
|
---|
24 | activeHandles[handle] = true;
|
---|
25 | RESOLVED.then(() => findAndClearHandle(handle) && cb());
|
---|
26 | return handle;
|
---|
27 | },
|
---|
28 |
|
---|
29 | clearImmediate(handle: number): void {
|
---|
30 | findAndClearHandle(handle);
|
---|
31 | },
|
---|
32 | };
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Used for internal testing purposes only. Do not export from library.
|
---|
36 | */
|
---|
37 | export const TestTools = {
|
---|
38 | pending() {
|
---|
39 | return Object.keys(activeHandles).length;
|
---|
40 | }
|
---|
41 | };
|
---|
Note:
See
TracBrowser
for help on using the repository browser.