From 8b7d4b0c9ed7113b685ecbae646b9d7df5a89aca Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Tue, 13 Jan 2015 17:05:05 +0100 Subject: [PATCH] beginning of comments in web3 --- lib/web3.js | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/lib/web3.js b/lib/web3.js index 85f2f9065..a27940f84 100644 --- a/lib/web3.js +++ b/lib/web3.js @@ -23,6 +23,9 @@ * @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) { if (obj instanceof Promise) { return Promise.resolve(obj); @@ -62,12 +65,14 @@ function flattenPromise (obj) { return Promise.resolve(obj); } +/// @returns an array of objects describing web3 api methods var web3Methods = function () { return [ { name: 'sha3', call: 'web3_sha3' } ]; }; +/// @returns an array of objects describing web3.eth api methods var ethMethods = function () { var blockCall = function (args) { return typeof args[0] === "string" ? "eth_blockByHash" : "eth_blockByNumber"; @@ -101,6 +106,7 @@ var ethMethods = function () { return methods; }; +/// @returns an array of objects describing web3.eth api properties var ethProperties = function () { return [ { name: 'coinbase', getter: 'eth_coinbase', setter: 'eth_setCoinbase' }, @@ -115,6 +121,7 @@ var ethProperties = function () { ]; }; +/// @returns an array of objects describing web3.db api methods var dbMethods = function () { return [ { name: 'put', call: 'db_put' }, @@ -124,6 +131,7 @@ var dbMethods = function () { ]; }; +/// @returns an array of objects describing web3.shh api methods var shhMethods = function () { return [ { name: 'post', call: 'shh_post' }, @@ -134,6 +142,7 @@ var shhMethods = function () { ]; }; +/// @returns an array of objects describing web3.eth.watch api methods var ethWatchMethods = function () { var newFilter = function (args) { return typeof args[0] === 'string' ? 'eth_newFilterString' : 'eth_newFilter'; @@ -146,6 +155,7 @@ var ethWatchMethods = function () { ]; }; +/// @returns an array of objects describing web3.shh.watch api methods var shhWatchMethods = function () { return [ { name: 'newFilter', call: 'shh_newFilter' }, @@ -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) { methods.forEach(function (method) { obj[method.name] = function () { @@ -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) { properties.forEach(function (property) { var proto = {}; @@ -221,7 +235,7 @@ var decToHex = function (dec) { return parseInt(dec).toString(16); }; - +/// setups web3 object, and it's in-browser executed methods var web3 = { _callbacks: {}, _events: {}, @@ -339,6 +353,7 @@ var web3 = { } }; +/// setups all api methods setupMethods(web3, web3Methods()); setupMethods(web3.eth, ethMethods()); setupProperties(web3.eth, ethProperties()); @@ -348,12 +363,16 @@ setupMethods(web3.shh, shhMethods()); var ethWatch = { changed: 'eth_changed' }; + setupMethods(ethWatch, ethWatchMethods()); + var shhWatch = { changed: 'shh_changed' }; + setupMethods(shhWatch, shhWatchMethods()); +/// Provider manager object prototype var ProviderManager = function() { this.queued = []; this.polls = []; @@ -375,6 +394,7 @@ var ProviderManager = function() { poll(); }; +/// sends outgoing requests, if provider is not available, enqueue the request ProviderManager.prototype.send = function(data, cb) { data._id = this.id; if (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) { if(this.provider !== undefined && this.provider.unload !== undefined) { this.provider.unload(); @@ -401,6 +422,7 @@ ProviderManager.prototype.set = function(provider) { this.ready = true; }; +/// resends queued messages ProviderManager.prototype.sendQueued = function() { for(var i = 0; this.queued.length; i++) { // Resend @@ -408,10 +430,13 @@ ProviderManager.prototype.sendQueued = function() { } }; +/// @returns true if the provider i properly set ProviderManager.prototype.installed = function() { 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) { if (!this.provider || !this.provider.poll) { return; @@ -419,6 +444,7 @@ ProviderManager.prototype.startPolling = function (data, pollId) { this.polls.push({data: data, id: pollId}); }; +/// should be called to stop polling for certain watch changes ProviderManager.prototype.stopPolling = function (pollId) { for (var i = this.polls.length; i--;) { var poll = this.polls[i]; @@ -436,10 +462,13 @@ web3.setProvider = function(provider) { web3.provider.sendQueued(); }; +/// returns true if provider is installed web3.haveProvider = function() { 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) { this.impl = impl; this.callbacks = []; @@ -453,10 +482,12 @@ var Filter = function(options, impl) { }); }; +/// alias for changed* Filter.prototype.arrived = function(callback) { this.changed(callback); }; +/// gets called when there is new eth/shh message Filter.prototype.changed = function(callback) { var self = this; this.promise.then(function(id) { @@ -464,12 +495,14 @@ Filter.prototype.changed = function(callback) { }); }; +/// trigger calling new message from people Filter.prototype.trigger = function(messages) { for(var i = 0; i < this.callbacks.length; i++) { this.callbacks[i].call(this, messages); } }; +/// should be called to uninstall current filter Filter.prototype.uninstall = function() { var self = this; this.promise.then(function (id) { @@ -479,6 +512,7 @@ Filter.prototype.uninstall = function() { }); }; +/// should be called to manually trigger getting latest messages from the client Filter.prototype.messages = function() { var self = this; return this.promise.then(function (id) { @@ -486,10 +520,12 @@ Filter.prototype.messages = function() { }); }; +/// alias for messages Filter.prototype.logs = function () { return this.messages(); }; +/// callled when there is new incoming message function messageHandler(data) { if(data._event !== undefined) { web3.trigger(data._event, data._id, data.data);