@@ -30,6 +30,7 @@ const { |
30 | 30 | fromSync, |
31 | 31 | isAsyncIterable, |
32 | 32 | isSyncIterable, |
| 33 | + kNextSyncBatch, |
33 | 34 | } = require('internal/streams/iter/from'); |
34 | 35 |
|
35 | 36 | const { |
@@ -78,6 +79,15 @@ const kNoShareError = Symbol('kNoShareError'); |
78 | 79 | const kPull = Symbol('kPull'); |
79 | 80 | const kSetFactorySignal = Symbol('kSetFactorySignal'); |
80 | 81 |
|
| 82 | +// Clear `pending` from the pendingNext of the consumer `state` when it |
| 83 | +// settles, unless a next() has been queued behind it. |
| 84 | +function clearPendingNext(state, pending) { |
| 85 | + const onSettled = () => { |
| 86 | + if (state.pendingNext === pending) state.pendingNext = null; |
| 87 | + }; |
| 88 | + PromisePrototypeThen(pending, onSettled, onSettled); |
| 89 | +} |
| 90 | + |
81 | 91 | class ShareImpl { |
82 | 92 | #source; |
83 | 93 | #options; |
@@ -160,6 +170,8 @@ class ShareImpl { |
160 | 170 | waiting: false, |
161 | 171 | // Created by #readAfterPull() on first use. |
162 | 172 | afterPull: undefined, |
| 173 | + // The read #readAfterPull() returned, while it is pending. |
| 174 | + waitingRead: null, |
163 | 175 | // The last pending next() of the consumer, if it is waiting for the |
164 | 176 | // source: the next one waits for it, as next() calls of an async |
165 | 177 | // generator do. |
@@ -186,18 +198,19 @@ class ShareImpl { |
186 | 198 | let result; |
187 | 199 | try { |
188 | 200 | result = self.#tryRead(state); |
| 201 | + // A sync source can be read at once, when there is room. |
| 202 | + while (result === kPull && |
| 203 | + self.#bufferedBytes < self.#options.budget && |
| 204 | + self.#pullSync()) { |
| 205 | + result = self.#tryRead(state); |
| 206 | + } |
189 | 207 | } catch (error) { |
190 | 208 | return PromiseReject(error); |
191 | 209 | } |
192 | 210 | if (result !== kPull) return PromiseResolve(result); |
193 | 211 | return self.#readAsync(state); |
194 | 212 | }; |
195 | | - const clearPending = (pending) => { |
196 | | - const onSettled = () => { |
197 | | - if (state.pendingNext === pending) state.pendingNext = null; |
198 | | - }; |
199 | | - PromisePrototypeThen(pending, onSettled, onSettled); |
200 | | - }; |
| 213 | + const clearPending = (pending) => clearPendingNext(state, pending); |
201 | 214 |
|
202 | 215 | return ObjectSetPrototypeOf({ |
203 | 216 | next() { |
@@ -212,7 +225,9 @@ class ShareImpl { |
212 | 225 | } |
213 | 226 | state.pendingNext = next; |
214 | 227 | markPromiseAsHandled(next); |
215 | | - clearPending(next); |
| 228 | + // A read waiting for a pull of the source clears pendingNext |
| 229 | + // itself (see #readAfterPull()). |
| 230 | + if (next !== state.waitingRead) clearPending(next); |
216 | 231 | return next; |
217 | 232 | }, |
218 | 233 |
|
@@ -344,7 +359,9 @@ class ShareImpl { |
344 | 359 | #readAsync(state) { |
345 | 360 | state.waiting = true; |
346 | 361 | if (this.#bufferedBytes < this.#options.budget) { |
347 | | - return this.#readAfterPull(state); |
| 362 | + const read = this.#readAfterPull(state); |
| 363 | + state.waitingRead = read; |
| 364 | + return read; |
348 | 365 | } |
349 | 366 | return this.#readAsyncLoop(state); |
350 | 367 | } |
@@ -386,14 +403,69 @@ class ShareImpl { |
386 | 403 |
|
387 | 404 | // The common case of #readAsyncLoop(), without an async function: there |
388 | 405 | // is room in the buffer, so read the source and then the consumer's batch. |
| 406 | + // When the read settles with the result afterPull() returns, it is no |
| 407 | + // longer pending: clear it from pendingNext, unless a next() has been |
| 408 | + // queued behind it, without a reaction of its own. |
389 | 409 | #readAfterPull(state) { |
390 | 410 | state.afterPull ??= () => { |
391 | | - const result = this.#tryRead(state); |
392 | | - return result !== kPull ? result : this.#readAsyncLoop(state); |
| 411 | + let result; |
| 412 | + try { |
| 413 | + result = this.#tryRead(state); |
| 414 | + } catch (error) { |
| 415 | + this.#clearWaitingRead(state); |
| 416 | + throw error; |
| 417 | + } |
| 418 | + if (result === kPull) { |
| 419 | + // Still pending: clear it when it settles. |
| 420 | + const read = state.waitingRead; |
| 421 | + state.waitingRead = null; |
| 422 | + if (state.pendingNext === read) clearPendingNext(state, read); |
| 423 | + return this.#readAsyncLoop(state); |
| 424 | + } |
| 425 | + this.#clearWaitingRead(state); |
| 426 | + return result; |
393 | 427 | }; |
394 | 428 | return PromisePrototypeThen(this.#pullFromSource(false), state.afterPull); |
395 | 429 | } |
396 | 430 |
|
| 431 | + #clearWaitingRead(state) { |
| 432 | + if (state.pendingNext === state.waitingRead) state.pendingNext = null; |
| 433 | + state.waitingRead = null; |
| 434 | + } |
| 435 | + |
| 436 | + // Read a batch of the source synchronously, if it can be read so (a sync |
| 437 | + // source normalized by from()) and no read of it is pending. Returns |
| 438 | + // whether the source was read: a batch buffered, or its end or error |
| 439 | + // recorded. |
| 440 | + #pullSync() { |
| 441 | + if (this.#pulling || this.#sourceExhausted || this.#cancelled) { |
| 442 | + return false; |
| 443 | + } |
| 444 | + try { |
| 445 | + const iterator = this.#getSourceIterator(); |
| 446 | + const nextSyncBatch = iterator[kNextSyncBatch]; |
| 447 | + if (nextSyncBatch === undefined) return false; |
| 448 | + const batch = FunctionPrototypeCall(nextSyncBatch, iterator); |
| 449 | + if (batch === undefined) return false; |
| 450 | + if (batch === null) { |
| 451 | + this.#sourceExhausted = true; |
| 452 | + } else { |
| 453 | + this.#bufferBatch(batch); |
| 454 | + } |
| 455 | + } catch (error) { |
| 456 | + this.#sourceError = error; |
| 457 | + this.#sourceExhausted = true; |
| 458 | + } |
| 459 | + if (this.#sourceExhausted && this.#consumers.size === 0) { |
| 460 | + this.#cleanupFactorySignal(); |
| 461 | + } |
| 462 | + for (let i = 0; i < this.#pullWaiters.length; i++) { |
| 463 | + this.#pullWaiters[i](); |
| 464 | + } |
| 465 | + this.#pullWaiters = []; |
| 466 | + return true; |
| 467 | + } |
| 468 | + |
397 | 469 | async #waitForBufferSpace() { |
398 | 470 | while (this.#bufferedBytes >= this.#options.budget) { |
399 | 471 | if (this.#cancelled || |
@@ -461,29 +533,7 @@ class ShareImpl { |
461 | 533 |
|
462 | 534 | let next; |
463 | 535 | try { |
464 | | - if (!this.#sourceIterator) { |
465 | | - if (isAsyncIterable(this.#source)) { |
466 | | - this.#sourceIterator = |
467 | | - this.#source[SymbolAsyncIterator](); |
468 | | - } else if (isSyncIterable(this.#source)) { |
469 | | - const syncIterator = |
470 | | - this.#source[SymbolIterator](); |
471 | | - this.#sourceIterator = ObjectSetPrototypeOf({ |
472 | | - // Its result is passed to PromiseResolve(). |
473 | | - next() { |
474 | | - return syncIterator.next(); |
475 | | - }, |
476 | | - async return() { |
477 | | - return syncIterator.return?.() ?? |
478 | | - new IterResult(true, undefined); |
479 | | - }, |
480 | | - }, null); |
481 | | - } else { |
482 | | - throw new ERR_INVALID_ARG_TYPE( |
483 | | - 'source', ['AsyncIterable', 'Iterable'], this.#source); |
484 | | - } |
485 | | - } |
486 | | - next = this.#sourceIterator.next(); |
| 536 | + next = this.#getSourceIterator().next(); |
487 | 537 | } catch (error) { |
488 | 538 | this.#onReadError(error); |
489 | 539 | return promise; |
@@ -493,6 +543,32 @@ class ShareImpl { |
493 | 543 | return promise; |
494 | 544 | } |
495 | 545 |
|
| 546 | + #getSourceIterator() { |
| 547 | + if (!this.#sourceIterator) { |
| 548 | + if (isAsyncIterable(this.#source)) { |
| 549 | + this.#sourceIterator = |
| 550 | + this.#source[SymbolAsyncIterator](); |
| 551 | + } else if (isSyncIterable(this.#source)) { |
| 552 | + const syncIterator = |
| 553 | + this.#source[SymbolIterator](); |
| 554 | + this.#sourceIterator = ObjectSetPrototypeOf({ |
| 555 | + // Its result is passed to PromiseResolve(). |
| 556 | + next() { |
| 557 | + return syncIterator.next(); |
| 558 | + }, |
| 559 | + async return() { |
| 560 | + return syncIterator.return?.() ?? |
| 561 | + new IterResult(true, undefined); |
| 562 | + }, |
| 563 | + }, null); |
| 564 | + } else { |
| 565 | + throw new ERR_INVALID_ARG_TYPE( |
| 566 | + 'source', ['AsyncIterable', 'Iterable'], this.#source); |
| 567 | + } |
| 568 | + } |
| 569 | + return this.#sourceIterator; |
| 570 | + } |
| 571 | + |
496 | 572 | // The handlers of a read of the source, created once per share. A read |
497 | 573 | // ended by a cancellation is ignored. |
498 | 574 | #onReadResult = (result) => { |
|