()
| 107 | } |
| 108 | |
| 109 | #createRawConsumer() { |
| 110 | const state = { |
| 111 | __proto__: null, |
| 112 | cursor: this.#bufferStart, |
| 113 | resolve: null, |
| 114 | reject: null, |
| 115 | detached: false, |
| 116 | pendingNext: PromiseResolve(), |
| 117 | }; |
| 118 | |
| 119 | this.#consumers.add(state); |
| 120 | if (this.#consumers.size === 1) { |
| 121 | this.#cachedMinCursor = state.cursor; |
| 122 | this.#cachedMinCursorConsumers = 1; |
| 123 | } else if (state.cursor === this.#cachedMinCursor) { |
| 124 | this.#cachedMinCursorConsumers++; |
| 125 | } else { |
| 126 | this.#recomputeMinCursor(); |
| 127 | } |
| 128 | const self = this; |
| 129 | |
| 130 | return { |
| 131 | __proto__: null, |
| 132 | [SymbolAsyncIterator]() { |
| 133 | const getNext = async () => { |
| 134 | if (self.#sourceError) { |
| 135 | state.detached = true; |
| 136 | self.#consumers.delete(state); |
| 137 | throw self.#sourceError; |
| 138 | } |
| 139 | |
| 140 | // Loop until we get data, source is exhausted, or |
| 141 | // consumer is detached. Multiple consumers may be woken |
| 142 | // after a single pull - those that find no data at their |
| 143 | // cursor must re-pull rather than terminating prematurely. |
| 144 | for (;;) { |
| 145 | if (state.detached) { |
| 146 | if (self.#sourceError) throw self.#sourceError; |
| 147 | return { __proto__: null, done: true, value: undefined }; |
| 148 | } |
| 149 | |
| 150 | if (self.#cancelled) { |
| 151 | state.detached = true; |
| 152 | self.#deleteConsumer(state); |
| 153 | return { __proto__: null, done: true, value: undefined }; |
| 154 | } |
| 155 | |
| 156 | // Check if data is available in buffer |
| 157 | const bufferIndex = state.cursor - self.#bufferStart; |
| 158 | if (bufferIndex < self.#buffer.length) { |
| 159 | const chunk = self.#buffer.get(bufferIndex); |
| 160 | const cursor = state.cursor; |
| 161 | state.cursor++; |
| 162 | if (cursor === self.#cachedMinCursor && |
| 163 | --self.#cachedMinCursorConsumers === 0) { |
| 164 | self.#tryTrimBuffer(); |
| 165 | } |
| 166 | return { __proto__: null, done: false, value: chunk }; |
no test coverage detected