tj / axon

message-oriented socket library for node.js heavily inspired by zeromq
MIT License
1.49k stars 155 forks source link

argument handling #79

Closed gjohnson closed 11 years ago

gjohnson commented 11 years ago

As I am moving things around a tid-bit, I noticed it would be nice to centralize argument handling, so we don't have this scattered everywhere lol:

if (Array.isArray(msg)) {
  args = msg;
} else {
  for (var i = 0; i < arguments.length; ++i) {
    args[i] = arguments[i];
  }
}

Perhaps have a utils function like this... Orrrr maybe we should be a little more strict about send() for multipart and say that if you want multipart you need to use an array... even though I personally like doing send(a, b, c) over send([a, b, c]).

Meh?

tj commented 11 years ago

we have both right now right? (cant remember) I like send(a,b,c) too but both are nice

gjohnson commented 11 years ago

Yeah we do have both, not even sure why lol. I would bet that anyone using an array is probably just going to use json and do something like: send({ stuff: [...] }). Perhaps we should only support variadic style multipart, that way we could actually be optimized for single messages and clean up the code a little?

http://jsperf.com/more

tj commented 11 years ago

i think passing an array still makes sense in case you have to built it up, that's easier than applying to the function. Probably wouldn't hurt to chuck it in a util function but those are all way up in the millions ops/s so it probably doesn't affect us much

brycebaril commented 11 years ago

FYI this tripped me up where I was using send(data) and my data just happened to be an array where I didn't want it to be treated like multipart and had to read the code to see what was going on.

Like the comment above I essentially just changed it to send({stuff: data}) but maybe this is an opportunity for better documentation, or even a different method for explicit multipart send.

tj commented 11 years ago

@brycebaril agreed with docs, I think the array as multipart is still useful, mostly because splatting with js sucks but it is more efficient (</overkill>) haha, something we can think about changing though for sure