|
6 | 6 | ArrayBufferPrototypeGetDetached, |
7 | 7 | ArrayBufferPrototypeGetResizable, |
8 | 8 | ArrayPrototypePush, |
9 | | - ArrayPrototypeShift, |
10 | 9 | ArrayPrototypeSlice, |
11 | 10 | FunctionPrototypeCall, |
12 | 11 | ObjectDefineProperty, |
@@ -39,6 +38,7 @@ const { |
39 | 38 |
|
40 | 39 | const { isSharedArrayBuffer, isUint8Array } = require('internal/util/types'); |
41 | 40 | const { kWeakHandler } = require('internal/event_target'); |
| 41 | +const { RingBuffer } = require('internal/streams/iter/ringbuffer'); |
42 | 42 |
|
43 | 43 | const { |
44 | 44 | validateInteger, |
@@ -473,6 +473,15 @@ const kStart = 0; |
473 | 473 | const kActive = 1; |
474 | 474 | const kDone = 2; |
475 | 475 |
|
| 476 | +class QueuedOperation { |
| 477 | + constructor(method, arg, resolve, reject) { |
| 478 | + this.method = method; |
| 479 | + this.arg = arg; |
| 480 | + this.resolve = resolve; |
| 481 | + this.reject = reject; |
| 482 | + } |
| 483 | +} |
| 484 | + |
476 | 485 | /** |
477 | 486 | * Serializes the operations of a hand-written async iterator the way an |
478 | 487 | * async generator queues its requests: an operation started while another |
@@ -481,13 +490,13 @@ const kDone = 2; |
481 | 490 | */ |
482 | 491 | function createOperationQueue() { |
483 | 492 | let busy = false; |
| 493 | + // Created on first use, then kept: queued operations, a RingBuffer of |
| 494 | + // QueuedOperation, since as many can be queued as calls are made. |
484 | 495 | let queue = null; |
485 | 496 |
|
486 | 497 | function drain() { |
487 | | - if (busy || queue === null) return; |
488 | | - const { 0: method, 1: arg, 2: resolve, 3: reject } = |
489 | | - ArrayPrototypeShift(queue); |
490 | | - if (queue.length === 0) queue = null; |
| 498 | + if (busy || queue === null || queue.length === 0) return; |
| 499 | + const { method, arg, resolve, reject } = queue.shift(); |
491 | 500 | busy = true; |
492 | 501 | PromisePrototypeThen(method(arg), resolve, reject); |
493 | 502 | } |
@@ -497,18 +506,20 @@ function createOperationQueue() { |
497 | 506 | // Run `method(arg)`, which returns a promise and must call settled() |
498 | 507 | // once its result is known, now or after the operations before it. |
499 | 508 | run(method, arg) { |
500 | | - if (busy || queue !== null) { |
| 509 | + if (busy || (queue !== null && queue.length !== 0)) { |
501 | 510 | const { promise, resolve, reject } = PromiseWithResolvers(); |
502 | | - queue ??= []; |
503 | | - ArrayPrototypePush(queue, [method, arg, resolve, reject]); |
| 511 | + queue ??= new RingBuffer(); |
| 512 | + queue.push(new QueuedOperation(method, arg, resolve, reject)); |
504 | 513 | return promise; |
505 | 514 | } |
506 | 515 | busy = true; |
507 | 516 | return method(arg); |
508 | 517 | }, |
509 | 518 | settled() { |
510 | 519 | busy = false; |
511 | | - if (queue !== null) PromisePrototypeThen(kResolvedPromise, drain); |
| 520 | + if (queue !== null && queue.length !== 0) { |
| 521 | + PromisePrototypeThen(kResolvedPromise, drain); |
| 522 | + } |
512 | 523 | }, |
513 | 524 | // Like settled(), but start the next queued operation now, as an async |
514 | 525 | // generator does once the await of a yield or return completes. |
|