| 58 | } |
| 59 | |
| 60 | function testShareSyncCancelMidIteration() { |
| 61 | // Verify cancel during iteration stops data flow and cleans up |
| 62 | const enc = new TextEncoder(); |
| 63 | let sourceReturnCalled = false; |
| 64 | function* gen() { |
| 65 | try { |
| 66 | yield [enc.encode('a')]; |
| 67 | yield [enc.encode('b')]; |
| 68 | yield [enc.encode('c')]; |
| 69 | } finally { |
| 70 | sourceReturnCalled = true; |
| 71 | } |
| 72 | } |
| 73 | const shared = shareSync(gen(), { highWaterMark: 16 }); |
| 74 | const consumer = shared.pull(); |
| 75 | |
| 76 | const items = []; |
| 77 | for (const batch of consumer) { |
| 78 | for (const chunk of batch) { |
| 79 | items.push(new TextDecoder().decode(chunk)); |
| 80 | } |
| 81 | // Cancel after first batch |
| 82 | shared.cancel(); |
| 83 | } |
| 84 | assert.strictEqual(items.length, 1); |
| 85 | assert.strictEqual(items[0], 'a'); |
| 86 | assert.strictEqual(sourceReturnCalled, true); |
| 87 | } |
| 88 | |
| 89 | function testShareSyncCancelWithReason() { |
| 90 | // When cancel(reason) is called, a consumer that hasn't started |