- From: Jimmy Wärting <notifications@github.com>
- Date: Fri, 14 Aug 2020 05:42:59 -0700
- To: whatwg/streams <streams@noreply.github.com>
- Cc: Subscribed <subscribed@noreply.github.com>
- Message-ID: <whatwg/streams/issues/1063/674055631@github.com>
left a ball of roller coster and created more issues thats need to be fixed, don't sound like an easy task to write spec and working cross multiple threads and pipes 😅
so here is rough workaround for those who stubble up on this issue and seeks for a solution that works right now that I myself have adapted into one of my libraries
<details>
<summary>MessageChannel solution</summary>
```js
// this method pass the readableStream into a messageChannel in order to create a more direct communication
var worker = new Worker(`data:text/javascript,onmessage = evt => postMessage(evt.data, [evt.data]);`);
var rs = new ReadableStream({
start(c) {
c.enqueue('a')
c.enqueue('b')
c.enqueue('c')
c.close()
}
})
// send the transferable readableStream
// to a messageChannel port
var mc = new MessageChannel()
mc.port1.postMessage(rs, [rs])
mc.port1.close()
// post the port instead of the transfered stream
worker.postMessage(mc.port2, [mc.port2])
worker.onmessage = evt => {
var port = evt.data
// now you can terminate the worker since you now got a messageChannel port that you can listen to instead.
// the web worker don't have anything to do with the transfered readableStream anymore
worker.terminate()
port.onmessage = evt => {
port.close()
port.onmessage = null
// sucess
evt.data.pipeTo(new WritableStream({
write(x) {
console.log(x)
},
close() {
console.log('closed')
}
}))
}
}
```
<details>
--
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/streams/issues/1063#issuecomment-674055631
Received on Friday, 14 August 2020 12:43:16 UTC