Jsonrpc - fix singleton pattern

This commit is contained in:
kumavis 2016-08-26 14:55:16 -07:00
parent 35bcace717
commit 74995bbbf7
7 changed files with 39 additions and 54 deletions

View File

@ -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));
}

View File

@ -17,25 +17,13 @@
/** @file jsonrpc.js
* @authors:
* Marek Kotewicz <marek@ethdev.com>
* Aaron Kumavis <aaron@kumavis.me>
* @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);
});
};

View File

@ -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));
}

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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');