Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Clean up argument handling

Closes #79.
  • Loading branch information...
commit 34a5d69f8d92ca2b6d3006178da0ae3faa0e784e 1 parent 6f6f992
Garrett Johnson authored
View
19 lib/plugins/round-robin.js
@@ -1,5 +1,11 @@
/**
+ * Deps.
+ */
+
+var slice = require('../utils').slice;
+
+/**
* Round-robin plugin.
*
* Provides a `send` method which will
@@ -34,19 +40,12 @@ module.exports = function(options){
sock.send = function(msg){
var socks = this.socks
, len = socks.length
- , sock = socks[n++ % len]
- , args = [];
+ , sock = socks[n++ % len];
- if (Array.isArray(msg)) {
- args = msg;
- } else {
- for (var i = 0; i < arguments.length; ++i) {
- args[i] = arguments[i];
- }
- }
+ if (arguments.length > 1) msg = slice(arguments);
if (sock && sock.writable) {
- sock.write(this.pack(args));
+ sock.write(this.pack(msg));
} else {
fallback(msg);
}
View
14 lib/sockets/pub.js
@@ -4,7 +4,8 @@
*/
var Socket = require('./sock')
- , Batch = require('../batch');
+ , Batch = require('../batch')
+ , slice = require('../utils').slice;
/**
* Expose `PubSocket`.
@@ -73,15 +74,8 @@ PubSocket.prototype.flushBatch = function(){
PubSocket.prototype.send = function(msg){
if (++this.n == this.get('batch max')) return this.flushBatch();
- if (Array.isArray(msg)) {
- this.batch.add(this.pack(msg));
- } else {
- var args = [];
- for (var i = 0; i < arguments.length; ++i) {
- args[i] = arguments[i];
- }
- this.batch.add(this.pack(args));
- }
+ if (arguments.length > 1) msg = slice(arguments);
+ this.batch.add(this.pack(msg));
return this;
};
View
13 lib/sockets/req.js
@@ -5,6 +5,7 @@
var Socket = require('./sock')
, queue = require('../plugins/queue')
+ , slice = require('../utils').slice
, debug = require('debug')('axon:req');
/**
@@ -78,15 +79,9 @@ ReqSocket.prototype.send = function(msg){
var socks = this.socks
, len = socks.length
, sock = socks[this.n++ % len]
- , args = [];
-
- if (Array.isArray(msg)) {
- args = msg;
- } else {
- for (var i = 0; i < arguments.length; ++i) {
- args[i] = arguments[i];
- }
- }
+ , args = Array.isArray(msg)
+ ? msg
+ : slice(arguments);
if (sock) {
if ('function' == typeof args[args.length - 1]) {
View
19 lib/utils.js
@@ -0,0 +1,19 @@
+
+/**
+ * Slice helper.
+ *
+ * @api private
+ * @param {Arguments} args
+ * @return {Array}
+ */
+
+exports.slice = function(args){
+ var len = args.length
+ , ret = new Array(len);
+
+ for (var i = 0; i < len; i++) {
+ ret[i] = args[i];
+ }
+
+ return ret;
+};
Please sign in to comment.
Something went wrong with that request. Please try again.