Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Clean up message arguments #80

Merged
merged 1 commit into from

2 participants

Garrett Johnson TJ Holowaychuk
Garrett Johnson
Collaborator

Since we actually handle the arrays in Socket.prototype.pack we can just pass it right along.

See inline comments.

Closes #79.

Garrett Johnson Clean up argument handling
Closes #79.
34a5d69
Garrett Johnson gjohnson commented on the diff
lib/sockets/req.js
@@ -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];
- }
- }
Garrett Johnson Collaborator

We still need to make sure we actually have an array here because of the pushing/unshift to grab the callback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Garrett Johnson gjohnson commented on the diff
lib/utils.js
@@ -0,0 +1,19 @@
+
+/**
+ * Slice helper.
+ *
+ * @api private
+ * @param {Arguments} args
+ * @return {Array}
+ */
+
Garrett Johnson Collaborator

Not really a true slice() without the offset, but whatever.... can change the name if we need to.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
TJ Holowaychuk
Owner
tj commented

LGTM! thanks man

TJ Holowaychuk tj merged commit 8f47de7 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 30, 2012
  1. Clean up argument handling

    Garrett Johnson authored
    Closes #79.
This page is out of date. Refresh to see the latest.
19 lib/plugins/round-robin.js
View
@@ -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);
}
14 lib/sockets/pub.js
View
@@ -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;
};
13 lib/sockets/req.js
View
@@ -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];
- }
- }
Garrett Johnson Collaborator

We still need to make sure we actually have an array here because of the pushing/unshift to grab the callback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ , args = Array.isArray(msg)
+ ? msg
+ : slice(arguments);
if (sock) {
if ('function' == typeof args[args.length - 1]) {
19 lib/utils.js
View
@@ -0,0 +1,19 @@
+
+/**
+ * Slice helper.
+ *
+ * @api private
+ * @param {Arguments} args
+ * @return {Array}
+ */
+
Garrett Johnson Collaborator

Not really a true slice() without the offset, but whatever.... can change the name if we need to.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+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;
+};
Something went wrong with that request. Please try again.