Code coverage report for browserify-middleware/lib/dynamic-cache.js

Statements: 67.74% (42 / 62)      Branches: 45.83% (11 / 24)      Functions: 75% (12 / 16)      Lines: 68.97% (40 / 58)      Ignored: none     

All files » browserify-middleware/lib/ » dynamic-cache.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    1   1 64       64 32       1   1 5 5 5 5     1 28 28 28 27 20 20 40 20 27   20     20                 20 40 40                             40         1 33 33   33 64 64 64 64 64       33 12                   1 33    
'use strict';
 
var fs = require('fs');
 
function objectValues(obj) {
  Iif (typeof obj !== 'object') {
    return [];
  }
 
  return Object.keys(obj).map(function (key) {
    return obj[key];
  });
}
 
module.exports = DynamicCache;
 
function DynamicCache() {
  this._queue = [];
  this._cache = {};
  this._files = {};
  this._time = {};
}
 
DynamicCache.prototype.update = function (cb) {
  var files = Object.keys(this._time);
  var remaining = files.length;
  if (remaining === 0) return cb();
  if (this._queue.length) return this._queue.push(cb);
  this._queue.push(cb);
  var endOne = function () {
    if (0 === --remaining) {
      for (var i = 0; i < this._queue.length; i++) {
        this._queue[i]();
      }
      this._queue = [];
    }
  }.bind(this);
  var getDeps = function (filename) {
    if (this._files[filename]) {
      return [[filename]].concat(this._files[filename].map(getDeps)).reduce(function (a, b) {
        return a.concat(b);
      });
    } else {
      return [filename];
    }
  }.bind(this);
  files.forEach(function (filename) {
    fs.stat(filename, function (err, stats) {
      Iif (err || stats.mtime.getTime() !== this._time[filename]) {
        var deps = getDeps(filename);
        for (var i = 0; i < deps.length; i++) {
          var dep = deps[i];
          if (this._cache[dep]) {
            delete this._cache[dep];
          }
        }
        if (filename in this._files) {
          delete this._files[filename];
        }
        if (filename in this._time) {
          delete this._time[filename];
        }
      }
      endOne();
    }.bind(this))
  }.bind(this));
};
 
DynamicCache.prototype.populate = function (bundle) {
  this._files = {};
  this._time = {};
 
  bundle.on('dep', function (dep) {
    fs.stat(dep.file, function (err, stats) {
      Iif (err) return;
      this._cache[dep.id] = dep;
      this._files[dep.file] = objectValues(dep.deps);
      this._time[dep.file] = stats.mtime.getTime();
    }.bind(this));
  }.bind(this));
 
  bundle.on('transform', function (tr, file) {
    tr.on('file', function (dependency) {
      fs.stat(dependency, function (err, stats) {
        if (err) return;
        this._files[dependency] = objectValues(dependency.deps);
        this._time[dependency] = stats.mtime.getTime();
      }.bind(this));
    }.bind(this));
  }.bind(this));
};
 
DynamicCache.prototype.getCache = function () {
  return this._cache;
};