Last active
February 26, 2018 14:05
-
-
Save nakajo2011/284213062e2eba0cbf001fb6673dbf5b to your computer and use it in GitHub Desktop.
web3.eth.filter.watch該当ソースコード
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// see https://github.com/ethereum/web3.js/blob/develop/lib/web3/filter.js#L191-L200 | |
Filter.prototype.watch = function (callback) { | |
this.callbacks.push(callback); | |
if(this.filterId) { | |
getLogsAtStart(this, callback); | |
pollFilter(this); | |
} | |
return this; | |
}; | |
// see https://github.com/ethereum/web3.js/blob/develop/lib/web3/filter.js#L115-L139 | |
var pollFilter = function(self) { | |
var onMessage = function (error, messages) { | |
if (error) { | |
return self.callbacks.forEach(function (callback) { | |
callback(error); | |
}); | |
} | |
if(utils.isArray(messages)) { | |
messages.forEach(function (message) { | |
message = self.formatter ? self.formatter(message) : message; | |
self.callbacks.forEach(function (callback) { | |
callback(null, message); | |
}); | |
}); | |
} | |
}; | |
self.requestManager.startPolling({ | |
method: self.implementation.poll.call, | |
params: [self.filterId], | |
}, self.filterId, onMessage, self.stopWatching.bind(self)); | |
}; | |
// see https://github.com/ethereum/web3.js/blob/develop/lib/web3/requestmanager.js#L196-L262 | |
RequestManager.prototype.poll = function () { | |
/*jshint maxcomplexity: 6 */ | |
this.timeout = setTimeout(this.poll.bind(this), c.ETH_POLLING_TIMEOUT); | |
if (Object.keys(this.polls).length === 0) { | |
return; | |
} | |
~~~ snip ~~~ | |
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) { | |
var id = pollsIdMap[result.id]; | |
// make sure the filter is still installed after arrival of the request | |
if (self.polls[id]) { | |
result.callback = self.polls[id].callback; | |
return result; | |
} else | |
return false; | |
}).filter(function (result) { | |
return !!result; | |
}).filter(function (result) { | |
var valid = Jsonrpc.isValidResponse(result); | |
if (!valid) { | |
result.callback(errors.InvalidResponse(result)); | |
} | |
return valid; | |
}).forEach(function (result) { | |
result.callback(null, result.result); | |
}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment