| 96 | * - Async items will be `await`ed |
| 97 | */ |
| 98 | export function flushQueue(): Promise<void> { |
| 99 | return new Promise<void>((resolve, reject) => { |
| 100 | async function drain() { |
| 101 | try { |
| 102 | if (queuedReadTasks.length > 0) { |
| 103 | const readTasks = queuedReadTasks.slice(); |
| 104 | |
| 105 | queuedReadTasks.length = 0; |
| 106 | |
| 107 | let cb: Function; |
| 108 | while ((cb = readTasks.shift())) { |
| 109 | const result = cb(Date.now()); |
| 110 | if (result != null && typeof result.then === 'function') { |
| 111 | await result; |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | if (queuedWriteTasks.length > 0) { |
| 117 | const writeTasks = queuedWriteTasks.slice(); |
| 118 | |
| 119 | queuedWriteTasks.length = 0; |
| 120 | |
| 121 | let cb: Function; |
| 122 | while ((cb = writeTasks.shift())) { |
| 123 | const result = cb(Date.now()); |
| 124 | if (result != null && typeof result.then === 'function') { |
| 125 | await result; |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | if (queuedReadTasks.length + queuedWriteTasks.length > 0) { |
| 131 | process.nextTick(drain); |
| 132 | } else { |
| 133 | resolve(); |
| 134 | } |
| 135 | } catch (e) { |
| 136 | reject(`flushQueue: ${e}`); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | process.nextTick(drain); |
| 141 | }); |
| 142 | } |
| 143 | |
| 144 | export async function flushAll(): Promise<void> { |
| 145 | while (queuedTicks.length + queuedLoadModules.length + queuedWriteTasks.length + queuedReadTasks.length > 0) { |