diff options
author | Marek Kotewicz <marek.kotewicz@gmail.com> | 2014-11-11 18:46:46 +0800 |
---|---|---|
committer | Marek Kotewicz <marek.kotewicz@gmail.com> | 2014-11-11 18:46:46 +0800 |
commit | 84b36a71939cfc6c673f0fd3fec079ca7e23df6c (patch) | |
tree | 3289a94dd8ffe0ad25dfff6d298204bb985bb07f /lib/httprpc.js | |
parent | dea68f073e9a071e3e3940f7a467bf3cdc8d1992 (diff) | |
download | dexon-84b36a71939cfc6c673f0fd3fec079ca7e23df6c.tar dexon-84b36a71939cfc6c673f0fd3fec079ca7e23df6c.tar.gz dexon-84b36a71939cfc6c673f0fd3fec079ca7e23df6c.tar.bz2 dexon-84b36a71939cfc6c673f0fd3fec079ca7e23df6c.tar.lz dexon-84b36a71939cfc6c673f0fd3fec079ca7e23df6c.tar.xz dexon-84b36a71939cfc6c673f0fd3fec079ca7e23df6c.tar.zst dexon-84b36a71939cfc6c673f0fd3fec079ca7e23df6c.zip |
retabed files
Diffstat (limited to 'lib/httprpc.js')
-rw-r--r-- | lib/httprpc.js | 107 |
1 files changed, 53 insertions, 54 deletions
diff --git a/lib/httprpc.js b/lib/httprpc.js index 99f02b532..8141a6bae 100644 --- a/lib/httprpc.js +++ b/lib/httprpc.js @@ -21,74 +21,73 @@ * @date 2014 */ -if(process.env.NODE_ENV !== "build") { +if (process.env.NODE_ENV !== "build") { var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest; // jshint ignore:line } +var HttpRpcProvider = function (host) { + this.handlers = []; + this.host = host; +}; - var HttpRpcProvider = function (host) { - this.handlers = []; - this.host = host; +function formatJsonRpcObject(object) { + return { + jsonrpc: '2.0', + method: object.call, + params: object.args, + id: object._id }; +} - function formatJsonRpcObject(object) { - return { - jsonrpc: '2.0', - method: object.call, - params: object.args, - id: object._id - }; - } - - function formatJsonRpcMessage(message) { - var object = JSON.parse(message); - - return { - _id: object.id, - data: object.result, - error: object.error - }; - } - - HttpRpcProvider.prototype.sendRequest = function (payload, cb) { - var data = formatJsonRpcObject(payload); +function formatJsonRpcMessage(message) { + var object = JSON.parse(message); - var request = new XMLHttpRequest(); - request.open("POST", this.host, true); - request.send(JSON.stringify(data)); - request.onreadystatechange = function () { - if (request.readyState === 4 && cb) { - cb(request); - } - }; + return { + _id: object.id, + data: object.result, + error: object.error }; +} - HttpRpcProvider.prototype.send = function (payload) { - var self = this; - this.sendRequest(payload, function (request) { - self.handlers.forEach(function (handler) { - handler.call(self, formatJsonRpcMessage(request.responseText)); - }); - }); +HttpRpcProvider.prototype.sendRequest = function (payload, cb) { + var data = formatJsonRpcObject(payload); + + var request = new XMLHttpRequest(); + request.open("POST", this.host, true); + request.send(JSON.stringify(data)); + request.onreadystatechange = function () { + if (request.readyState === 4 && cb) { + cb(request); + } }; +}; - HttpRpcProvider.prototype.poll = function (payload, id) { - var self = this; - this.sendRequest(payload, function (request) { - var parsed = JSON.parse(request.responseText); - if (parsed.error || (parsed.result instanceof Array ? parsed.result.length === 0 : !parsed.result)) { - return; - } - self.handlers.forEach(function (handler) { - handler.call(self, {_event: payload.call, _id: id, data: parsed.result}); - }); +HttpRpcProvider.prototype.send = function (payload) { + var self = this; + this.sendRequest(payload, function (request) { + self.handlers.forEach(function (handler) { + handler.call(self, formatJsonRpcMessage(request.responseText)); }); - }; + }); +}; - Object.defineProperty(HttpRpcProvider.prototype, "onmessage", { - set: function (handler) { - this.handlers.push(handler); +HttpRpcProvider.prototype.poll = function (payload, id) { + var self = this; + this.sendRequest(payload, function (request) { + var parsed = JSON.parse(request.responseText); + if (parsed.error || (parsed.result instanceof Array ? parsed.result.length === 0 : !parsed.result)) { + return; } + self.handlers.forEach(function (handler) { + handler.call(self, {_event: payload.call, _id: id, data: parsed.result}); + }); }); +}; + +Object.defineProperty(HttpRpcProvider.prototype, "onmessage", { + set: function (handler) { + this.handlers.push(handler); + } +}); module.exports = HttpRpcProvider; |