Code coverage report for lib/changesHandler.js

Statements: 93.22% (55 / 59)      Branches: 85.71% (24 / 28)      Functions: 92.86% (13 / 14)      Lines: 93.22% (55 / 59)      Ignored: 10 statements, 3 functions, 10 branches     

All files » lib/ » changesHandler.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    2 2 2 2 2 2   2     1 1 1   1   1     1 1 1 1     1 1           1 1 1   1   2 179     179 179 1 709     709 98 98   611 611         611 565 565 565     610 60 60     610         179 179     2 179     179           2     4010 1 4010 1       2 4010 4010     2
'use strict';
 
var EventEmitter = require('events').EventEmitter;
var inherits = require('inherits');
var isChromeApp = require('./deps/env/isChromeApp');
var hasLocalStorage = require('./deps/env/hasLocalStorage');
var pick = require('./deps/pick');
var call = require('./deps/call');
 
inherits(Changes, EventEmitter);
 
/* istanbul ignore next */
function attachBrowserEvents(self) {
  Iif (isChromeApp()) {
    chrome.storage.onChanged.addListener(function (e) {
      // make sure it's event addressed to us
      if (e.db_name != null) {
        //object only has oldValue, newValue members
        self.emit(e.dbName.newValue);
      }
    });
  } else Iif (hasLocalStorage()) {
    if (typeof addEventListener !== 'undefined') {
      addEventListener("storage", function (e) {
        self.emit(e.key);
      });
    } else { // old IE
      window.attachEvent("storage", function (e) {
        self.emit(e.key);
      });
    }
  }
}
 
function Changes() {
  EventEmitter.call(this);
  this._listeners = {};
 
  attachBrowserEvents(this);
}
Changes.prototype.addListener = function (dbName, id, db, opts) {
  Iif (this._listeners[id]) {
    return;
  }
  var self = this;
  var inprogress = false;
  function eventFunction() {
    Iif (!self._listeners[id]) {
      return;
    }
    if (inprogress) {
      inprogress = 'waiting';
      return;
    }
    inprogress = true;
    var changesOpts = pick(opts, [
      'style', 'include_docs', 'attachments', 'conflicts', 'filter',
      'doc_ids', 'view', 'since', 'query_params', 'binary'
    ]);
 
    db.changes(changesOpts).on('change', function (c) {
      Eif (c.seq > opts.since && !opts.cancelled) {
        opts.since = c.seq;
        call(opts.onChange, c);
      }
    }).on('complete', function () {
      if (inprogress === 'waiting') {
        setTimeout(function(){
          eventFunction();
        },0);
      }
      inprogress = false;
    }).on('error', function () {
      inprogress = false;
    });
  }
  this._listeners[id] = eventFunction;
  this.on(dbName, eventFunction);
};
 
Changes.prototype.removeListener = function (dbName, id) {
  Iif (!(id in this._listeners)) {
    return;
  }
  EventEmitter.prototype.removeListener.call(this, dbName,
    this._listeners[id]);
};
 
 
/* istanbul ignore next */
Changes.prototype.notifyLocalWindows = function (dbName) {
  //do a useless change on a storage thing
  //in order to get other windows's listeners to activate
  Iif (isChromeApp()) {
    chrome.storage.local.set({dbName: dbName});
  } else Iif (hasLocalStorage()) {
    localStorage[dbName] = (localStorage[dbName] === "a") ? "b" : "a";
  }
};
 
Changes.prototype.notify = function (dbName) {
  this.emit(dbName);
  this.notifyLocalWindows(dbName);
};
 
module.exports = Changes;