ba296613f283e0396259697f9882ca7f47a013a6
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / rxjs / src / internal / util / Immediate.ts
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 };