rxjs/webSocket should support deserializer return Promise
Feature Request
rxjs/webSock should support deserializer return Promise
Is your feature request related to a problem? Please describe.
import { webSocket } from 'rxjs/webSocket'
const api = (options) => {
const subject = webSocket(options);
return {
subscribe: (params: any) => {
const observableA = subject.multiplex(
() => params,
() => ({ close: true }),
(message: any) => {
return message.requestId === params.requestId
}
);
return observableA
}
}
}
const ws = api({
url: 'ws://127.0.0.1:50055/ws/v2',
protocol: 'json',
binaryType: 'blob',
deserializer: ({ data }) => {
return new Promise((res, rej) => {
const reader = new FileReader()
reader.readAsText(data)
reader.onload = () => {
res(JSON.parse(reader.result))
}
})
}
})
ws.subscribe({requestId: '1', data:{}})
.subscribe((res) => {
console.log('res', res);
})
the deserializer have to return Promise because i have to resolve Blob, but socket.onmessage doesn't support deserializer function to return Promise
So the goal of this is to do some additional processing to the raw stream data as it arrives?
I think I see the issue, if you have some format that requires you do something asynchronous to deserialize it, you need to do it here, especially if you're multiplexing the socket. 🤔
(Sorry it took me so long to find this one, BTW) :(
So the goal of this is to do some additional processing to the raw stream data as it arrives?
I think I see the issue, if you have some format that requires you do something asynchronous to deserialize it, you need to do it here, especially if you're multiplexing the socket. 🤔
Yes 💯
just to clarify: would this allow us to offload json parsing onto a web worker and away from the main UI thread?
@h4de5 you could but that would be pointless. objects are serialised when going between threads even in shared memory buffers.
The point of allowing this function to be a promise is that it allows control to the event loop to be given back. Slower but less blocking.
@benlesh Is this going to be taken on anytime soon? I have very few options when it comes to the synchronous parsing of javascript. reason for async? Breaking up json parsing.