()
| 64 | } |
| 65 | |
| 66 | async function flushNext() { |
| 67 | if (isFlushing) return; |
| 68 | const [nextTask] = Array.from(queue); |
| 69 | |
| 70 | if (!nextTask) return; |
| 71 | |
| 72 | isFlushing = true; |
| 73 | |
| 74 | // Get resolvable promise of original call waiting for this task to be resolved. |
| 75 | const flushPromise = taskFlushedPromisesMap.get(nextTask); |
| 76 | |
| 77 | assert(flushPromise, "No flush promise for task added"); |
| 78 | |
| 79 | try { |
| 80 | if (IS_DEV && DEV_ARTIFICIAL_DELAY > 0) { |
| 81 | await wait(DEV_ARTIFICIAL_DELAY); |
| 82 | } |
| 83 | |
| 84 | // Try to execute |
| 85 | const result = await nextTask(); |
| 86 | // Only if successful - delete task for queue to allow next tasks to be executed. |
| 87 | queue.delete(nextTask); |
| 88 | flushPromise.resolve(result); |
| 89 | scheduleNext(); |
| 90 | } catch (error) { |
| 91 | rejectAllAndReset(nextTask, error); |
| 92 | } finally { |
| 93 | isFlushing = false; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | function scheduleNext() { |
| 98 | setTimeout(() => flushNext(), 0); |
no test coverage detected