Skip to content

Commit

Permalink
imporved async callback adding, without setinterval
Browse files Browse the repository at this point in the history
  • Loading branch information
frozeman committed Jun 8, 2015
1 parent f242489 commit 16252f3
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 155 deletions.
109 changes: 60 additions & 49 deletions dist/web3-light.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 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.

109 changes: 60 additions & 49 deletions dist/web3.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 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.

109 changes: 60 additions & 49 deletions lib/web3/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,50 @@ var getOptions = function (options) {
};
};

/**
Adds the callback and sets up the methods, to iterate over the results.
@method addCallback
@param {Object} self
@param {funciton} callback
*/
var addCallback = function(self, callback) {

var onMessage = function (error, messages) {
if (error) {
return self.callbacks.forEach(function (callback) {
callback(error);
});
}

messages.forEach(function (message) {
message = self.formatter ? self.formatter(message) : message;
self.callbacks.forEach(function (callback) {
callback(null, message);
});
});
};

// call getFilterLogs on 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);
});
});
}

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

var Filter = function (options, methods, formatter) {
var self = this;
var implementation = {};
Expand All @@ -83,64 +127,31 @@ var Filter = function (options, methods, formatter) {
this.options = getOptions(options);
this.implementation = implementation;
this.callbacks = [];
this.addCallbacks = [];
this.formatter = formatter;
this.implementation.newFilter(this.options, function(error, id){
if(error)
if(error) {
self.filterError = error;
else
self.addCallbacks.forEach(function(callback){
callback(error);
});
} else if(self.addCallbacks) {
self.filterId = id;
self.addCallbacks.forEach(function(callback){
addCallback(self, callback);
});
self.addCallbacks = [];
}
});
};

Filter.prototype.watch = function (callback) {
var self = this;

// check inf an interval of 10ms if the filter id has arrived
var intervalId = setInterval(function(){

if(self.filterId || self.filterError)
clearInterval(intervalId);

if(!self.filterId)
return;
this.callbacks.push(callback);

self.callbacks.push(callback);

var onMessage = function (error, messages) {
if (error) {
return self.callbacks.forEach(function (callback) {
callback(error);
});
}

messages.forEach(function (message) {
message = self.formatter ? self.formatter(message) : message;
self.callbacks.forEach(function (callback) {
callback(null, message);
});
});
};

// call getFilterLogs on 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);
});
});
}

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

}, 10);
if(this.filterId)
addCallback(this, callback);
else
this.addCallbacks.push(callback);
};

Filter.prototype.stopWatching = function () {
Expand Down

0 comments on commit 16252f3

Please sign in to comment.