GraftJS / jschan

JavaScript port of libchan based around streams
MIT License
157 stars 9 forks source link

Automatic object stream piping #26

Open mcollina opened 9 years ago

mcollina commented 9 years ago

Currently, we cannot pass a standard objectMode stream inside a channel with automated piping: we need to create a new channel and pipe.

Is it sound to use that pattern? Plus, solving this issue might be more tricky, because Transform stream are not supported by the automated piping natively, you will need to flag them with something like jschanReadable or jschanWritable.

Any opinions on this?

davidmarkclements commented 9 years ago

Hey @mcollina can you elaborate with some (pseudo) code?

mcollina commented 9 years ago

So you might want to use a https://www.npmjs.org/package/from2 fro generating some events to pop to a client.

Right now you have to do:

session.on('channel', function(chan) {
  chan.on('data', function(msg) {
    var chan = chan.WriteChannel()
    from2(...).pipe(chan)
    msg.returnChannel.end({
      more: chan,
      error: false
    })
  })
})

Using graft syntax:

through.obj(function(msg, enc, done) {
    var chan = msg._channel.WriteChannel()
    from2(...).pipe(chan)
    msg.returnChannel.end({
      more: chan,
      error: false
    })
})

However, with this you might want to use:

through.obj(function(msg, enc, done) {
    msg.returnChannel.end({
      more: from2(...),
      error: false
    })
})

and it gets automatically piped.

This would also greatly help seneca integration, as at that point it could just forget about channels.

The only point will be Transform streams, which must get an isReadChannel or isWriteChannel property hooked up.

davidmarkclements commented 9 years ago

could we abstract the pain by doing the channel switching under the hood in response to a pipe event?

AdrianRossouw commented 9 years ago

deja vu? #22

mcollina commented 9 years ago

@AdrianRossouw this is the missing bit for #22. However it requires some changes to msgpack5.

@davidmarkclements for sure! This requires some changes to msgpack5, if you want to help we can discuss the details there.