| 18 | } |
| 19 | |
| 20 | function getCurrentTimeout(timeouts) { |
| 21 | let totalTimeout |
| 22 | // iterate over all timeouts starting from highest values of order |
| 23 | new Map([...timeouts.entries()].sort().reverse()).forEach((timeout, order) => { |
| 24 | if ( |
| 25 | timeout !== undefined && |
| 26 | // when orders >= 0 - timeout value overrides those set with higher order elements |
| 27 | (order >= 0 || |
| 28 | // when `order < 0 && totalTimeout === undefined` - timeout is used when nothing is set by elements with higher order |
| 29 | totalTimeout === undefined || |
| 30 | // when `order < 0` - timeout overrides higher values of timeout or 'no timeout' (totalTimeout === 0) set by elements with higher order |
| 31 | (timeout > 0 && (timeout < totalTimeout || totalTimeout === 0))) |
| 32 | ) { |
| 33 | totalTimeout = timeout |
| 34 | } |
| 35 | }) |
| 36 | return totalTimeout |
| 37 | } |
| 38 | |
| 39 | class TimeoutError extends Error { |
| 40 | constructor(message) { |