* If one task in queue failed - reject all pending tasks. * * We start rejecting in reverse order (starting from last added). * * eg. if you add topic, then message - if topic fails > we first reject (and undo) adding message and then topic. * * In a way like CMD+Z undo works - you
(
failedTask: Task<unknown>,
failedTaskError: unknown
)
| 41 | * In a way like CMD+Z undo works - you first undo last actions. |
| 42 | */ |
| 43 | function rejectAllAndReset( |
| 44 | failedTask: Task<unknown>, |
| 45 | failedTaskError: unknown |
| 46 | ) { |
| 47 | const tasksFromNewest = Array.from(queue).reverse(); |
| 48 | |
| 49 | // Run in action so all fails are flushed to UI at once. |
| 50 | runInAction(() => { |
| 51 | for (const taskToReject of tasksFromNewest) { |
| 52 | const taskPromise = taskFlushedPromisesMap.get(taskToReject); |
| 53 | assert(taskPromise, "Incorrect state - no task promise"); |
| 54 | |
| 55 | queue.delete(taskToReject); |
| 56 | |
| 57 | if (taskToReject === failedTask) { |
| 58 | taskPromise.reject(failedTaskError); |
| 59 | } else { |
| 60 | taskPromise.reject(new Error(`Previous task in queue failed`)); |
| 61 | } |
| 62 | } |
| 63 | }); |
| 64 | } |
| 65 | |
| 66 | async function flushNext() { |
| 67 | if (isFlushing) return; |