github.com/jonasnick/go-ethereum@v0.7.12-0.20150216215225-22176f05d387/cmd/mist/assets/ext/ethereum.js/lib/providermanager.js (about)

     1  /*
     2      This file is part of ethereum.js.
     3  
     4      ethereum.js is free software: you can redistribute it and/or modify
     5      it under the terms of the GNU Lesser General Public License as published by
     6      the Free Software Foundation, either version 3 of the License, or
     7      (at your option) any later version.
     8  
     9      ethereum.js is distributed in the hope that it will be useful,
    10      but WITHOUT ANY WARRANTY; without even the implied warranty of
    11      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12      GNU Lesser General Public License for more details.
    13  
    14      You should have received a copy of the GNU Lesser General Public License
    15      along with ethereum.js.  If not, see <http://www.gnu.org/licenses/>.
    16  */
    17  /** @file providermanager.js
    18   * @authors:
    19   *   Jeffrey Wilcke <jeff@ethdev.com>
    20   *   Marek Kotewicz <marek@ethdev.com>
    21   *   Marian Oancea <marian@ethdev.com>
    22   *   Gav Wood <g@ethdev.com>
    23   * @date 2014
    24   */
    25  
    26  var web3 = require('./web3'); 
    27  var jsonrpc = require('./jsonrpc');
    28  
    29  
    30  /**
    31   * Provider manager object prototype
    32   * It's responsible for passing messages to providers
    33   * If no provider is set it's responsible for queuing requests
    34   * It's also responsible for polling the ethereum node for incoming messages
    35   * Default poll timeout is 12 seconds
    36   * If we are running ethereum.js inside ethereum browser, there are backend based tools responsible for polling,
    37   * and provider manager polling mechanism is not used
    38   */
    39  var ProviderManager = function() {
    40      this.polls = [];
    41      this.provider = undefined;
    42  
    43      var self = this;
    44      var poll = function () {
    45          self.polls.forEach(function (data) {
    46              var result = self.send(data.data);
    47  
    48              if (!(result instanceof Array) || result.length === 0) {
    49                  return;
    50              }
    51  
    52              data.callback(result);
    53          });
    54  
    55          setTimeout(poll, 1000);
    56      };
    57      poll();
    58  };
    59  
    60  /// sends outgoing requests
    61  /// @params data - an object with at least 'method' property
    62  ProviderManager.prototype.send = function(data) {
    63      var payload = jsonrpc.toPayload(data.method, data.params);
    64  
    65      if (this.provider === undefined) {
    66          console.error('provider is not set');
    67          return null; 
    68      }
    69  
    70      var result = this.provider.send(payload);
    71  
    72      if (!jsonrpc.isValidResponse(result)) {
    73          console.log(result);
    74          return null;
    75      }
    76  
    77      return result.result;
    78  };
    79  
    80  /// setups provider, which will be used for sending messages
    81  ProviderManager.prototype.set = function(provider) {
    82      this.provider = provider;
    83  };
    84  
    85  /// this method is only used, when we do not have native qt bindings and have to do polling on our own
    86  /// should be callled, on start watching for eth/shh changes
    87  ProviderManager.prototype.startPolling = function (data, pollId, callback) {
    88      this.polls.push({data: data, id: pollId, callback: callback});
    89  };
    90  
    91  /// should be called to stop polling for certain watch changes
    92  ProviderManager.prototype.stopPolling = function (pollId) {
    93      for (var i = this.polls.length; i--;) {
    94          var poll = this.polls[i];
    95          if (poll.id === pollId) {
    96              this.polls.splice(i, 1);
    97          }
    98      }
    99  };
   100  
   101  module.exports = ProviderManager;
   102