Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal for new syntax #21

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

proposal for new syntax #21

juliangruber opened this issue Jan 31, 2013 · 8 comments

Comments

@juliangruber
Copy link
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
Copy link
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
Copy link
Contributor Author

in line 38:

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

@dominictarr
Copy link
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
Copy link
Contributor Author

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
Copy link
Contributor Author

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
Copy link
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
Copy link

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
Copy link
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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants