@@ -19,7 +19,6 @@ const { |
19 | 19 | PromisePrototypeThen, |
20 | 20 | PromiseResolve, |
21 | 21 | PromiseWithResolvers, |
22 | | - SafePromiseRace, |
23 | 22 | Symbol, |
24 | 23 | SymbolAsyncIterator, |
25 | 24 | SymbolIterator, |
@@ -54,6 +53,7 @@ const { |
54 | 53 |
|
55 | 54 | const { |
56 | 55 | IterResult, |
| 56 | + kResolvedPromise, |
57 | 57 | getProtocolMethod, |
58 | 58 | toUint8Array, |
59 | 59 | } = require('internal/streams/iter/utils'); |
@@ -62,7 +62,6 @@ const { |
62 | 62 | // Bounds peak memory when arrays flow through transforms, which must |
63 | 63 | // allocate output for the entire batch at once. |
64 | 64 | const FROM_BATCH_SIZE = 128; |
65 | | -const kNormalizationCancelled = Symbol('kNormalizationCancelled'); |
66 | 65 | // Yielded by normalizeAsyncValue() (only when `emitFlush` is true) right |
67 | 66 | // before it waits on a promise or on a nested async iterable. Callers that |
68 | 67 | // batch chunks yield whatever they have collected so far, so that chunks that |
@@ -83,31 +82,56 @@ function cancelNormalization(context, reason, suppressCleanup = false) { |
83 | 82 | context.cancelled = true; |
84 | 83 | context.reason = reason; |
85 | 84 | context.suppressCleanup = suppressCleanup; |
86 | | - context.resolve?.(kNormalizationCancelled); |
| 85 | + context.resolve?.(); |
87 | 86 | } |
88 | 87 |
|
89 | 88 | function throwIfNormalizationCancelled(context) { |
90 | 89 | if (context?.cancelled) throw context.reason; |
91 | 90 | } |
92 | 91 |
|
93 | | -async function waitForNormalization(value, context) { |
| 92 | +/** |
| 93 | + * Wait for `value`, but stop waiting if the normalization is cancelled. |
| 94 | + * Settles with the first of: |
| 95 | + * - `value` fulfilling: its value, or the cancellation reason if the |
| 96 | + * normalization has been cancelled by then; |
| 97 | + * - `value` rejecting: its rejection reason; |
| 98 | + * - cancellation: the cancellation reason. |
| 99 | + * This runs for every value of a normalized async source, so it uses a |
| 100 | + * single promise and a single reaction on `value` rather than racing |
| 101 | + * promises. |
| 102 | + * @param {any} value |
| 103 | + * @param {object} [context] |
| 104 | + * @returns {Promise<any>|any} |
| 105 | + */ |
| 106 | +function waitForNormalization(value, context) { |
94 | 107 | if (context === undefined) return value; |
95 | | - const { promise, resolve } = PromiseWithResolvers(); |
| 108 | + const { promise, resolve, reject } = PromiseWithResolvers(); |
| 109 | + const onCancel = () => { |
| 110 | + if (context.resolve === onCancel) context.resolve = null; |
| 111 | + reject(context.reason); |
| 112 | + }; |
| 113 | + PromisePrototypeThen( |
| 114 | + PromiseResolve(value), |
| 115 | + (result) => { |
| 116 | + if (context.resolve === onCancel) context.resolve = null; |
| 117 | + if (context.cancelled) { |
| 118 | + reject(context.reason); |
| 119 | + } else { |
| 120 | + resolve(result); |
| 121 | + } |
| 122 | + }, |
| 123 | + (error) => { |
| 124 | + if (context.resolve === onCancel) context.resolve = null; |
| 125 | + reject(error); |
| 126 | + }); |
96 | 127 | if (context.cancelled) { |
97 | | - resolve(kNormalizationCancelled); |
| 128 | + // Already cancelled: a `value` that has already settled still takes |
| 129 | + // precedence, as its reaction above runs first. |
| 130 | + PromisePrototypeThen(kResolvedPromise, onCancel); |
98 | 131 | } else { |
99 | | - context.resolve = resolve; |
100 | | - } |
101 | | - try { |
102 | | - const result = await SafePromiseRace([ |
103 | | - PromiseResolve(value), |
104 | | - promise, |
105 | | - ]); |
106 | | - throwIfNormalizationCancelled(context); |
107 | | - return result; |
108 | | - } finally { |
109 | | - if (context.resolve === resolve) context.resolve = null; |
| 132 | + context.resolve = onCancel; |
110 | 133 | } |
| 134 | + return promise; |
111 | 135 | } |
112 | 136 |
|
113 | 137 | function createNormalizationIterator(createIterator) { |
|