2 const RESOLVED = (() => Promise.resolve())();
3 const activeHandles: { [key: number]: any } = {};
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.
10 function findAndClearHandle(handle: number): boolean {
11 if (handle in activeHandles) {
12 delete activeHandles[handle];
19 * Helper functions to schedule and unschedule microtasks.
21 export const Immediate = {
22 setImmediate(cb: () => void): number {
23 const handle = nextHandle++;
24 activeHandles[handle] = true;
25 RESOLVED.then(() => findAndClearHandle(handle) && cb());
29 clearImmediate(handle: number): void {
30 findAndClearHandle(handle);
35 * Used for internal testing purposes only. Do not export from library.
37 export const TestTools = {
39 return Object.keys(activeHandles).length;