| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 215105e commit 271c745
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -463,20 +463,11 @@ function merge(...args) { | |||
| 463 | 463 | if (result.done) { | |
| 464 | 464 | activeCount--; | |
| 465 | 465 | } else { | |
| 466 | - ArrayPrototypePush(ready, result.value); | ||
| 467 | - // Immediately request the next value from this source | ||
| 468 | - // (at most one pending .next() per source) | ||
| 469 | - PromisePrototypeThen( | ||
| 470 | - iterator.next(), | ||
| 471 | - (r) => onSettled(iterator, r), | ||
| 472 | - (err) => { | ||
| 473 | - ArrayPrototypePush(ready, { __proto__: null, error: err }); | ||
| 474 | - if (waitResolve) { | ||
| 475 | - waitResolve(); | ||
| 476 | - waitResolve = null; | ||
| 477 | - } | ||
| 478 | - }, | ||
| 479 | - ); | ||
| 466 | + ArrayPrototypePush(ready, { | ||
| 467 | + __proto__: null, | ||
| 468 | + iterator, | ||
| 469 | + value: result.value, | ||
| 470 | + }); | ||
| 480 | 471 | } | |
| 481 | 472 | if (waitResolve) { | |
| 482 | 473 | waitResolve(); | |
@@ -513,7 +504,18 @@ function merge(...args) { | |||
| 513 | 504 | if (item?.error) { | |
| 514 | 505 | throw item.error; | |
| 515 | 506 | } | |
| 516 | - yield item; | ||
| 507 | + yield item.value; | ||
| 508 | + PromisePrototypeThen( | ||
| 509 | + item.iterator.next(), | ||
| 510 | + (r) => onSettled(item.iterator, r), | ||
| 511 | + (err) => { | ||
| 512 | + ArrayPrototypePush(ready, { __proto__: null, error: err }); | ||
| 513 | + if (waitResolve) { | ||
| 514 | + waitResolve(); | ||
| 515 | + waitResolve = null; | ||
| 516 | + } | ||
| 517 | + }, | ||
| 518 | + ); | ||
| 517 | 519 | } | |
| 518 | 520 | ||
| 519 | 521 | // If sources are still active, wait for the next settlement | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -170,6 +170,32 @@ async function testMergeSignalDuringPendingMultiSourceRead() { | |||
| 170 | 170 | await assert.rejects(next, { name: 'AbortError' }); | |
| 171 | 171 | } | |
| 172 | 172 | ||
| 173 | + async function testMergeDoesNotDrainSourcesWhileIdle() { | ||
| 174 | + function source(n) { | ||
| 175 | + return { | ||
| 176 | + __proto__: null, | ||
| 177 | + pulls: 0, | ||
| 178 | + async *[Symbol.asyncIterator]() { | ||
| 179 | + while (this.pulls < n) { | ||
| 180 | + yield [Buffer.from(`${++this.pulls}`)]; | ||
| 181 | + } | ||
| 182 | + }, | ||
| 183 | + }; | ||
| 184 | + } | ||
| 185 | + | ||
| 186 | + const a = source(5); | ||
| 187 | + const b = source(5); | ||
| 188 | + const iterator = merge(a, b)[Symbol.asyncIterator](); | ||
| 189 | + | ||
| 190 | + await iterator.next(); | ||
| 191 | + await new Promise(setImmediate); | ||
| 192 | + | ||
| 193 | + assert.strictEqual(a.pulls, 1); | ||
| 194 | + assert.strictEqual(b.pulls, 1); | ||
| 195 | + | ||
| 196 | + await iterator.return?.(); | ||
| 197 | + } | ||
| 198 | + | ||
| 173 | 199 | // merge() accepts string sources (normalized via from()) | |
| 174 | 200 | async function testMergeStringSources() { | |
| 175 | 201 | const batches = []; | |
@@ -306,6 +332,7 @@ Promise.all([ | |||
| 306 | 332 | testMergeConsumerBreak(), | |
| 307 | 333 | testMergeSignalMidIteration(), | |
| 308 | 334 | testMergeSignalDuringPendingMultiSourceRead(), | |
| 335 | + testMergeDoesNotDrainSourcesWhileIdle(), | ||
| 309 | 336 | testMergeStringSources(), | |
| 310 | 337 | testMergeObjectLikeSources(), | |
| 311 | 338 | testMergeCleanupErrorOnly(), | |
| Back | FazBrowse Home | New Git URL |
0 commit comments