| 49 | this.flushTimeouts(); |
| 50 | } |
| 51 | private flushTimeouts() { |
| 52 | if (this._flushing) { |
| 53 | this._flushingInvalidated = true; |
| 54 | return; |
| 55 | } |
| 56 | this._flushing = true; |
| 57 | |
| 58 | const sorted = [...this.timeouts].sort( |
| 59 | ([_idA, timeoutA], [_idB, timeoutB]) => { |
| 60 | const endA = timeoutA.start + timeoutA.timeout; |
| 61 | const endB = timeoutB.start + timeoutB.timeout; |
| 62 | return endB > endA ? -1 : 1; |
| 63 | } |
| 64 | ); |
| 65 | |
| 66 | for (const [id, timeout] of sorted) { |
| 67 | if (this._flushingInvalidated) { |
| 68 | this._flushingInvalidated = false; |
| 69 | this._flushing = false; |
| 70 | this.flushTimeouts(); |
| 71 | return; |
| 72 | } |
| 73 | if (this.now() - timeout.start >= timeout.timeout) { |
| 74 | this.timeouts.delete(id); |
| 75 | timeout.fn.call(null); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | this._flushing = false; |
| 80 | } |
| 81 | public increment(ms: number): void { |
| 82 | this._now += ms; |
| 83 | this.flushTimeouts(); |