| 85 | } |
| 86 | |
| 87 | async function testShareCancelMidIteration() { |
| 88 | // Verify that cancel during iteration stops data flow |
| 89 | let sourceReturnCalled = false; |
| 90 | const enc = new TextEncoder(); |
| 91 | async function* gen() { |
| 92 | try { |
| 93 | yield [enc.encode('a')]; |
| 94 | yield [enc.encode('b')]; |
| 95 | yield [enc.encode('c')]; |
| 96 | } finally { |
| 97 | sourceReturnCalled = true; |
| 98 | } |
| 99 | } |
| 100 | const shared = share(gen(), { highWaterMark: 16 }); |
| 101 | const consumer = shared.pull(); |
| 102 | |
| 103 | const items = []; |
| 104 | for await (const batch of consumer) { |
| 105 | for (const chunk of batch) { |
| 106 | items.push(new TextDecoder().decode(chunk)); |
| 107 | } |
| 108 | // Cancel after first batch |
| 109 | shared.cancel(); |
| 110 | } |
| 111 | assert.strictEqual(items.length, 1); |
| 112 | assert.strictEqual(items[0], 'a'); |
| 113 | |
| 114 | await new Promise(setImmediate); |
| 115 | assert.strictEqual(sourceReturnCalled, true); |
| 116 | } |
| 117 | |
| 118 | async function testShareCancelWithReason() { |
| 119 | const shared = share(from('data')); |