good morning!!!!

Skip to content
Snippets Groups Projects
Commit 8b7d4b0c authored by Marek Kotewicz's avatar Marek Kotewicz
Browse files

beginning of comments in web3

parent ee167e53
Branches
Tags
No related merge requests found
...@@ -23,6 +23,9 @@ ...@@ -23,6 +23,9 @@
* @date 2014 * @date 2014
*/ */
/// Recursively resolves all promises in given object and replaces the resolved values with promises
/// @param any object/array/promise/anything else..
/// @returns (resolves) object with replaced promises with their result
function flattenPromise (obj) { function flattenPromise (obj) {
if (obj instanceof Promise) { if (obj instanceof Promise) {
return Promise.resolve(obj); return Promise.resolve(obj);
...@@ -62,12 +65,14 @@ function flattenPromise (obj) { ...@@ -62,12 +65,14 @@ function flattenPromise (obj) {
return Promise.resolve(obj); return Promise.resolve(obj);
} }
/// @returns an array of objects describing web3 api methods
var web3Methods = function () { var web3Methods = function () {
return [ return [
{ name: 'sha3', call: 'web3_sha3' } { name: 'sha3', call: 'web3_sha3' }
]; ];
}; };
/// @returns an array of objects describing web3.eth api methods
var ethMethods = function () { var ethMethods = function () {
var blockCall = function (args) { var blockCall = function (args) {
return typeof args[0] === "string" ? "eth_blockByHash" : "eth_blockByNumber"; return typeof args[0] === "string" ? "eth_blockByHash" : "eth_blockByNumber";
...@@ -101,6 +106,7 @@ var ethMethods = function () { ...@@ -101,6 +106,7 @@ var ethMethods = function () {
return methods; return methods;
}; };
/// @returns an array of objects describing web3.eth api properties
var ethProperties = function () { var ethProperties = function () {
return [ return [
{ name: 'coinbase', getter: 'eth_coinbase', setter: 'eth_setCoinbase' }, { name: 'coinbase', getter: 'eth_coinbase', setter: 'eth_setCoinbase' },
...@@ -115,6 +121,7 @@ var ethProperties = function () { ...@@ -115,6 +121,7 @@ var ethProperties = function () {
]; ];
}; };
/// @returns an array of objects describing web3.db api methods
var dbMethods = function () { var dbMethods = function () {
return [ return [
{ name: 'put', call: 'db_put' }, { name: 'put', call: 'db_put' },
...@@ -124,6 +131,7 @@ var dbMethods = function () { ...@@ -124,6 +131,7 @@ var dbMethods = function () {
]; ];
}; };
/// @returns an array of objects describing web3.shh api methods
var shhMethods = function () { var shhMethods = function () {
return [ return [
{ name: 'post', call: 'shh_post' }, { name: 'post', call: 'shh_post' },
...@@ -134,6 +142,7 @@ var shhMethods = function () { ...@@ -134,6 +142,7 @@ var shhMethods = function () {
]; ];
}; };
/// @returns an array of objects describing web3.eth.watch api methods
var ethWatchMethods = function () { var ethWatchMethods = function () {
var newFilter = function (args) { var newFilter = function (args) {
return typeof args[0] === 'string' ? 'eth_newFilterString' : 'eth_newFilter'; return typeof args[0] === 'string' ? 'eth_newFilterString' : 'eth_newFilter';
...@@ -146,6 +155,7 @@ var ethWatchMethods = function () { ...@@ -146,6 +155,7 @@ var ethWatchMethods = function () {
]; ];
}; };
/// @returns an array of objects describing web3.shh.watch api methods
var shhWatchMethods = function () { var shhWatchMethods = function () {
return [ return [
{ name: 'newFilter', call: 'shh_newFilter' }, { name: 'newFilter', call: 'shh_newFilter' },
...@@ -154,6 +164,8 @@ var shhWatchMethods = function () { ...@@ -154,6 +164,8 @@ var shhWatchMethods = function () {
]; ];
}; };
/// creates methods in a given object based on method description on input
/// setups api calls for these methods
var setupMethods = function (obj, methods) { var setupMethods = function (obj, methods) {
methods.forEach(function (method) { methods.forEach(function (method) {
obj[method.name] = function () { obj[method.name] = function () {
...@@ -177,6 +189,8 @@ var setupMethods = function (obj, methods) { ...@@ -177,6 +189,8 @@ var setupMethods = function (obj, methods) {
}); });
}; };
/// creates properties in a given object based on properties description on input
/// setups api calls for these properties
var setupProperties = function (obj, properties) { var setupProperties = function (obj, properties) {
properties.forEach(function (property) { properties.forEach(function (property) {
var proto = {}; var proto = {};
...@@ -221,7 +235,7 @@ var decToHex = function (dec) { ...@@ -221,7 +235,7 @@ var decToHex = function (dec) {
return parseInt(dec).toString(16); return parseInt(dec).toString(16);
}; };
/// setups web3 object, and it's in-browser executed methods
var web3 = { var web3 = {
_callbacks: {}, _callbacks: {},
_events: {}, _events: {},
...@@ -339,6 +353,7 @@ var web3 = { ...@@ -339,6 +353,7 @@ var web3 = {
} }
}; };
/// setups all api methods
setupMethods(web3, web3Methods()); setupMethods(web3, web3Methods());
setupMethods(web3.eth, ethMethods()); setupMethods(web3.eth, ethMethods());
setupProperties(web3.eth, ethProperties()); setupProperties(web3.eth, ethProperties());
...@@ -348,12 +363,16 @@ setupMethods(web3.shh, shhMethods()); ...@@ -348,12 +363,16 @@ setupMethods(web3.shh, shhMethods());
var ethWatch = { var ethWatch = {
changed: 'eth_changed' changed: 'eth_changed'
}; };
setupMethods(ethWatch, ethWatchMethods()); setupMethods(ethWatch, ethWatchMethods());
var shhWatch = { var shhWatch = {
changed: 'shh_changed' changed: 'shh_changed'
}; };
setupMethods(shhWatch, shhWatchMethods()); setupMethods(shhWatch, shhWatchMethods());
/// Provider manager object prototype
var ProviderManager = function() { var ProviderManager = function() {
this.queued = []; this.queued = [];
this.polls = []; this.polls = [];
...@@ -375,6 +394,7 @@ var ProviderManager = function() { ...@@ -375,6 +394,7 @@ var ProviderManager = function() {
poll(); poll();
}; };
/// sends outgoing requests, if provider is not available, enqueue the request
ProviderManager.prototype.send = function(data, cb) { ProviderManager.prototype.send = function(data, cb) {
data._id = this.id; data._id = this.id;
if (cb) { if (cb) {
...@@ -392,6 +412,7 @@ ProviderManager.prototype.send = function(data, cb) { ...@@ -392,6 +412,7 @@ ProviderManager.prototype.send = function(data, cb) {
} }
}; };
/// setups provider, which will be used for sending messages
ProviderManager.prototype.set = function(provider) { ProviderManager.prototype.set = function(provider) {
if(this.provider !== undefined && this.provider.unload !== undefined) { if(this.provider !== undefined && this.provider.unload !== undefined) {
this.provider.unload(); this.provider.unload();
...@@ -401,6 +422,7 @@ ProviderManager.prototype.set = function(provider) { ...@@ -401,6 +422,7 @@ ProviderManager.prototype.set = function(provider) {
this.ready = true; this.ready = true;
}; };
/// resends queued messages
ProviderManager.prototype.sendQueued = function() { ProviderManager.prototype.sendQueued = function() {
for(var i = 0; this.queued.length; i++) { for(var i = 0; this.queued.length; i++) {
// Resend // Resend
...@@ -408,10 +430,13 @@ ProviderManager.prototype.sendQueued = function() { ...@@ -408,10 +430,13 @@ ProviderManager.prototype.sendQueued = function() {
} }
}; };
/// @returns true if the provider i properly set
ProviderManager.prototype.installed = function() { ProviderManager.prototype.installed = function() {
return this.provider !== undefined; return this.provider !== undefined;
}; };
/// this method is only used, when we do not have native qt bindings and have to do polling on our own
/// should be callled, on start watching for eth/shh changes
ProviderManager.prototype.startPolling = function (data, pollId) { ProviderManager.prototype.startPolling = function (data, pollId) {
if (!this.provider || !this.provider.poll) { if (!this.provider || !this.provider.poll) {
return; return;
...@@ -419,6 +444,7 @@ ProviderManager.prototype.startPolling = function (data, pollId) { ...@@ -419,6 +444,7 @@ ProviderManager.prototype.startPolling = function (data, pollId) {
this.polls.push({data: data, id: pollId}); this.polls.push({data: data, id: pollId});
}; };
/// should be called to stop polling for certain watch changes
ProviderManager.prototype.stopPolling = function (pollId) { ProviderManager.prototype.stopPolling = function (pollId) {
for (var i = this.polls.length; i--;) { for (var i = this.polls.length; i--;) {
var poll = this.polls[i]; var poll = this.polls[i];
...@@ -436,10 +462,13 @@ web3.setProvider = function(provider) { ...@@ -436,10 +462,13 @@ web3.setProvider = function(provider) {
web3.provider.sendQueued(); web3.provider.sendQueued();
}; };
/// returns true if provider is installed
web3.haveProvider = function() { web3.haveProvider = function() {
return !!web3.provider.provider; return !!web3.provider.provider;
}; };
/// should be used when we want to watch something
/// it's using inner polling mechanism and is notified about changes
var Filter = function(options, impl) { var Filter = function(options, impl) {
this.impl = impl; this.impl = impl;
this.callbacks = []; this.callbacks = [];
...@@ -453,10 +482,12 @@ var Filter = function(options, impl) { ...@@ -453,10 +482,12 @@ var Filter = function(options, impl) {
}); });
}; };
/// alias for changed*
Filter.prototype.arrived = function(callback) { Filter.prototype.arrived = function(callback) {
this.changed(callback); this.changed(callback);
}; };
/// gets called when there is new eth/shh message
Filter.prototype.changed = function(callback) { Filter.prototype.changed = function(callback) {
var self = this; var self = this;
this.promise.then(function(id) { this.promise.then(function(id) {
...@@ -464,12 +495,14 @@ Filter.prototype.changed = function(callback) { ...@@ -464,12 +495,14 @@ Filter.prototype.changed = function(callback) {
}); });
}; };
/// trigger calling new message from people
Filter.prototype.trigger = function(messages) { Filter.prototype.trigger = function(messages) {
for(var i = 0; i < this.callbacks.length; i++) { for(var i = 0; i < this.callbacks.length; i++) {
this.callbacks[i].call(this, messages); this.callbacks[i].call(this, messages);
} }
}; };
/// should be called to uninstall current filter
Filter.prototype.uninstall = function() { Filter.prototype.uninstall = function() {
var self = this; var self = this;
this.promise.then(function (id) { this.promise.then(function (id) {
...@@ -479,6 +512,7 @@ Filter.prototype.uninstall = function() { ...@@ -479,6 +512,7 @@ Filter.prototype.uninstall = function() {
}); });
}; };
/// should be called to manually trigger getting latest messages from the client
Filter.prototype.messages = function() { Filter.prototype.messages = function() {
var self = this; var self = this;
return this.promise.then(function (id) { return this.promise.then(function (id) {
...@@ -486,10 +520,12 @@ Filter.prototype.messages = function() { ...@@ -486,10 +520,12 @@ Filter.prototype.messages = function() {
}); });
}; };
/// alias for messages
Filter.prototype.logs = function () { Filter.prototype.logs = function () {
return this.messages(); return this.messages();
}; };
/// callled when there is new incoming message
function messageHandler(data) { function messageHandler(data) {
if(data._event !== undefined) { if(data._event !== undefined) {
web3.trigger(data._event, data._id, data.data); web3.trigger(data._event, data._id, data.data);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment