diff --git a/js/amount.js b/js/amount.js index 87bfdce74a..ead5aa74db 100644 --- a/js/amount.js +++ b/js/amount.js @@ -10,7 +10,7 @@ var UInt160 = function () { }; // Returns NaN on error. -UInt160.method('parse_json', function (j) { +UInt160.prototype.parse_json = function (j) { // Canonicalize and validate switch (j) { @@ -50,11 +50,11 @@ UInt160.method('parse_json', function (j) { } return this.value; -}); +}; // Convert from internal form. // XXX Json form should allow 0 and 1, C++ doesn't currently allow it. -UInt160.method('to_json', function () { +UInt160.prototype.to_json = function () { if ("0" === this.value) { return exports.consts.hex_xns; } @@ -69,7 +69,7 @@ UInt160.method('to_json', function () { { return this.value; } -}); +}; var Currency = function () { // Internal form: 0 = XNS. 3 letter-code. @@ -82,7 +82,7 @@ var Currency = function () { } // Returns NaN on error. -Currency.method('parse_json', function (j) { +Currency.prototype.parse_json = function (j) { if ("" === j || "0" === j || "XNS" === j) { this.value = 0; } @@ -94,15 +94,15 @@ Currency.method('parse_json', function (j) { } return this.value; -}); +}; -Currency.method('to_json', function () { +Currency.prototype.to_json = function () { return this.value ? this.value : 'XNS'; -}); +}; -Currency.method('to_human', function() { +Currency.prototype.to_human = function() { return this.value ? this.value : 'XNS'; -}); +}; var Amount = function () { // Json format: @@ -116,16 +116,15 @@ var Amount = function () { this.currency = new Currency(); this.issuer = new UInt160(); - -} +}; // Convert only value to JSON text. -Amount.method('to_text', function() { +Amount.prototype.to_text = function() { // XXX Needs to work for native and non-native. return this.is_negative ? -this.value : this.value; // XXX Use biginteger. -}); +}; -Amount.method('to_json', function() { +Amount.prototype.to_json = function() { if (this.is_native) { return this.to_text(); } @@ -137,49 +136,49 @@ Amount.method('to_json', function() { 'issuer' : this.issuer.to_json(), }; } -}); +}; // Parse a native value. -Amount.method('parse_native', function() { +Amount.prototype.parse_native = function(j) { if ('integer' === typeof j) { // XNS - this.value = x >= 0 ? j : -j; // XXX Use biginteger. + this.value = j >= 0 ? j : -j; // XXX Use biginteger. this.offset = 0; this.is_native = true; - this.is_negative = x < 0; + this.is_negative = j < 0; } else if ('string' === typeof j) { - this.value = x >= 0 ? j : -j; // XXX Use biginteger. + this.value = j >= 0 ? j : -j; // XXX Use biginteger. this.offset = 0; this.is_native = true; - this.is_negative = x < 0; + this.is_negative = j < 0; } else { this.value = NaN; } -}); +}; // Parse a non-native value. -Amount.method('parse_value', function() { +Amount.prototype.parse_value = function(j) { if ('integer' === typeof j) { - this.value = x >= 0 ? j : -j; // XXX Use biginteger. + this.value = j >= 0 ? j : -j; // XXX Use biginteger. this.offset = 0; this.is_native = false; - this.is_negative = x < 0; + this.is_negative = j < 0; } else if ('string' === typeof j) { - this.value = x >= 0 ? j : -j; // XXX Use biginteger. + this.value = j >= 0 ? j : -j; // XXX Use biginteger. this.offset = 0; this.is_native = false; - this.is_negative = x < 0; + this.is_negative = j < 0; } else { this.value = NaN; } -}); +}; // <-> j -Amount.method('parse_json', function(j) { +Amount.prototype.parse_json = function(j) { if ('object' === typeof j && j.currency) { this.parse_value(j); @@ -191,13 +190,13 @@ Amount.method('parse_json', function(j) { this.currency = 0; this.issuer = 0; } -}); +}; -exports.UInt160 = UInt160; +exports.Amount = Amount; exports.Currency = Currency; -exports.Amount = Amount; +exports.UInt160 = UInt160; -exports.consts = { +exports.consts = { 'address_xns' : "iiiiiiiiiiiiiiiiiiiiihoLvTp", 'address_one' : "iiiiiiiiiiiiiiiiiiiiBZbvjr", 'currency_xns' : 0, diff --git a/js/remote.js b/js/remote.js index 069b1c0753..8919d6308a 100644 --- a/js/remote.js +++ b/js/remote.js @@ -63,7 +63,7 @@ var fees = { 'offer' : 100, }; -Remote.method('connect_helper', function () { +Remote.prototype.connect_helper = function () { var self = this; if (this.trace) console.log("remote: connect: %s", this.url); @@ -88,11 +88,13 @@ Remote.method('connect_helper', function () { if (self.expire) { if (this.trace) console.log("remote: was expired"); + self.done(ws.readyState); } else { // Delay and retry. setTimeout(function () { if (this.trace) console.log("remote: retry"); + self.connect_helper(); }, 50); // Retry rate 50ms. } @@ -101,6 +103,7 @@ Remote.method('connect_helper', function () { // Covers failure to open. ws.onclose = function () { if (this.trace) console.log("remote: onclose: %s", ws.readyState); + ws.onerror = undefined; self.done(ws.readyState); }; @@ -112,6 +115,7 @@ Remote.method('connect_helper', function () { if (message.type !== 'response') { console.log("unexpected message: %s", json); + } else { var done = ws.response[message.id]; if (done) { @@ -121,12 +125,12 @@ Remote.method('connect_helper', function () { } } }); -}); +}; // Target state is connectted. // done(readyState): // --> readyState: OPEN, CLOSED -Remote.method('connect', function (done, timeout) { +Remote.prototype.connect = function (done, timeout) { var self = this; this.url = util.format("ws://%s:%s", this.websocket_ip, this.websocket_port); @@ -147,10 +151,10 @@ Remote.method('connect', function (done, timeout) { } this.connect_helper(); -}); +}; // Target stated is disconnected. -Remote.method('disconnect', function (done) { +Remote.prototype.disconnect = function (done) { var self = this; var ws = this.ws; @@ -160,11 +164,11 @@ Remote.method('disconnect', function (done) { }; ws.close(); -}); +}; // Send a request. The request should lack the id. // <-> request: what to send, consumed. -Remote.method('request', function (request, onDone, onFailure) { +Remote.prototype.request = function (request, onDone, onFailure) { var self = this; this.id += 1; // Advance id. @@ -187,24 +191,24 @@ Remote.method('request', function (request, onDone, onFailure) { if (this.trace) console.log("remote: request: %s", JSON.stringify(request)); this.ws.send(JSON.stringify(request)); -}); +}; -Remote.method('request_ledger_closed', function (onDone, onFailure) { +Remote.prototype.request_ledger_closed = function (onDone, onFailure) { assert(this.trusted); // If not trusted, need to check proof. this.request({ 'command' : 'ledger_closed' }, onDone, onFailure); -}); +}; // Get the current proposed ledger entry. May be closed (and revised) at any time (even before returning). // Only for use by unit tests. -Remote.method('request_ledger_current', function (onDone, onFailure) { +Remote.prototype.request_ledger_current = function (onDone, onFailure) { this.request({ 'command' : 'ledger_current' }, onDone, onFailure); -}); +}; // <-> request: // --> ledger : optional // --> ledger_index : optional // --> type -Remote.method('request_ledger_entry', function (req, onDone, onFailure) { +Remote.prototype.request_ledger_entry = function (req, onDone, onFailure) { var self = this; assert(this.trusted); // If not trusted, need to check proof, maybe talk packet protocol. @@ -264,12 +268,12 @@ Remote.method('request_ledger_entry', function (req, onDone, onFailure) { }, onFailure); } } -}); +}; // Submit a json transaction. // done(value) // XXX <-> value: { 'status', status, 'result' : result, ... } -Remote.method('submit', function (req, onDone, onFailure) { +Remote.prototype.submit = function (req, onDone, onFailure) { if (this.trace) console.log("remote: submit: %s", JSON.stringify(req)); req.command = 'submit'; @@ -282,7 +286,7 @@ Remote.method('submit', function (req, onDone, onFailure) { { this.request(req, onDone, onFailure); } -}); +}; // // Higher level functions. @@ -290,7 +294,7 @@ Remote.method('submit', function (req, onDone, onFailure) { // Subscribe to a server to get the current and closed ledger. // XXX Set up routine to update on notification. -Remote.method('server_subscribe', function (onDone, onFailure) { +Remote.prototype.server_subscribe = function (onDone, onFailure) { var self = this; this.request( @@ -303,11 +307,11 @@ Remote.method('server_subscribe', function (onDone, onFailure) { }, onFailure ); -}); +}; // Refresh accounts[account].seq // done(result); -Remote.method('account_seq', function (account, advance, onDone, onFailure) { +Remote.prototype.account_seq = function (account, advance, onDone, onFailure) { var self = this; var account_root_entry = this.accounts[account]; @@ -341,10 +345,10 @@ Remote.method('account_seq', function (account, advance, onDone, onFailure) { onFailure ); } -}); +}; // A submit that fills in the sequence number. -Remote.method('submit_seq', function (trans, onDirty, onDone, onFailure) { +Remote.prototype.submit_seq = function (trans, onDirty, onDone, onFailure) { var self = this; // Get the next sequence number for the account. @@ -354,18 +358,18 @@ Remote.method('submit_seq', function (trans, onDirty, onDone, onFailure) { self.submit(trans, onDone, onFailure); }, onFailure); -}); +}; // Mark an account's root node as dirty. -Remote.method('dirty_account_root', function (account) { +Remote.prototype.dirty_account_root = function (account) { delete this.ledgers.current.account_root.account; -}); +}; // // Transactions // -Remote.method('ripple_line_set', function (secret, src, dst, amount, onDone) { +Remote.prototype.ripple_line_set = function (secret, src, dst, amount, onDone) { var secret = this.config.accounts[src] ? this.config.accounts[src].secret : secret; var src_account = this.config.accounts[src] ? this.config.accounts[src].account : src; var dst_account = this.config.accounts[dst] ? this.config.accounts[dst].account : dst; @@ -382,9 +386,9 @@ Remote.method('ripple_line_set', function (secret, src, dst, amount, onDone) { 'secret' : secret, }, function () { }, onDone); -}); +}; -Remote.method('send_xns', function (secret, src, dst, amount, create, onDone) { +Remote.prototype.send_xns = function (secret, src, dst, amount, create, onDone) { var secret = this.config.accounts[src] ? this.config.accounts[src].secret : secret; var src_account = this.config.accounts[src] ? this.config.accounts[src].account : src; var dst_account = this.config.accounts[dst] ? this.config.accounts[dst].account : dst; @@ -402,7 +406,7 @@ Remote.method('send_xns', function (secret, src, dst, amount, create, onDone) { 'secret' : secret, }, function () { }, onDone); -}); +}; exports.Remote = Remote; exports.remoteConfig = remoteConfig; diff --git a/js/utils.js b/js/utils.js index a589ac391c..950da3e416 100644 --- a/js/utils.js +++ b/js/utils.js @@ -4,127 +4,127 @@ var fs = require("fs"); var path = require("path"); Function.prototype.method = function(name,func) { - this.prototype[name] = func; + this.prototype[name] = func; - return this; + return this; }; var filterErr = function(code, done) { - return function(e) { - done(e.code !== code ? e : undefined); - }; + return function(e) { + done(e.code !== code ? e : undefined); + }; }; var throwErr = function(done) { - return function(e) { - if (e) - throw e; - - done(); - }; + return function(e) { + if (e) + throw e; + + done(); + }; }; // apply function to elements of array. Return first true value to done or undefined. var mapOr = function(func, array, done) { - if (array.length) { - func(array[array.length-1], function(v) { - if (v) { - done(v); - } - else { - array.length -= 1; - mapOr(func, array, done); - } - }); - } - else { - done(); - } + if (array.length) { + func(array[array.length-1], function(v) { + if (v) { + done(v); + } + else { + array.length -= 1; + mapOr(func, array, done); + } + }); + } + else { + done(); + } }; // Make a directory and sub-directories. var mkPath = function(dirPath, mode, done) { - fs.mkdir(dirPath, typeof mode === "string" ? parseInt(mode, 8) : mode, function(e) { - if (!e || e.code === "EEXIST") { - // Created or already exists, done. - done(); - } - else if (e.code === "ENOENT") { - // Missing sub dir. + fs.mkdir(dirPath, typeof mode === "string" ? parseInt(mode, 8) : mode, function(e) { + if (!e || e.code === "EEXIST") { + // Created or already exists, done. + done(); + } + else if (e.code === "ENOENT") { + // Missing sub dir. - mkPath(path.dirname(dirPath), mode, function(e) { - if (e) { - throw e; - } - else { - mkPath(dirPath, mode, done); - } - }); + mkPath(path.dirname(dirPath), mode, function(e) { + if (e) { + throw e; } else { - throw e; + mkPath(dirPath, mode, done); } - }); + }); + } + else { + throw e; + } + }); }; // Empty a directory. var emptyPath = function(dirPath, done) { - fs.readdir(dirPath, function(err, files) { - if (err) { - done(err); - } - else { - mapOr(rmPath, files.map(function(f) { return path.join(dirPath, f); }), done); - } - }); + fs.readdir(dirPath, function(err, files) { + if (err) { + done(err); + } + else { + mapOr(rmPath, files.map(function(f) { return path.join(dirPath, f); }), done); + } + }); }; // Remove path recursively. var rmPath = function(dirPath, done) { // console.log("rmPath: %s", dirPath); - fs.lstat(dirPath, function(err, stats) { - if (err && err.code == "ENOENT") { - done(); - } - if (err) { - done(err); - } - else if (stats.isDirectory()) { - emptyPath(dirPath, function(e) { - if (e) { - done(e); - } - else { -// console.log("rmdir: %s", dirPath); done(); - fs.rmdir(dirPath, done); - } - }); - } - else { -// console.log("unlink: %s", dirPath); done(); - fs.unlink(dirPath, done); - } - }); + fs.lstat(dirPath, function(err, stats) { + if (err && err.code == "ENOENT") { + done(); + } + if (err) { + done(err); + } + else if (stats.isDirectory()) { + emptyPath(dirPath, function(e) { + if (e) { + done(e); + } + else { +// console.log("rmdir: %s", dirPath); done(); + fs.rmdir(dirPath, done); + } + }); + } + else { +// console.log("unlink: %s", dirPath); done(); + fs.unlink(dirPath, done); + } + }); }; // Create directory if needed and empty if needed. var resetPath = function(dirPath, mode, done) { - mkPath(dirPath, mode, function(e) { - if (e) { - done(e); - } - else { - emptyPath(dirPath, done); - } - }); + mkPath(dirPath, mode, function(e) { + if (e) { + done(e); + } + else { + emptyPath(dirPath, done); + } + }); }; var trace = function(comment, func) { - return function() { - console.log("%s: %s", trace, arguments.toString); - func(arguments); - }; + return function() { + console.log("%s: %s", trace, arguments.toString); + func(arguments); + }; }; var hexToString = function (h) {