Mesg-js: implement 'ready' state for streams

proposal

#1

related with:

With the new high level APIs of application and service clients we don’t have a way of notify users about if the stream is ready for serving task/event/result data.

In the example below, we never know if the stream is ready to serve result data. We can only be sure of that when the first result received. But in order to provide a more consistent API and implement new ack feature, we need to know that stream is ready to serve data so we can execute a task afterwards.

const stream = app.listenResult({serviceID: "3989b1e17bd5fc4b7fca846141cb8dc4efa6fc64"})
stream.on('result', (e) => { console.log(e) })
stream.on('end', (e) => { console.log(e) })

I propose two solutions for this:

1- introduce ‘ready’ event:

// note that with this implementation, since we return stream directly with the listenResult() it's possible to cancel the stream easily/anytime by calling stream.cancel()
// with the other option stream cancelation only possible after stream is actually started(ready).
const stream = app.listenResult({serviceID: "3989b1e17bd5fc4b7fca846141cb8dc4efa6fc64"})
stream.on('ready', () => {  })
stream.on('result', (e) => { console.log(e) })
stream.on('end', (e) => { console.log(e) })

2- or we can accept a callback and inject the stream to it when it’s ready.

app.listenResult({serviceID: "3989b1e17bd5fc4b7fca846141cb8dc4efa6fc64"}, (stream) => {
  stream.on('result', (e) => { console.log(e) })
  stream.on('end', (e) => { console.log(e) })
})
  • event
  • callback

0 voters


#2

We decided to change public APIs so this proposal is no longer needed.

However, I implemented the event version with this commit. We’ll keep this commit and other updated version of high level APIs in another branch called generated-and-high-level-apis for future attention.