Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

refactor juggling meta information for api methods

  • Loading branch information...
commit ce31240884b05cb1952967b0e27691091bc81d0c 1 parent 7893578
@timoxley authored
Showing with 19 additions and 16 deletions.
  1. +19 −16 lib/client.js
View
35 lib/client.js
@@ -14,13 +14,11 @@ var Client = function(api) {
Client.prototype = new EventEmitter
-Client.prototype.connect = function(host, callback) {
- if (typeof callback !== 'function') callback = function(){}
- this.host = host
- this.ready = false
- var self = this
- try {
- this.up = upnode(function(remote, connection) {
+// returns a function that takes remote, connection
+// manipulates meta info from dnode into
+// args passed to enode api calls
+var addMetaInformationTo = function(self) {
+ return function(remote, connection) {
var wrappedAPI = {}
_.each(self._api, function(value, name) {
if (typeof value === 'function') {
@@ -31,28 +29,33 @@ Client.prototype.connect = function(host, callback) {
}
var args = Array.prototype.splice.call(arguments, 0) // copy args
var cb = args.pop()
-
var errorFriendlyCallback = function() {
var callbackArgs = self.serializeErrors(arguments)
cb.apply(self, callbackArgs)
}
args.push(errorFriendlyCallback)
args.push(meta)
- value.apply(this, args)
+ value.apply(self, args)
}
} else {
wrappedAPI[name] = value
}
})
-
_(this).extend(wrappedAPI)
- }).connect(host, function(remote, connection) {
-
-
- self.connection = connection
- connection.emit('up', remote);
- })
+ }
+}
+Client.prototype.connect = function(host, callback) {
+ if (typeof callback !== 'function') callback = function(){}
+ this.host = host
+ this.ready = false
+ var self = this
+ var up = upnode(addMetaInformationTo(this))
+ try {
+ self.up = up.connect(host, function(remote, connection) {
+ self.connection = connection
+ connection.emit('up', remote);
+ })
} catch (err) {
self.emit('error', err)
}
Please sign in to comment.
Something went wrong with that request. Please try again.