-
Notifications
You must be signed in to change notification settings - Fork 5k
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
Changes from all commits
ddafe00
d9ce08e
1d3d727
f242489
16252f3
b6c49d4
1aefebd
1495a0c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,7 +43,7 @@ var RequestManager = function (provider) { | |
arguments.callee._singletonInstance = this; | ||
|
||
this.provider = provider; | ||
this.polls = []; | ||
this.polls = {}; | ||
this.timeout = null; | ||
this.poll(); | ||
}; | ||
|
@@ -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 */ | ||
|
||
|
@@ -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); | ||
|
@@ -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; | ||
} | ||
|
||
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
There was a problem hiding this comment.
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