Code coverage report for web3/lib/web3/requestmanager.js

Statements: 86.15% (56 / 65)      Branches: 65.79% (25 / 38)      Functions: 100% (13 / 13)      Lines: 86.15% (56 / 65)      Ignored: none     

All files » web3/lib/web3/ » requestmanager.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212                                                    1 1 1 1               1 156 156                   1 76 7 7     69 69   69       69                   1 75     75 75 72 1     71       71                     1 4       4 4 4       4       4                       1 9 9                                 1 12   12   12         10 10     10                         1 181     181 11   181     181 135 7 7 7                     1 24       24 6         24       1  
/*
    This file is part of web3.js.
 
    web3.js is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
 
    web3.js is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.
 
    You should have received a copy of the GNU Lesser General Public License
    along with web3.js.  If not, see <http://www.gnu.org/licenses/>.
*/
/** 
 * @file requestmanager.js
 * @author Jeffrey Wilcke <jeff@ethdev.com>
 * @author Marek Kotewicz <marek@ethdev.com>
 * @author Marian Oancea <marian@ethdev.com>
 * @author Fabian Vogelsteller <fabian@ethdev.com>
 * @author Gav Wood <g@ethdev.com>
 * @date 2014
 */
 
var Jsonrpc = require('./jsonrpc');
var utils = require('../utils/utils');
var c = require('../utils/config');
var errors = require('./errors');
 
/**
 * It's responsible for passing messages to providers
 * It's also responsible for polling the ethereum node for incoming messages
 * Default poll timeout is 1 second
 * Singleton
 */
var RequestManager = function (provider) {
    this.setProvider(provider);
    this.subscriptions = {};
};
 
/**
 * Should be used to synchronously send request
 *
 * @method send
 * @param {Object} data
 * @return {Object}
 */
RequestManager.prototype.send = function (data) {
    if (!this.provider) {
        console.error(errors.InvalidProvider());
        return null;
    }
 
    var payload = Jsonrpc.getInstance().toPayload(data.method, data.params);
    var result = this.provider.send(payload);
 
    Iif (!Jsonrpc.getInstance().isValidResponse(result)) {
        throw errors.InvalidResponse(result);
    }
 
    return result.result;
};
 
/**
 * Should be used to asynchronously send request
 *
 * @method sendAsync
 * @param {Object} data
 * @param {Function} callback
 */
RequestManager.prototype.sendAsync = function (data, callback) {
    Iif (!this.provider) {
        return callback(errors.InvalidProvider());
    }
    var payload = Jsonrpc.getInstance().toPayload(data.method, data.params);
    this.provider.sendAsync(payload, function (err, result) {        
        if (err) {
            return callback(err);
        }
 
        Iif (!Jsonrpc.getInstance().isValidResponse(result)) {
            return callback(errors.InvalidResponse(result));
        }
 
        callback(null, result.result);
    });
};
 
/**
 * Should be called to asynchronously send batch request
 *
 * @method sendBatch
 * @param {Array} batch data
 * @param {Function} callback
 */
RequestManager.prototype.sendBatch = function (data, callback) {
    Iif (!this.provider) {
        return callback(errors.InvalidProvider());
    }
 
    var payload = Jsonrpc.getInstance().toBatchPayload(data);
    this.provider.sendAsync(payload, function (err, results) {
        Iif (err) {
            return callback(err);
        }
 
        Iif (!utils.isArray(results)) {
            return callback(errors.InvalidResponse(results));
        }
 
        callback(err, results);
    }); 
};
 
 
/**
 * Waits for notifications
 *
 * @method addSubscription
 * @param {String} id           the subscription id
 * @param {Function} callback   the callback to call for incoming notifications
 */
RequestManager.prototype.addSubscription = function (type, id, callback) {
    Eif(this.provider.on) {
        this.subscriptions[id] = {
            callback: callback,
            type: type
        };
        
    } else {
        throw new Error('This provider doesn\'t support subscriptions', this.provider);
    }
};
 
/**
 * Waits for notifications
 *
 * @method removeSubscription
 * @param {String} id           the subscription id
 * @param {Function} callback   fired once the subscription is removed
 */
RequestManager.prototype.removeSubscription = function (id, callback) {
    var _this = this;
 
    Eif(this.subscriptions[id]) {
 
        this.sendAsync({
            method: this.subscriptions[id].type + '_unsubscribe',
            params: [id]
        }, function(err, result){
 
            Eif(!err) {
                delete _this.subscriptions[id];
            }
 
            Iif(utils.isFunction(callback))
                callback(err, result);
        });
 
    }
}
 
/**
 * Should be used to set provider of request manager
 *
 * @method setProvider
 * @param {Object}
 */
RequestManager.prototype.setProvider = function (p) {
    var _this = this;
 
    // reset the old one before changing
    if(this.provider)
        this.reset();
 
    this.provider = p;
 
    // listen to incoming notifications
    if(this.provider && this.provider.on) {
        this.provider.on('notification', function(err, result){
            Eif(!err) {
                Eif(_this.subscriptions[result.params.subscription] && _this.subscriptions[result.params.subscription].callback)
                    _this.subscriptions[result.params.subscription].callback(null, result.params.result);
            }
        });
    }
};
 
/**
 * Should be called to reset the polling mechanism of the request manager
 *
 * @method reset
 */
RequestManager.prototype.reset = function (keepIsSyncing) {
    var _this = this;
 
 
    // uninstall all subscriptions
    Object.keys(this.subscriptions).forEach(function(id){
        _this.removeSubscription(id);
    });
 
 
    //  reset notification callbacks etc.
    Iif(this.provider.reset)
        this.provider.reset();
};
 
module.exports = RequestManager;