| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent fce3df7 commit 88a3392
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -686,6 +686,10 @@ function wireBroadcastWriteSignal(entry, signal, resolve, reject, self) { | |||
| 686 | 686 | signal.addEventListener('abort', onAbort, { __proto__: null, once: true }); | |
| 687 | 687 | } | |
| 688 | 688 | ||
| 689 | + function onBroadcastCancel(broadcastImpl, signal) { | ||
| 690 | + onSignalAbort(signal, () => broadcastImpl.cancel(signal.reason)); | ||
| 691 | + } | ||
| 692 | + | ||
| 689 | 693 | // ============================================================================= | |
| 690 | 694 | // Public API | |
| 691 | 695 | // ============================================================================= | |
@@ -720,7 +724,7 @@ function broadcast(options = { __proto__: null }) { | |||
| 720 | 724 | broadcastImpl.setWriter(writer); | |
| 721 | 725 | ||
| 722 | 726 | if (signal) { | |
| 723 | - onSignalAbort(signal, () => broadcastImpl.cancel()); | ||
| 727 | + onBroadcastCancel(broadcastImpl, signal); | ||
| 724 | 728 | } | |
| 725 | 729 | ||
| 726 | 730 | return { __proto__: null, writer, broadcast: broadcastImpl }; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -144,6 +144,7 @@ class ShareImpl { | |||
| 144 | 144 | // cursor must re-pull rather than terminating prematurely. | |
| 145 | 145 | for (;;) { | |
| 146 | 146 | if (state.detached) { | |
| 147 | + if (self.#sourceError) throw self.#sourceError; | ||
| 147 | 148 | return { __proto__: null, done: true, value: undefined }; | |
| 148 | 149 | } | |
| 149 | 150 | ||
@@ -628,6 +629,10 @@ class SyncShareImpl { | |||
| 628 | 629 | } | |
| 629 | 630 | } | |
| 630 | 631 | ||
| 632 | + function onShareCancel(shareImpl, signal) { | ||
| 633 | + onSignalAbort(signal, () => shareImpl.cancel(signal.reason)); | ||
| 634 | + } | ||
| 635 | + | ||
| 631 | 636 | // ============================================================================= | |
| 632 | 637 | // Public API | |
| 633 | 638 | // ============================================================================= | |
@@ -657,7 +662,7 @@ function share(source, options = { __proto__: null }) { | |||
| 657 | 662 | const shareImpl = new ShareImpl(normalized, opts); | |
| 658 | 663 | ||
| 659 | 664 | if (signal) { | |
| 660 | - onSignalAbort(signal, () => shareImpl.cancel()); | ||
| 665 | + onShareCancel(shareImpl, signal); | ||
| 661 | 666 | } | |
| 662 | 667 | ||
| 663 | 668 | return shareImpl; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -70,11 +70,12 @@ async function testAbortSignal() { | |||
| 70 | 70 | ||
| 71 | 71 | ac.abort(); | |
| 72 | 72 | ||
| 73 | - const batches = []; | ||
| 74 | - for await (const batch of consumer) { | ||
| 75 | - batches.push(batch); | ||
| 76 | - } | ||
| 77 | - assert.strictEqual(batches.length, 0); | ||
| 73 | + await assert.rejects(async () => { | ||
| 74 | + // eslint-disable-next-line no-unused-vars | ||
| 75 | + for await (const _ of consumer) { | ||
| 76 | + assert.fail('Should not reach here'); | ||
| 77 | + } | ||
| 78 | + }, { name: 'AbortError' }); | ||
| 78 | 79 | } | |
| 79 | 80 | ||
| 80 | 81 | async function testAlreadyAbortedSignal() { | |
@@ -84,11 +85,12 @@ async function testAlreadyAbortedSignal() { | |||
| 84 | 85 | const { broadcast: bc } = broadcast({ signal: ac.signal }); | |
| 85 | 86 | const consumer = bc.push(); | |
| 86 | 87 | ||
| 87 | - const batches = []; | ||
| 88 | - for await (const batch of consumer) { | ||
| 89 | - batches.push(batch); | ||
| 90 | - } | ||
| 91 | - assert.strictEqual(batches.length, 0); | ||
| 88 | + await assert.rejects(async () => { | ||
| 89 | + // eslint-disable-next-line no-unused-vars | ||
| 90 | + for await (const _ of consumer) { | ||
| 91 | + assert.fail('Should not reach here'); | ||
| 92 | + } | ||
| 93 | + }, { name: 'AbortError' }); | ||
| 92 | 94 | } | |
| 93 | 95 | ||
| 94 | 96 | // ============================================================================= | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -134,16 +134,66 @@ async function testShareCancelWithReason() { | |||
| 134 | 134 | ||
| 135 | 135 | async function testShareAbortSignal() { | |
| 136 | 136 | const ac = new AbortController(); | |
| 137 | - const shared = share(from('data'), { signal: ac.signal }); | ||
| 138 | - const consumer = shared.pull(); | ||
| 137 | + const reason = new Error('share aborted'); | ||
| 138 | + const enc = new TextEncoder(); | ||
| 139 | + async function* source() { | ||
| 140 | + yield [enc.encode('a')]; | ||
| 141 | + yield [enc.encode('b')]; | ||
| 142 | + } | ||
| 143 | + const shared = share(source(), { | ||
| 144 | + highWaterMark: 1, | ||
| 145 | + backpressure: 'block', | ||
| 146 | + signal: ac.signal, | ||
| 147 | + }); | ||
| 148 | + const fast = shared.pull()[Symbol.asyncIterator](); | ||
| 149 | + shared.pull(); | ||
| 150 | + | ||
| 151 | + await fast.next(); | ||
| 152 | + const read = fast.next(); | ||
| 153 | + const rejected = assert.rejects(read, (error) => error === reason); | ||
| 154 | + ac.abort(reason); | ||
| 155 | + | ||
| 156 | + await rejected; | ||
| 157 | + } | ||
| 158 | + | ||
| 159 | + async function testShareAbortSignalWhileSourcePullPending() { | ||
| 160 | + const ac = new AbortController(); | ||
| 161 | + const { | ||
| 162 | + promise: resumePromise, | ||
| 163 | + resolve: resume, | ||
| 164 | + } = Promise.withResolvers(); | ||
| 165 | + const { | ||
| 166 | + promise: sourceStartedPromise, | ||
| 167 | + resolve: sourceStarted, | ||
| 168 | + } = Promise.withResolvers(); | ||
| 169 | + | ||
| 170 | + const source = { | ||
| 171 | + __proto__: null, | ||
| 172 | + [Symbol.asyncIterator]() { | ||
| 173 | + return { | ||
| 174 | + __proto__: null, | ||
| 175 | + async next() { | ||
| 176 | + sourceStarted(); | ||
| 177 | + await resumePromise; | ||
| 178 | + return { __proto__: null, done: true, value: undefined }; | ||
| 179 | + }, | ||
| 180 | + }; | ||
| 181 | + }, | ||
| 182 | + }; | ||
| 183 | + | ||
| 184 | + const shared = share(source, { signal: ac.signal }); | ||
| 185 | + const iter1 = shared.pull()[Symbol.asyncIterator](); | ||
| 186 | + const iter2 = shared.pull()[Symbol.asyncIterator](); | ||
| 187 | + const read1 = iter1.next(); | ||
| 188 | + const read2 = iter2.next(); | ||
| 189 | + const rejected1 = assert.rejects(read1, { name: 'AbortError' }); | ||
| 190 | + const rejected2 = assert.rejects(read2, { name: 'AbortError' }); | ||
| 139 | 191 | ||
| 192 | + await sourceStartedPromise; | ||
| 140 | 193 | ac.abort(); | |
| 194 | + resume(); | ||
| 141 | 195 | ||
| 142 | - const batches = []; | ||
| 143 | - for await (const batch of consumer) { | ||
| 144 | - batches.push(batch); | ||
| 145 | - } | ||
| 146 | - assert.strictEqual(batches.length, 0); | ||
| 196 | + await Promise.all([rejected1, rejected2]); | ||
| 147 | 197 | } | |
| 148 | 198 | ||
| 149 | 199 | async function testShareAlreadyAborted() { | |
@@ -153,11 +203,12 @@ async function testShareAlreadyAborted() { | |||
| 153 | 203 | const shared = share(from('data'), { signal: ac.signal }); | |
| 154 | 204 | const consumer = shared.pull(); | |
| 155 | 205 | ||
| 156 | - const batches = []; | ||
| 157 | - for await (const batch of consumer) { | ||
| 158 | - batches.push(batch); | ||
| 159 | - } | ||
| 160 | - assert.strictEqual(batches.length, 0); | ||
| 206 | + await assert.rejects(async () => { | ||
| 207 | + // eslint-disable-next-line no-unused-vars | ||
| 208 | + for await (const _ of consumer) { | ||
| 209 | + assert.fail('Should not reach here'); | ||
| 210 | + } | ||
| 211 | + }, { name: 'AbortError' }); | ||
| 161 | 212 | } | |
| 162 | 213 | ||
| 163 | 214 | // ============================================================================= | |
@@ -273,6 +324,7 @@ Promise.all([ | |||
| 273 | 324 | testShareCancelMidIteration(), | |
| 274 | 325 | testShareCancelWithReason(), | |
| 275 | 326 | testShareAbortSignal(), | |
| 327 | + testShareAbortSignalWhileSourcePullPending(), | ||
| 276 | 328 | testShareAlreadyAborted(), | |
| 277 | 329 | testShareSourceError(), | |
| 278 | 330 | testShareLateJoiningConsumer(), | |
| Back | FazBrowse Home | New Git URL |
0 commit comments