@@ -17,6 +17,7 @@ const { |
17 | 17 | FunctionPrototypeCall, |
18 | 18 | ObjectSetPrototypeOf, |
19 | 19 | PromisePrototypeThen, |
| 20 | + PromiseReject, |
20 | 21 | PromiseResolve, |
21 | 22 | PromiseWithResolvers, |
22 | 23 | Symbol, |
@@ -414,38 +415,94 @@ function yieldNormalizationAbortable(source, context) { |
414 | 415 | } |
415 | 416 | } |
416 | 417 |
|
| 418 | + // Settle a pending next() with a result of the source's next(). Throws |
| 419 | + // (to be handled by the caller) like the checks it replaces would. |
| 420 | + function toIterResult(result) { |
| 421 | + throwIfNormalizationCancelled(context); |
| 422 | + if ((typeof result !== 'object' && typeof result !== 'function') || |
| 423 | + result === null) { |
| 424 | + throw new ERR_INVALID_RETURN_VALUE( |
| 425 | + 'an object', 'iterator.next()', result); |
| 426 | + } |
| 427 | + if (result.done) { |
| 428 | + reading = false; |
| 429 | + throwIfNormalizationCancelled(context); |
| 430 | + completed = true; |
| 431 | + closed = true; |
| 432 | + return new IterResult(true, result.value); |
| 433 | + } |
| 434 | + const value = result.value; |
| 435 | + reading = false; |
| 436 | + throwIfNormalizationCancelled(context); |
| 437 | + return new IterResult(false, value); |
| 438 | + } |
| 439 | + |
| 440 | + // Reject a pending next(), closing the source first if the |
| 441 | + // normalization has been cancelled. |
| 442 | + function rejectNext(reject, error) { |
| 443 | + if (context.cancelled) { |
| 444 | + PromisePrototypeThen(closeSource(true), () => { |
| 445 | + reading = false; |
| 446 | + reject(error); |
| 447 | + }); |
| 448 | + return; |
| 449 | + } |
| 450 | + reading = false; |
| 451 | + reject(error); |
| 452 | + } |
| 453 | + |
417 | 454 | return ObjectSetPrototypeOf({ |
418 | | - async next() { |
| 455 | + // next() runs for every value of the source, so instead of being an |
| 456 | + // async function awaiting waitForNormalization(), it waits for the |
| 457 | + // source with a single promise, which a cancellation rejects |
| 458 | + // directly. |
| 459 | + next() { |
419 | 460 | if (completed) { |
420 | | - return new IterResult(true, undefined); |
| 461 | + return PromiseResolve(new IterResult(true, undefined)); |
421 | 462 | } |
422 | | - throwIfNormalizationCancelled(context); |
| 463 | + if (context.cancelled) return PromiseReject(context.reason); |
423 | 464 | reading = true; |
424 | 465 |
|
| 466 | + const { promise, resolve, reject } = PromiseWithResolvers(); |
| 467 | + let next; |
425 | 468 | try { |
426 | | - const next = FunctionPrototypeCall(nextMethod, iterator); |
427 | | - const result = await waitForNormalization(next, context); |
428 | | - if ((typeof result !== 'object' && typeof result !== 'function') || |
429 | | - result === null) { |
430 | | - throw new ERR_INVALID_RETURN_VALUE( |
431 | | - 'an object', 'iterator.next()', result); |
432 | | - } |
433 | | - if (result.done) { |
434 | | - reading = false; |
435 | | - throwIfNormalizationCancelled(context); |
436 | | - completed = true; |
437 | | - closed = true; |
438 | | - return new IterResult(true, result.value); |
439 | | - } |
440 | | - const value = result.value; |
441 | | - reading = false; |
442 | | - throwIfNormalizationCancelled(context); |
443 | | - return new IterResult(false, value); |
| 469 | + next = FunctionPrototypeCall(nextMethod, iterator); |
444 | 470 | } catch (error) { |
445 | | - if (context.cancelled) await closeSource(true); |
446 | | - reading = false; |
447 | | - throw error; |
| 471 | + rejectNext(reject, error); |
| 472 | + return promise; |
448 | 473 | } |
| 474 | + // The first of the source's result and a cancellation settles |
| 475 | + // next(); whichever comes later is ignored. |
| 476 | + let settled = false; |
| 477 | + const onCancel = () => { |
| 478 | + if (settled) return; |
| 479 | + settled = true; |
| 480 | + if (context.resolve === onCancel) context.resolve = null; |
| 481 | + rejectNext(reject, context.reason); |
| 482 | + }; |
| 483 | + context.resolve = onCancel; |
| 484 | + PromisePrototypeThen( |
| 485 | + PromiseResolve(next), |
| 486 | + (result) => { |
| 487 | + if (settled) return; |
| 488 | + settled = true; |
| 489 | + if (context.resolve === onCancel) context.resolve = null; |
| 490 | + let iterResult; |
| 491 | + try { |
| 492 | + iterResult = toIterResult(result); |
| 493 | + } catch (error) { |
| 494 | + rejectNext(reject, error); |
| 495 | + return; |
| 496 | + } |
| 497 | + resolve(iterResult); |
| 498 | + }, |
| 499 | + (error) => { |
| 500 | + if (settled) return; |
| 501 | + settled = true; |
| 502 | + if (context.resolve === onCancel) context.resolve = null; |
| 503 | + rejectNext(reject, error); |
| 504 | + }); |
| 505 | + return promise; |
449 | 506 | }, |
450 | 507 | async return(value) { |
451 | 508 | await closeSource( |
|