GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — stream: make stream/iter from() cancellation waits cheaper · nodejs/node@3b6a489

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib/internal/streams/iter
    • from.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/internal/streams/iter/from.js‎

Copy file name to clipboardExpand all lines: lib/internal/streams/iter/from.js
+41-17Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ const {
1919
PromisePrototypeThen,
2020
PromiseResolve,
2121
PromiseWithResolvers,
22-
SafePromiseRace,
2322
Symbol,
2423
SymbolAsyncIterator,
2524
SymbolIterator,
@@ -54,6 +53,7 @@ const {
5453

5554
const {
5655
IterResult,
56+
kResolvedPromise,
5757
getProtocolMethod,
5858
toUint8Array,
5959
} = require('internal/streams/iter/utils');
@@ -62,7 +62,6 @@ const {
6262
// Bounds peak memory when arrays flow through transforms, which must
6363
// allocate output for the entire batch at once.
6464
const FROM_BATCH_SIZE = 128;
65-
const kNormalizationCancelled = Symbol('kNormalizationCancelled');
6665
// Yielded by normalizeAsyncValue() (only when `emitFlush` is true) right
6766
// before it waits on a promise or on a nested async iterable. Callers that
6867
// batch chunks yield whatever they have collected so far, so that chunks that
@@ -83,31 +82,56 @@ function cancelNormalization(context, reason, suppressCleanup = false) {
8382
context.cancelled = true;
8483
context.reason = reason;
8584
context.suppressCleanup = suppressCleanup;
86-
context.resolve?.(kNormalizationCancelled);
85+
context.resolve?.();
8786
}
8887

8988
function throwIfNormalizationCancelled(context) {
9089
if (context?.cancelled) throw context.reason;
9190
}
9291

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) {
94107
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+
});
96127
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);
98131
} 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;
110133
}
134+
return promise;
111135
}
112136

113137
function createNormalizationIterator(createIterator) {

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.