()
| 51 | |
| 52 | const iterator: AsyncGenerator<Uint8Array> & AsyncDisposable = { |
| 53 | next() { |
| 54 | // First, we consume all unread events |
| 55 | const value = unconsumedEvents.shift(); |
| 56 | if (value != null) { |
| 57 | return Promise.resolve({ value, done: false }); |
| 58 | } |
| 59 | |
| 60 | // Then we error, if an error happened |
| 61 | // This happens one time if at all, because after 'error' |
| 62 | // we stop listening |
| 63 | if (error != null) { |
| 64 | const p = Promise.reject(error); |
| 65 | // Only the first element errors |
| 66 | error = null; |
| 67 | return p; |
| 68 | } |
| 69 | |
| 70 | // If the iterator is finished, resolve to done |
| 71 | if (finished) return closeHandler(); |
| 72 | |
| 73 | // Wait until an event happens |
| 74 | const { promise, resolve, reject } = promiseWithResolvers<IteratorResult<Uint8Array>>(); |
| 75 | unconsumedPromises.push({ resolve, reject }); |
| 76 | return promise; |
| 77 | }, |
| 78 | |
| 79 | return() { |
| 80 | return closeHandler(); |
nothing calls this directly
no test coverage detected