From b23f596e3fb4d61779d5c015abb58bbcfce4c1ab Mon Sep 17 00:00:00 2001 From: Arthur Britto Date: Wed, 17 Oct 2012 15:37:23 -0700 Subject: [PATCH] JS: Automatically subscribe to server. --- js/remote.js | 25 +++++++++++++++---------- test/remote-test.js | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 10 deletions(-) diff --git a/js/remote.js b/js/remote.js index 1f300b569e..e36f790081 100644 --- a/js/remote.js +++ b/js/remote.js @@ -189,6 +189,7 @@ Remote.prototype._set_state = function (state) { this.online_state = 'open'; this.emit('connected'); break; + case 'offline': this.online_state = 'closed'; this.emit('disconnected'); @@ -295,11 +296,11 @@ Remote.prototype._connect_start = function () { }; if (self.online_target) { - if (self.trace) console.log("remote: onopen: %s: online_target2=%s", ws.readyState, self.online_target); + self._server_subscribe(); // Automatically subscribe. + self._set_state('online'); } else { - if (self.trace) console.log("remote: onopen: %s: online_target3=%s", ws.readyState, self.online_target); self._connect_stop(); } }; @@ -323,11 +324,12 @@ Remote.prototype._connect_start = function () { // Node's ws module doesn't pass arguments to onmessage. ws.on('message', function (json, flags) { - self._connect_message(json, flags); + self._connect_message(ws, json, flags); }); }; -Remote.prototype._connect_message = function (json, flags) { +// It is possible for messages to be dispatched after the connection is closed. +Remote.prototype._connect_message = function (ws, json, flags) { var message = JSON.parse(json); var unexpected = false; var request; @@ -339,7 +341,7 @@ Remote.prototype._connect_message = function (json, flags) { switch (message.type) { case 'response': { - request = this.ws.response[message.id]; + request = ws.response[message.id]; if (!request) { unexpected = true; @@ -558,20 +560,23 @@ Remote.prototype.submit = function (transaction) { // Subscribe to a server to get 'ledger_closed' events. // 'subscribed' : This command was successful. // 'ledger_closed : ledger_closed and ledger_current_index are updated. -Remote.prototype.server_subscribe = function () { +Remote.prototype._server_subscribe = function () { var self = this; var request = new Request(this, 'server_subscribe'); request. on('success', function (message) { - self.ledger_closed = message.ledger_closed; - self.ledger_current_index = message.ledger_current_index; self.stand_alone = !!message.stand_alone; - self.emit('subscribed'); + if (message.ledger_closed && message.ledger_current_index) { + self.ledger_closed = message.ledger_closed; + self.ledger_current_index = message.ledger_current_index; - self.emit('ledger_closed', self.ledger_closed, self.ledger_current_index-1); + self.emit('ledger_closed', self.ledger_closed, self.ledger_current_index-1); + } + + self.emit('subscribed'); }) .request(); diff --git a/test/remote-test.js b/test/remote-test.js index ebcdf731b5..5a99b43c27 100644 --- a/test/remote-test.js +++ b/test/remote-test.js @@ -213,6 +213,46 @@ buster.testCase("Remote functions", { }) .submit(); }, + + "create account final" : + function (done) { + var got_proposed; + var got_success; + + alpha.transaction() + .payment('root', 'alice', Amount.from_json("10000")) + .flags('CreateAccount') + .on('success', function (r) { + console.log("create_account: %s", JSON.stringify(r)); + + got_success = true; + }) + .on('error', function (m) { + console.log("error: %s", m); + + buster.assert(false); + }) + .on('final', function (m) { + console.log("final: %s", JSON.stringify(m)); + + buster.assert(got_success && got_proposed); + done(); + }) + .on('proposed', function (m) { + console.log("proposed: %s", JSON.stringify(m)); + + // buster.assert.equals(m.result, 'terNO_DST'); + buster.assert.equals(m.result, 'tesSUCCESS'); + + got_proposed = true; + + alpha.ledger_accept(); + }) + .on('status', function (s) { + console.log("status: %s", JSON.stringify(s)); + }) + .submit(); + }, }); // vim:sw=2:sts=2:ts=8