Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

asyncified filters fixes #220 #213 #159 #230

Merged
merged 8 commits into from
Jun 8, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
188 changes: 130 additions & 58 deletions dist/web3-light.js

Large diffs are not rendered by default.

85 changes: 85 additions & 0 deletions dist/web3-light.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/web3-light.min.js

Large diffs are not rendered by default.

188 changes: 130 additions & 58 deletions dist/web3.js

Large diffs are not rendered by default.

85 changes: 85 additions & 0 deletions dist/web3.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/web3.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/utils/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ module.exports = {
ETH_SIGNATURE_LENGTH: 4,
ETH_UNITS: ETH_UNITS,
ETH_BIGNUMBER_ROUNDING_MODE: { ROUNDING_MODE: BigNumber.ROUND_DOWN },
ETH_POLLING_TIMEOUT: 1000,
ETH_POLLING_TIMEOUT: 1000/2,
defaultBlock: 'latest',
defaultAccount: undefined
};
Expand Down
3 changes: 1 addition & 2 deletions lib/web3.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ web3.eth.filter = function (fil, eventParams, options, formatter) {
return fil(eventParams, options);
}

// what outputLogFormatter? that's wrong
//return new Filter(fil, watches.eth(), formatters.outputLogFormatter);
// output logs works for blockFilter and pendingTransaction filters?
return new Filter(fil, watches.eth(), formatter || formatters.outputLogFormatter);
};
/*jshint maxparams:3 */
Expand Down
99 changes: 71 additions & 28 deletions lib/web3/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,36 @@ var getOptions = function (options) {
};
};

var Filter = function (options, methods, formatter) {
var implementation = {};
methods.forEach(function (method) {
method.attachToObject(implementation);
});
this.options = getOptions(options);
this.implementation = implementation;
this.callbacks = [];
this.formatter = formatter;
this.filterId = this.implementation.newFilter(this.options);
/**
Adds the callback and sets up the methods, to iterate over the results.

@method getLogsAtStart
@param {Object} self
@param {funciton}
*/
var getLogsAtStart = function(self, callback){
// call getFilterLogs for the first watch callback start
if (!utils.isString(self.options)) {
self.get(function (err, messages) {
// don't send all the responses to all the watches again... just to self one
if (err) {
callback(err);
}

messages.forEach(function (message) {
callback(null, message);
});
});
}
};

Filter.prototype.watch = function (callback) {
this.callbacks.push(callback);
var self = this;
/**
Adds the callback and sets up the methods, to iterate over the results.

@method pollFilter
@param {Object} self
*/
var pollFilter = function(self) {

var onMessage = function (error, messages) {
if (error) {
Expand All @@ -105,29 +120,55 @@ Filter.prototype.watch = function (callback) {
});
};

// call getFilterLogs on start
if (!utils.isString(this.options)) {
this.get(function (err, messages) {
// don't send all the responses to all the watches again... just to this one
if (err) {
callback(err);
}
RequestManager.getInstance().startPolling({
method: self.implementation.poll.call,
params: [self.filterId],
}, self.filterId, onMessage, self.stopWatching.bind(self));

messages.forEach(function (message) {
callback(null, message);
};

var Filter = function (options, methods, formatter) {
var self = this;
var implementation = {};
methods.forEach(function (method) {
method.attachToObject(implementation);
});
this.options = getOptions(options);
this.implementation = implementation;
this.callbacks = [];
this.pollFilters = [];
this.formatter = formatter;
this.implementation.newFilter(this.options, function(error, id){
if(error) {
self.callbacks.forEach(function(callback){
callback(error);
});
});
} else {
self.filterId = id;
// get filter logs at start
self.callbacks.forEach(function(callback){
getLogsAtStart(self, callback);
});
pollFilter(self);
}
});
};

Filter.prototype.watch = function (callback) {
this.callbacks.push(callback);

if(this.filterId) {
getLogsAtStart(this, callback);
pollFilter(this);
}

RequestManager.getInstance().startPolling({
method: this.implementation.poll.call,
params: [this.filterId],
}, this.filterId, onMessage, this.stopWatching.bind(this));
return this;
};

Filter.prototype.stopWatching = function () {
RequestManager.getInstance().stopPolling(this.filterId);
this.implementation.uninstallFilter(this.filterId);
// remove filter async
this.implementation.uninstallFilter(this.filterId, function(){});
this.callbacks = [];
};

Expand All @@ -149,6 +190,8 @@ Filter.prototype.get = function (callback) {
return self.formatter ? self.formatter(log) : log;
});
}

return this;
};

module.exports = Filter;
Expand Down
13 changes: 11 additions & 2 deletions lib/web3/function.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
var web3 = require('../web3');
var coder = require('../solidity/coder');
var utils = require('../utils/utils');
var formatters = require('./formatters');
var sha3 = require('../utils/sha3');

/**
Expand All @@ -46,6 +47,12 @@ SolidityFunction.prototype.extractCallback = function (args) {
}
};

SolidityFunction.prototype.extractDefaultBlock = function (args) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is different functionality, please separate it to another pr

if (args.length > this._inputTypes.length && !utils.isObject(args[args.length -1])) {
return formatters.inputDefaultBlockNumberFormatter(args.pop()); // modify the args array!
}
};

/**
* Should be used to create payload from arguments
*
Expand Down Expand Up @@ -97,15 +104,17 @@ SolidityFunction.prototype.unpackOutput = function (output) {
SolidityFunction.prototype.call = function () {
var args = Array.prototype.slice.call(arguments).filter(function (a) {return a !== undefined; });
var callback = this.extractCallback(args);
var defaultBlock = this.extractDefaultBlock(args);
var payload = this.toPayload(args);


if (!callback) {
var output = web3.eth.call(payload);
var output = web3.eth.call(payload, defaultBlock);
return this.unpackOutput(output);
}

var self = this;
web3.eth.call(payload, function (error, output) {
web3.eth.call(payload, defaultBlock, function (error, output) {
callback(error, self.unpackOutput(output));
});
};
Expand Down
56 changes: 36 additions & 20 deletions lib/web3/requestmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var RequestManager = function (provider) {
arguments.callee._singletonInstance = this;

this.provider = provider;
this.polls = [];
this.polls = {};
this.timeout = null;
this.poll();
};
Expand Down Expand Up @@ -156,7 +156,7 @@ RequestManager.prototype.setProvider = function (p) {
* @todo cleanup number of params
*/
RequestManager.prototype.startPolling = function (data, pollId, callback, uninstall) {
this.polls.push({data: data, id: pollId, callback: callback, uninstall: uninstall});
this.polls['poll_'+ pollId] = {data: data, id: pollId, callback: callback, uninstall: uninstall};
};
/*jshint maxparams:3 */

Expand All @@ -167,24 +167,21 @@ RequestManager.prototype.startPolling = function (data, pollId, callback, uninst
* @param {Number} pollId
*/
RequestManager.prototype.stopPolling = function (pollId) {
for (var i = this.polls.length; i--;) {
var poll = this.polls[i];
if (poll.id === pollId) {
this.polls.splice(i, 1);
}
}
delete this.polls['poll_'+ pollId];
};

/**
* Should be called to reset polling mechanism of request manager
* Should be called to reset the polling mechanism of the request manager
*
* @method reset
*/
RequestManager.prototype.reset = function () {
this.polls.forEach(function (poll) {
poll.uninstall(poll.id);
});
this.polls = [];
for (var key in this.polls) {
if (this.polls.hasOwnProperty(key)) {
this.polls[key].uninstall();
}
}
this.polls = {};

if (this.timeout) {
clearTimeout(this.timeout);
Expand All @@ -199,9 +196,10 @@ RequestManager.prototype.reset = function () {
* @method poll
*/
RequestManager.prototype.poll = function () {
/*jshint maxcomplexity: 6 */
this.timeout = setTimeout(this.poll.bind(this), c.ETH_POLLING_TIMEOUT);

if (!this.polls.length) {
if (this.polls === {}) {
return;
}

Expand All @@ -210,24 +208,42 @@ RequestManager.prototype.poll = function () {
return;
}

var payload = Jsonrpc.getInstance().toBatchPayload(this.polls.map(function (data) {
return data.data;
}));
var pollsData = [];
var pollsKeys = [];
for (var key in this.polls) {
if (this.polls.hasOwnProperty(key)) {
pollsData.push(this.polls[key].data);
pollsKeys.push(key);
}
}

if (pollsData.length === 0) {
return;
}

var payload = Jsonrpc.getInstance().toBatchPayload(pollsData);

var self = this;
this.provider.sendAsync(payload, function (error, results) {
// TODO: console log?
if (error) {
return;
}

if (!utils.isArray(results)) {
throw errors.InvalidResponse(results);
}

results.map(function (result, index) {
result.callback = self.polls[index].callback;
return result;
var key = pollsKeys[index];
// make sure the filter is still installed after arrival of the request
if(self.polls[key]) {
result.callback = self.polls[key].callback;
return result;
} else
return false;
}).filter(function (result) {
return (!result) ? false : true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor, could be also:

return !!result;

}).filter(function (result) {
var valid = Jsonrpc.getInstance().isValidResponse(result);
if (!valid) {
Expand Down
4 changes: 2 additions & 2 deletions lib/web3/watches.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ var eth = function () {

switch(type) {
case 'latest':
args.pop();
args.shift();
this.params = 0;
return 'eth_newBlockFilter';
case 'pending':
args.pop();
args.shift();
this.params = 0;
return 'eth_newPendingTransactionFilter';
default:
Expand Down
Loading