| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -209,22 +209,28 @@ function fromAsyncGen(fn) { | |||
| 209 | 209 | const signal = ac.signal; | |
| 210 | 210 | const value = fn(async function*() { | |
| 211 | 211 | while (true) { | |
| 212 | - const { chunk, done, cb } = await promise; | ||
| 212 | + const _promise = promise; | ||
| 213 | + promise = null; | ||
| 214 | + const { chunk, done, cb } = await _promise; | ||
| 213 | 215 | process.nextTick(cb); | |
| 214 | 216 | if (done) return; | |
| 215 | 217 | if (signal.aborted) throw new AbortError(); | |
| 216 | - yield chunk; | ||
| 217 | 218 | ({ promise, resolve } = createDeferredPromise()); | |
| 219 | + yield chunk; | ||
| 218 | 220 | } | |
| 219 | 221 | }(), { signal }); | |
| 220 | 222 | ||
| 221 | 223 | return { | |
| 222 | 224 | value, | |
| 223 | 225 | write(chunk, encoding, cb) { | |
| 224 | - resolve({ chunk, done: false, cb }); | ||
| 226 | + const _resolve = resolve; | ||
| 227 | + resolve = null; | ||
| 228 | + _resolve({ chunk, done: false, cb }); | ||
| 225 | 229 | }, | |
| 226 | 230 | final(cb) { | |
| 227 | - resolve({ done: true, cb }); | ||
| 231 | + const _resolve = resolve; | ||
| 232 | + resolve = null; | ||
| 233 | + _resolve({ done: true, cb }); | ||
| 228 | 234 | }, | |
| 229 | 235 | destroy(err, cb) { | |
| 230 | 236 | ac.abort(); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,7 +2,7 @@ | |||
| 2 | 2 | ||
| 3 | 3 | const common = require('../common'); | |
| 4 | 4 | const assert = require('assert'); | |
| 5 | - const { Duplex, Readable, Writable } = require('stream'); | ||
| 5 | + const { Duplex, Readable, Writable, pipeline } = require('stream'); | ||
| 6 | 6 | ||
| 7 | 7 | { | |
| 8 | 8 | const d = Duplex.from({ | |
@@ -118,3 +118,29 @@ const { Duplex, Readable, Writable } = require('stream'); | |||
| 118 | 118 | assert.strictEqual(d.readable, false); | |
| 119 | 119 | })); | |
| 120 | 120 | } | |
| 121 | + | ||
| 122 | + { | ||
| 123 | + // https://github.com/nodejs/node/issues/40497 | ||
| 124 | + pipeline( | ||
| 125 | + ['abc\ndef\nghi'], | ||
| 126 | + Duplex.from(async function * (source) { | ||
| 127 | + let rest = ''; | ||
| 128 | + for await (const chunk of source) { | ||
| 129 | + const lines = (rest + chunk.toString()).split('\n'); | ||
| 130 | + rest = lines.pop(); | ||
| 131 | + for (const line of lines) { | ||
| 132 | + yield line; | ||
| 133 | + } | ||
| 134 | + } | ||
| 135 | + yield rest; | ||
| 136 | + }), | ||
| 137 | + async function * (source) { | ||
| 138 | + let ret = ''; | ||
| 139 | + for await (const x of source) { | ||
| 140 | + ret += x; | ||
| 141 | + } | ||
| 142 | + assert.strictEqual(ret, 'abcdefghi'); | ||
| 143 | + }, | ||
| 144 | + common.mustCall(() => {}), | ||
| 145 | + ); | ||
| 146 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments