From 74995bbbf7e1d209da44a63c551120842a1b24db Mon Sep 17 00:00:00 2001 From: kumavis Date: Fri, 26 Aug 2016 14:55:16 -0700 Subject: [PATCH] Jsonrpc - fix singleton pattern --- lib/web3/batch.js | 2 +- lib/web3/jsonrpc.js | 36 ++++++++++++--------------------- lib/web3/requestmanager.js | 14 ++++++------- test/jsonrpc.id.js | 6 ++---- test/jsonrpc.isValidResponse.js | 21 +++++++++---------- test/jsonrpc.toBatchPayload.js | 7 +++---- test/jsonrpc.toPayload.js | 7 +++---- 7 files changed, 39 insertions(+), 54 deletions(-) diff --git a/lib/web3/batch.js b/lib/web3/batch.js index acb6336..0c9dc98 100644 --- a/lib/web3/batch.js +++ b/lib/web3/batch.js @@ -52,7 +52,7 @@ Batch.prototype.execute = function () { }).forEach(function (result, index) { if (requests[index].callback) { - if (!Jsonrpc.getInstance().isValidResponse(result)) { + if (!Jsonrpc.isValidResponse(result)) { return requests[index].callback(errors.InvalidResponse(result)); } diff --git a/lib/web3/jsonrpc.js b/lib/web3/jsonrpc.js index d6c0615..9523cfc 100644 --- a/lib/web3/jsonrpc.js +++ b/lib/web3/jsonrpc.js @@ -17,25 +17,13 @@ /** @file jsonrpc.js * @authors: * Marek Kotewicz + * Aaron Kumavis * @date 2015 */ -var Jsonrpc = function () { - // singleton pattern - if (arguments.callee._singletonInstance) { - return arguments.callee._singletonInstance; - } - arguments.callee._singletonInstance = this; - - this.messageId = 1; -}; - -/** - * @return {Jsonrpc} singleton - */ -Jsonrpc.getInstance = function () { - var instance = new Jsonrpc(); - return instance; +// Initialize Jsonrpc as a simple object with utility functions. +var Jsonrpc = { + messageId: 0 }; /** @@ -46,15 +34,18 @@ Jsonrpc.getInstance = function () { * @param {Array} params, an array of method params, optional * @returns {Object} valid jsonrpc payload object */ -Jsonrpc.prototype.toPayload = function (method, params) { +Jsonrpc.toPayload = function (method, params) { if (!method) console.error('jsonrpc method should be specified!'); + // advance message ID + Jsonrpc.messageId++ + return { jsonrpc: '2.0', + id: Jsonrpc.messageId, method: method, - params: params || [], - id: this.messageId++ + params: params || [] }; }; @@ -65,7 +56,7 @@ Jsonrpc.prototype.toPayload = function (method, params) { * @param {Object} * @returns {Boolean} true if response is valid, otherwise false */ -Jsonrpc.prototype.isValidResponse = function (response) { +Jsonrpc.isValidResponse = function (response) { return !!response && !response.error && response.jsonrpc === '2.0' && @@ -80,10 +71,9 @@ Jsonrpc.prototype.isValidResponse = function (response) { * @param {Array} messages, an array of objects with method (required) and params (optional) fields * @returns {Array} batch payload */ -Jsonrpc.prototype.toBatchPayload = function (messages) { - var self = this; +Jsonrpc.toBatchPayload = function (messages) { return messages.map(function (message) { - return self.toPayload(message.method, message.params); + return Jsonrpc.toPayload(message.method, message.params); }); }; diff --git a/lib/web3/requestmanager.js b/lib/web3/requestmanager.js index 3f9c6a0..aa59b3a 100644 --- a/lib/web3/requestmanager.js +++ b/lib/web3/requestmanager.js @@ -54,10 +54,10 @@ RequestManager.prototype.send = function (data) { return null; } - var payload = Jsonrpc.getInstance().toPayload(data.method, data.params); + var payload = Jsonrpc.toPayload(data.method, data.params); var result = this.provider.send(payload); - if (!Jsonrpc.getInstance().isValidResponse(result)) { + if (!Jsonrpc.isValidResponse(result)) { throw errors.InvalidResponse(result); } @@ -76,13 +76,13 @@ RequestManager.prototype.sendAsync = function (data, callback) { return callback(errors.InvalidProvider()); } - var payload = Jsonrpc.getInstance().toPayload(data.method, data.params); + var payload = Jsonrpc.toPayload(data.method, data.params); this.provider.sendAsync(payload, function (err, result) { if (err) { return callback(err); } - if (!Jsonrpc.getInstance().isValidResponse(result)) { + if (!Jsonrpc.isValidResponse(result)) { return callback(errors.InvalidResponse(result)); } @@ -102,7 +102,7 @@ RequestManager.prototype.sendBatch = function (data, callback) { return callback(errors.InvalidProvider()); } - var payload = Jsonrpc.getInstance().toBatchPayload(data); + var payload = Jsonrpc.toBatchPayload(data); this.provider.sendAsync(payload, function (err, results) { if (err) { @@ -217,7 +217,7 @@ RequestManager.prototype.poll = function () { return; } - var payload = Jsonrpc.getInstance().toBatchPayload(pollsData); + var payload = Jsonrpc.toBatchPayload(pollsData); // map the request id to they poll id var pollsIdMap = {}; @@ -250,7 +250,7 @@ RequestManager.prototype.poll = function () { }).filter(function (result) { return !!result; }).filter(function (result) { - var valid = Jsonrpc.getInstance().isValidResponse(result); + var valid = Jsonrpc.isValidResponse(result); if (!valid) { result.callback(errors.InvalidResponse(result)); } diff --git a/test/jsonrpc.id.js b/test/jsonrpc.id.js index 07ede28..bdd093e 100644 --- a/test/jsonrpc.id.js +++ b/test/jsonrpc.id.js @@ -7,13 +7,11 @@ describe('lib/web3/jsonrpc', function () { it('should increment the id', function () { // given - var a = Jsonrpc.getInstance(); - var b = Jsonrpc.getInstance(); var method = 'm'; // when - var p1 = a.toPayload(method); - var p2 = b.toPayload(method); + var p1 = Jsonrpc.toPayload(method); + var p2 = Jsonrpc.toPayload(method); // then assert.equal(p2.id, p1.id + 1); diff --git a/test/jsonrpc.isValidResponse.js b/test/jsonrpc.isValidResponse.js index fc42f50..e5ab2a6 100644 --- a/test/jsonrpc.isValidResponse.js +++ b/test/jsonrpc.isValidResponse.js @@ -1,6 +1,5 @@ var assert = require('assert'); -var jsonrpc = require('../lib/web3/jsonrpc'); -jsonrpc = new jsonrpc(); +var Jsonrpc = require('../lib/web3/jsonrpc'); describe('jsonrpc', function () { describe('isValidResponse', function () { @@ -14,7 +13,7 @@ describe('jsonrpc', function () { }; // when - var valid = jsonrpc.isValidResponse(response); + var valid = Jsonrpc.isValidResponse(response); // then assert.equal(valid, true); @@ -26,7 +25,7 @@ describe('jsonrpc', function () { var response = undefined; // when - var valid = jsonrpc.isValidResponse(response); + var valid = Jsonrpc.isValidResponse(response); // then assert.equal(valid, false); @@ -41,7 +40,7 @@ describe('jsonrpc', function () { }; // when - var valid = jsonrpc.isValidResponse(response); + var valid = Jsonrpc.isValidResponse(response); // then assert.equal(valid, false); @@ -57,7 +56,7 @@ describe('jsonrpc', function () { }; // when - var valid = jsonrpc.isValidResponse(response); + var valid = Jsonrpc.isValidResponse(response); // then assert.equal(valid, false); @@ -72,7 +71,7 @@ describe('jsonrpc', function () { }; // when - var valid = jsonrpc.isValidResponse(response); + var valid = Jsonrpc.isValidResponse(response); // then assert.equal(valid, false); @@ -88,7 +87,7 @@ describe('jsonrpc', function () { }; // when - var valid = jsonrpc.isValidResponse(response); + var valid = Jsonrpc.isValidResponse(response); // then assert.equal(valid, false); @@ -103,7 +102,7 @@ describe('jsonrpc', function () { }; // when - var valid = jsonrpc.isValidResponse(response); + var valid = Jsonrpc.isValidResponse(response); // then assert.equal(valid, false); @@ -119,7 +118,7 @@ describe('jsonrpc', function () { }; // when - var valid = jsonrpc.isValidResponse(response); + var valid = Jsonrpc.isValidResponse(response); // then assert.equal(valid, true); @@ -135,7 +134,7 @@ describe('jsonrpc', function () { }; // when - var valid = jsonrpc.isValidResponse(response); + var valid = Jsonrpc.isValidResponse(response); // then assert.equal(valid, true); diff --git a/test/jsonrpc.toBatchPayload.js b/test/jsonrpc.toBatchPayload.js index b1b523f..d82e159 100644 --- a/test/jsonrpc.toBatchPayload.js +++ b/test/jsonrpc.toBatchPayload.js @@ -1,6 +1,5 @@ var assert = require('assert'); -var jsonrpc = require('../lib/web3/jsonrpc'); -jsonrpc = new jsonrpc(); +var Jsonrpc = require('../lib/web3/jsonrpc'); describe('jsonrpc', function () { describe('toBatchPayload', function () { @@ -15,7 +14,7 @@ describe('jsonrpc', function () { }]; // when - var payload = jsonrpc.toBatchPayload(messages); + var payload = Jsonrpc.toBatchPayload(messages); // then assert.equal(payload instanceof Array, true); @@ -38,7 +37,7 @@ describe('jsonrpc', function () { var messages = []; // when - var payload = jsonrpc.toBatchPayload(messages); + var payload = Jsonrpc.toBatchPayload(messages); // then assert.equal(payload instanceof Array, true); diff --git a/test/jsonrpc.toPayload.js b/test/jsonrpc.toPayload.js index dbec9af..c480576 100644 --- a/test/jsonrpc.toPayload.js +++ b/test/jsonrpc.toPayload.js @@ -1,7 +1,6 @@ var chai = require('chai'); var assert = chai.assert; -var jsonrpc = require('../lib/web3/jsonrpc'); -jsonrpc = new jsonrpc(); +var Jsonrpc = require('../lib/web3/jsonrpc'); describe('jsonrpc', function () { describe('toPayload', function () { @@ -11,7 +10,7 @@ describe('jsonrpc', function () { var method = 'helloworld'; // when - var payload = jsonrpc.toPayload(method); + var payload = Jsonrpc.toPayload(method); // then assert.equal(payload.jsonrpc, '2.0'); @@ -28,7 +27,7 @@ describe('jsonrpc', function () { var params = [123, 'test']; // when - var payload = jsonrpc.toPayload(method, params); + var payload = Jsonrpc.toPayload(method, params); // then assert.equal(payload.jsonrpc, '2.0');