Skip to content

Commit

Permalink
picked watch change3
Browse files Browse the repository at this point in the history
  • Loading branch information
debris authored and frozeman committed Apr 13, 2015
1 parent 56d13f7 commit c62f817
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 33 deletions.
25 changes: 17 additions & 8 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.

2 changes: 1 addition & 1 deletion dist/web3-light.min.js

Large diffs are not rendered by default.

25 changes: 17 additions & 8 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.

25 changes: 17 additions & 8 deletions lib/web3/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ var Filter = function (options, methods, formatter) {

Filter.prototype.watch = function (callback) {
this.callbacks.push(callback);
var self = this,
requestmanager = RequestManager.getInstance();
var self = this;

var onMessage = function (error, messages) {
if (error) {
Expand All @@ -89,10 +88,19 @@ Filter.prototype.watch = function (callback) {

// call getFilterLogs on start
if (!utils.isString(this.options)) {
this.get(onMessage);
this.get(function (err, messages) {
// don't send all the responses to all the watches again... just to this one
if (err) {
callback(err);
}

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

requestmanager.startPolling({
RequestManager.getInstance().startPolling({
method: this.implementation.poll.call,
params: [this.filterId],
}, this.filterId, onMessage, this.stopWatching.bind(this));
Expand All @@ -106,14 +114,15 @@ Filter.prototype.stopWatching = function () {

Filter.prototype.get = function (callback) {
var self = this;
if(utils.isFunction(callback)) {
if (utils.isFunction(callback)) {
this.implementation.getLogs(this.filterId, function(err, res){
if(!err) {
if (err) {
callback(err);
} else {
callback(null, res.map(function (log) {
return self.formatter ? self.formatter(log) : log;
}));
} else
callback(err);
}
});
} else {
var logs = this.implementation.getLogs(this.filterId);
Expand Down
23 changes: 21 additions & 2 deletions test/contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,22 @@ describe('web3.eth.contract', function () {
],
address: '0x1234567890123456789012345678901234567890'
});
} else if (step === 2 && utils.isArray(payload)) {
} else if (step === 2) {
step = 3;
provider.injectResult([{
address: address,
topics: [
sha3,
'0x0000000000000000000000001234567890123456789012345678901234567890',
'0x0000000000000000000000000000000000000000000000000000000000000001'
],
number: 2,
data: '0x0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000008'
}]);
assert.equal(payload.jsonrpc, '2.0');
assert.equal(payload.method, 'eth_getFilterLogs');
} else if (step === 3 && utils.isArray(payload)) {
provider.injectBatchResults([[{
address: address,
topics: [
Expand All @@ -89,12 +104,16 @@ describe('web3.eth.contract', function () {
var Contract = web3.eth.contract(desc);
var contract = new Contract(address);

var res = 0;
contract.Changed({from: address}).watch(function(err, result) {
assert.equal(result.args.from, address);
assert.equal(result.args.amount, 1);
assert.equal(result.args.t1, 1);
assert.equal(result.args.t2, 8);
done();
res++;
if (res === 2) {
done();
}
});
});

Expand Down

0 comments on commit c62f817

Please sign in to comment.