From 8ffd0b13a3836e57d69ee74510671e16688da8f3 Mon Sep 17 00:00:00 2001 From: wltsmrz Date: Thu, 17 Apr 2014 15:19:08 -0700 Subject: [PATCH] Cleanup --- src/js/ripple/remote.js | 89 +++++++++++++++++----------------- src/js/ripple/request.js | 92 ++++++++++++++++++++---------------- src/js/ripple/transaction.js | 4 +- 3 files changed, 95 insertions(+), 90 deletions(-) diff --git a/src/js/ripple/remote.js b/src/js/ripple/remote.js index ef853827..b069e634 100644 --- a/src/js/ripple/remote.js +++ b/src/js/ripple/remote.js @@ -239,20 +239,20 @@ function Remote(opts, trace) { self.storage.getPendingTransactions(function(err, transactions) { if (err || !Array.isArray(transactions)) return; - var properties = [ - 'submittedIDs', - 'clientID', - 'submitIndex' - ]; - function resubmitTransaction(tx) { var transaction = self.transaction(); transaction.parseJson(tx.tx_json); - properties.forEach(function(prop) { - if (typeof tx[prop] !== 'undefined') { - transaction[prop] = tx[prop]; + + Object.keys(tx).forEach(function(prop) { + switch (prop) { + case 'submittedIDs': + case 'clientID': + case 'submitIndex': + transaction[prop] = tx[prop]; + break; } }); + transaction.submit(); }; @@ -344,16 +344,16 @@ Remote.prototype.addServer = function(opts) { server.on('message', serverMessage); function serverConnect() { + self._connection_count += 1; + if (opts.primary || !self._primary_server) { self._setPrimaryServer(server); } - switch (++self._connection_count) { - case 1: - self._setState('online'); - break; - case self._servers.length: - self.emit('ready'); - break; + if (self._connection_count === 1) { + self._setState('online'); + } + if (self._connection_count === self._servers.length) { + self.emit('ready'); } }; @@ -699,20 +699,18 @@ Remote.prototype.requestLedger = function(ledger, options, callback) { request.message.ledger = ledger; } - var requestFields = [ - 'full', - 'expand', - 'transactions', - 'accounts' - ]; - switch (typeof options) { case 'object': - for (var key in options) { - if (~requestFields.indexOf(key)) { - request.message[key] = true; + Object.keys(options).forEach(function(o) { + switch (o) { + case 'full': + case 'expand': + case 'transactions': + case 'accounts': + request.message[o] = true; + break; } - } + }, options); break; case 'function': @@ -732,7 +730,7 @@ Remote.prototype.requestLedger = function(ledger, options, callback) { return request; }; -// Only for unit testing. +Remote.prototype.requestLedgerClosed = Remote.prototype.requestLedgerHash = function(callback) { //utils.assert(this.trusted); // If not trusted, need to check proof. return new Request(this, 'ledger_closed').callback(callback); @@ -975,26 +973,24 @@ Remote.prototype.requestAccountTx = function(options, callback) { var request = new Request(this, 'account_tx'); - var requestFields = [ - 'account', - 'ledger_index_min', //earliest - 'ledger_index_max', //latest - 'binary', //false - 'count', //false - 'descending', //false - 'offset', //0 - 'limit', + Object.keys(options).forEach(function(o) { + switch (o) { + case 'account': + case 'ledger_index_min': //earliest + case 'ledger_index_max': //latest + case 'binary': //false + case 'count': //false + case 'descending': //false + case 'offset': //0 + case 'limit': - //extended account_tx - 'forward', //false - 'marker' - ]; - - for (var key in options) { - if (~requestFields.indexOf(key)) { - request.message[key] = options[key]; + //extended account_tx + case 'forward': //false + case 'marker': + request.message[o] = this[o]; + break; } - } + }, options); function propertiesFilter(obj, transaction) { var properties = Object.keys(obj); @@ -1259,6 +1255,7 @@ Remote.accountRootRequest = function(type, responseFilter, account, ledger, call } var request = this.requestLedgerEntry('account_root'); + request.accountRoot(account); request.ledgerChoose(ledger); diff --git a/src/js/ripple/request.js b/src/js/ripple/request.js index 6aacb966..c7261bc6 100644 --- a/src/js/ripple/request.js +++ b/src/js/ripple/request.js @@ -7,6 +7,7 @@ var Account = require('./account').Account; var Meta = require('./meta').Meta; var OrderBook = require('./orderbook').OrderBook; var RippleError = require('./rippleerror').RippleError; +var Server = require('./server').Server; // Request events emitted: // 'success' : Request successful. @@ -17,12 +18,9 @@ var RippleError = require('./rippleerror').RippleError; function Request(remote, command) { EventEmitter.call(this); - this.remote = remote; - this.requested = false; - this.message = { - command : command, - id : void(0) - }; + this.remote = remote; + this.requested = false; + this.message = { command: command, id: void(0) }; }; util.inherits(Request, EventEmitter); @@ -37,6 +35,7 @@ Request.prototype.request = function(remote) { if (this.requested) return; this.requested = true; + this.on('error', new Function); this.emit('request', remote); @@ -44,7 +43,7 @@ Request.prototype.request = function(remote) { this.remote._servers.forEach(function(server) { this.setServer(server); this.remote.request(this); - }, this ); + }, this); } else { this.remote.request(this); } @@ -53,37 +52,40 @@ Request.prototype.request = function(remote) { }; Request.prototype.callback = function(callback, successEvent, errorEvent) { - if (callback && typeof callback === 'function') { - var self = this; + var self = this; - function request_success(message) { - callback.call(self, null, message); - } - - function request_error(error) { - if (!(error instanceof RippleError)) { - error = new RippleError(error); - } - callback.call(self, error); - } - - this.once(successEvent || 'success', request_success); - this.once(errorEvent || 'error' , request_error); - this.request(); + if (this.requestsed || typeof callback !== 'function') { + return this; } + function requestSuccess(message) { + callback.call(self, null, message); + }; + + function requestError(error) { + if (!(error instanceof RippleError)) { + error = new RippleError(error); + } + callback.call(self, error); + }; + + this.once(successEvent || 'success', requestSuccess); + this.once(errorEvent || 'error' , requestError); + this.request(); + return this; }; Request.prototype.timeout = function(duration, callback) { var self = this; + function requested() { + self.timeout(duration, callback); + }; + if (!this.requested) { - function requested() { - self.timeout(duration, callback); - } - this.once('request', requested); - return; + // Defer until requested + return this.once('request', requested); } var emit = this.emit; @@ -112,8 +114,11 @@ Request.prototype.setServer = function(server) { case 'object': selected = server; break; + case 'string': + // Find server with hostname string var servers = this.remote._servers; + for (var i=0, s; s=servers[i]; i++) { if (s._host === server) { selected = s; @@ -123,18 +128,19 @@ Request.prototype.setServer = function(server) { break; }; - this.server = selected; + if (selected instanceof Server) { + this.server = selected; + } return this; }; Request.prototype.buildPath = function(build) { - if (this.remote.local_signing) { throw new Error( - '`build_path` is completely ignored when doing local signing as ' + - '`Paths` is a component of the signed blob. The `tx_blob` is signed,' + - 'sealed and delivered, and the txn unmodified after' ); + '`build_path` is completely ignored when doing local signing as ' + + '`Paths` is a component of the signed blob. The `tx_blob` is signed,' + + 'sealed and delivered, and the txn unmodified after' ); } if (build) { @@ -144,6 +150,7 @@ Request.prototype.buildPath = function(build) { // value being `truthy` delete this.message.build_path } + return this; }; @@ -153,6 +160,7 @@ Request.prototype.ledgerChoose = function(current) { } else { this.message.ledger_hash = this.remote._ledger_hash; } + return this; }; @@ -171,8 +179,8 @@ Request.prototype.ledgerIndex = function(ledger_index) { return this; }; -Request.prototype.ledgerSelect = function(ledger_spec) { - switch (ledger_spec) { +Request.prototype.ledgerSelect = function(ledger) { + switch (ledger) { case 'current': case 'closed': case 'verified': @@ -180,10 +188,10 @@ Request.prototype.ledgerSelect = function(ledger_spec) { break; default: - if (Number(ledger_spec)) { - this.message.ledger_index = ledger_spec; - } else { - this.message.ledger_hash = ledger_spec; + if (isNaN(ledger)) { + this.message.ledger_hash = ledger; + } else if (ledger = Number(ledger)) { + this.message.ledger_index = ledger; } break; } @@ -196,8 +204,8 @@ Request.prototype.accountRoot = function(account) { return this; }; -Request.prototype.index = function(hash) { - this.message.index = hash; +Request.prototype.index = function(index) { + this.message.index = index; return this; }; @@ -305,7 +313,7 @@ Request.prototype.books = function(books, snapshot) { Request.prototype.addBook = function (book, snapshot) { if (!Array.isArray(this.message.books)) { - this.message.books = []; + this.message.books = [ ]; } var json = { }; diff --git a/src/js/ripple/transaction.js b/src/js/ripple/transaction.js index b656142b..f1f4fc90 100644 --- a/src/js/ripple/transaction.js +++ b/src/js/ripple/transaction.js @@ -683,8 +683,8 @@ Transaction.prototype.payment = function(src, dst, amount) { src = options.source || options.from || options.account; } - if (options.invoiceID) { - this.invoiceID(options.invoiceID); + if (src.invoiceID) { + this.invoiceID(src.invoiceID); } if (!UInt160.is_valid(src)) {