proposal for new syntax #21

Open
juliangruber opened this Issue Jan 31, 2013 · 8 comments

Projects

None yet

3 participants

@juliangruber
Contributor

Instead of the large conditionals in the connection handler, like

md.on('connection', function (stream) {
  if (stream.meta == 'foo') { /* ... */ }
  else if (stream.meta == 'bar') { /* ... */ }
})

It would be nice to have this syntax:

md.on('foo', function (fooStream) { /* ... */ })
md.on('bar', function (barStream) { /* ... */ })

This way we have shorter code and named streams.

I tried implementing it but it only worked when I let it stop emitting 'connection'-Events.

What do you think?

@dominictarr
Owner

well, I often make meta an arbitary object, so it would need to continue to support the 'connection' event.
can you post the code that only worked when you removed 'connection'?

I guess this is okay for string events who's name does not have a special meaning - like 'newListener' or 'error' another event that streams use, etc.

@juliangruber
Contributor

in line 38:

var stream = createStream(id, data[1].meta, data[1].opts)
md.emit(stream.meta, stream)
@dominictarr
Owner

no, I don't understand why it broke, show me the broken code, enough to run.

it may be: if you don't pass have a connection listener, it calls stream.error('no handler for ' + stream.meta)

something like that...

@juliangruber
Contributor

It breaks when you have

var stream = createStream(id, data[1].meta, data[1].opts)
md.emit(stream.meta, stream)
md.emit('connection', stream)

or

var stream = createStream(id, data[1].meta, data[1].opts)
md.emit(stream.meta, stream)
md.emit('connection', createStream(id, data[1].meta, data[1].opts))

In both cases I can't get a connection

@juliangruber
Contributor

With the additional meta:

mdm.on('foobar', function (stream, meta) {})

That's more opinionated but imo nicer.

I started needing objects as meta too, so that'd be necessary also. I'll upload my modifications and a test later...

@dominictarr
Owner

Well, you can build this ontop of mux-demux if you want...

But I'm reluctant to change mux-demux at this point,

Intending to rewrite it.

@kumavis
kumavis commented Jan 20, 2014

take a look at https://github.com/AndreasMadsen/nspoint (Note: I couldn't actually get it to work.)
Very similar, except missing a few features. Also, instead of listening for a new substream on connection, you just expect your namespaces to be known on both sides, or roll your own channel for sending namespaces via a known namespace.
I liked the simple readme demo.

@dominictarr
Owner

@kumavis sure, well you could easily build that with a lib on top of mux-demux.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment