(port)
| 6 | const map = new Map(); |
| 7 | |
| 8 | function createStream(port) { |
| 9 | return new ReadableStream({ |
| 10 | start(controller) { |
| 11 | port.onmessage = ({ data }) => { |
| 12 | if (data === 'end') return controller.close(); |
| 13 | if (data === 'abort') return controller.error('Aborted the download'); |
| 14 | return controller.enqueue(data); |
| 15 | }; |
| 16 | }, |
| 17 | cancel(reason) { |
| 18 | console.log('user aborted', reason); |
| 19 | port.postMessage({ abort: true }); |
| 20 | }, |
| 21 | }); |
| 22 | } |
| 23 | |
| 24 | self.onmessage = (event) => { |
| 25 | if (event.data === 'ping') return; |