Code coverage report for ethereum.js/lib/filter.js

Statements: 59.38% (19 / 32)      Branches: 62.5% (10 / 16)      Functions: 33.33% (3 / 9)      Lines: 59.38% (19 / 32)      Ignored: none     

All files » ethereum.js/lib/ » filter.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                                                      1 1                     1 1       1   1         1                               1 1         1 1 1 1                 1   1       1       1           1                   1    
/*
    This file is part of ethereum.js.
 
    ethereum.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.
 
    ethereum.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 ethereum.js.  If not, see <http://www.gnu.org/licenses/>.
*/
/** @file filter.js
 * @authors:
 *   Jeffrey Wilcke <jeff@ethdev.com>
 *   Marek Kotewicz <marek@ethdev.com>
 *   Marian Oancea <marian@ethdev.com>
 *   Gav Wood <g@ethdev.com>
 * @date 2014
 */
 
/// Should be called to check if filter implementation is valid
/// @returns true if it is, otherwise false
var implementationIsValid = function (i) {
    return !!i && 
        typeof i.newFilter === 'function' && 
        typeof i.getMessages === 'function' && 
        typeof i.uninstallFilter === 'function' &&
        typeof i.startPolling === 'function' &&
        typeof i.stopPolling === 'function';
};
 
/// This method should be called on options object, to verify deprecated properties && lazy load dynamic ones
/// @param should be string or object
/// @returns options string or object
var getOptions = function (options) {
    Iif (typeof options === 'string') {
        return options;
    } 
 
    options = options || {};
 
    Iif (options.topics) {
        console.warn('"topics" is deprecated, is "topic" instead');
    }
 
    // evaluate lazy properties
    return {
        to: options.to,
        topic: options.topic,
        earliest: options.earliest,
        latest: options.latest,
        max: options.max,
        skip: options.skip,
        address: options.address
    };
};
 
/// Should be used when we want to watch something
/// it's using inner polling mechanism and is notified about changes
/// @param options are filter options
/// @param implementation, an abstract polling implementation
/// @param formatter (optional), callback function which formats output before 'real' callback 
var filter = function(options, implementation, formatter) {
    Iif (!implementationIsValid(implementation)) {
        console.error('filter implemenation is invalid');
        return;
    }
 
    options = getOptions(options);
    var callbacks = [];
    var filterId = implementation.newFilter(options);
    var onMessages = function (messages) {
        messages.forEach(function (message) {
            messages = formatter ? formatter(message) : message;
            callbacks.forEach(function (callback) {
                callback(message);
            });
        });
    };
 
    implementation.startPolling(filterId, onMessages, implementation.uninstallFilter);
 
    var changed = function (callback) {
        callbacks.push(callback);
    };
 
    var messages = function () {
        return implementation.getMessages(filterId);
    };
    
    var uninstall = function (callback) {
        implementation.stopPolling(filterId);
        implementation.uninstallFilter(filterId);
        callbacks = [];
    };
 
    return {
        changed: changed,
        arrived: changed,
        happened: changed,
        messages: messages,
        logs: messages,
        uninstall: uninstall
    };
};
 
module.exports = filter;