| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent bced143 commit d77788e
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -174,15 +174,15 @@ class ShareImpl { | |||
| 174 | 174 | } | |
| 175 | 175 | ||
| 176 | 176 | // Need to pull from source - check buffer limit | |
| 177 | - const canPull = await self.#waitForBufferSpace(); | ||
| 178 | - if (!canPull) { | ||
| 177 | + const shouldBuffer = await self.#waitForBufferSpace(); | ||
| 178 | + if (shouldBuffer === null) { | ||
| 179 | 179 | state.detached = true; | |
| 180 | 180 | self.#deleteConsumer(state); | |
| 181 | 181 | if (self.#sourceError) throw self.#sourceError; | |
| 182 | 182 | return { __proto__: null, done: true, value: undefined }; | |
| 183 | 183 | } | |
| 184 | 184 | ||
| 185 | - await self.#pullFromSource(); | ||
| 185 | + await self.#pullFromSource(!shouldBuffer); | ||
| 186 | 186 | } | |
| 187 | 187 | }; | |
| 188 | 188 | ||
@@ -263,7 +263,7 @@ class ShareImpl { | |||
| 263 | 263 | async #waitForBufferSpace() { | |
| 264 | 264 | while (this.#buffer.length >= this.#options.highWaterMark) { | |
| 265 | 265 | if (this.#cancelled || this.#sourceError || this.#sourceExhausted) { | |
| 266 | - return !this.#cancelled; | ||
| 266 | + return this.#cancelled ? null : true; | ||
| 267 | 267 | } | |
| 268 | 268 | ||
| 269 | 269 | switch (this.#options.backpressure) { | |
@@ -289,13 +289,13 @@ class ShareImpl { | |||
| 289 | 289 | this.#recomputeMinCursor(); | |
| 290 | 290 | return true; | |
| 291 | 291 | case 'drop-newest': | |
| 292 | - return true; | ||
| 292 | + return false; | ||
| 293 | 293 | } | |
| 294 | 294 | } | |
| 295 | 295 | return true; | |
| 296 | 296 | } | |
| 297 | 297 | ||
| 298 | - #pullFromSource() { | ||
| 298 | + #pullFromSource(discard = false) { | ||
| 299 | 299 | if (this.#sourceExhausted || this.#cancelled) { | |
| 300 | 300 | return PromiseResolve(); | |
| 301 | 301 | } | |
@@ -337,7 +337,7 @@ class ShareImpl { | |||
| 337 | 337 | ||
| 338 | 338 | if (result.done) { | |
| 339 | 339 | this.#sourceExhausted = true; | |
| 340 | - } else { | ||
| 340 | + } else if (!discard) { | ||
| 341 | 341 | this.#buffer.push(result.value); | |
| 342 | 342 | } | |
| 343 | 343 | } catch (error) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -168,10 +168,8 @@ async function testShareDropOldest() { | |||
| 168 | 168 | } | |
| 169 | 169 | ||
| 170 | 170 | async function testShareDropNewest() { | |
| 171 | - // With drop-newest and a stalled consumer, the async path allows the | ||
| 172 | - // buffer to grow beyond highWaterMark (the "drop" applies to the | ||
| 173 | - // backpressure signal, not the buffer contents). Both consumers | ||
| 174 | - // ultimately see all items. | ||
| 171 | + // With drop-newest and a stalled consumer, upstream results are discarded | ||
| 172 | + // once the buffer reaches highWaterMark. | ||
| 175 | 173 | async function* source() { | |
| 176 | 174 | for (let i = 0; i < 4; i++) { | |
| 177 | 175 | yield [new TextEncoder().encode(`${i}`)]; | |
@@ -181,25 +179,24 @@ async function testShareDropNewest() { | |||
| 181 | 179 | const fast = shared.pull(); | |
| 182 | 180 | const slow = shared.pull(); | |
| 183 | 181 | ||
| 184 | - // Fast consumer reads all items | ||
| 182 | + // The fast consumer fills the buffer, then drives the source to completion. | ||
| 185 | 183 | const fastItems = []; | |
| 186 | 184 | for await (const batch of fast) { | |
| 187 | 185 | for (const chunk of batch) { | |
| 188 | 186 | fastItems.push(new TextDecoder().decode(chunk)); | |
| 189 | 187 | } | |
| 190 | 188 | } | |
| 191 | - assert.strictEqual(fastItems.length, 4); | ||
| 189 | + assert.deepStrictEqual(fastItems, ['0', '1']); | ||
| 190 | + assert.strictEqual(shared.bufferSize, 2); | ||
| 192 | 191 | ||
| 193 | - // Slow consumer also sees all items (buffer grew past hwm) | ||
| 192 | + // The stalled consumer sees the buffered items, but not the dropped results. | ||
| 194 | 193 | const slowItems = []; | |
| 195 | 194 | for await (const batch of slow) { | |
| 196 | 195 | for (const chunk of batch) { | |
| 197 | 196 | slowItems.push(new TextDecoder().decode(chunk)); | |
| 198 | 197 | } | |
| 199 | 198 | } | |
| 200 | - assert.strictEqual(slowItems.length, 4); | ||
| 201 | - assert.strictEqual(slowItems[0], '0'); | ||
| 202 | - assert.strictEqual(slowItems[3], '3'); | ||
| 199 | + assert.deepStrictEqual(slowItems, ['0', '1']); | ||
| 203 | 200 | } | |
| 204 | 201 | ||
| 205 | 202 | // ============================================================================= | |
| Back | FazBrowse Home | New Git URL |
0 commit comments