mape / connect-assetmanager

Middleware for Connect (node.js) for handling your static assets.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Files skipped during the "match" test loop

opened this issue · comments

The following line 94 in assetmanager causes elements of the files array to be skipped if non-match elements are encountered:

group.files.splice(index, 1);

That line modifies the underlying array, and causes forEach to skip elements (on chrome, anyway.) Example:

var arr = [0,1,2,3,4]

arr.forEach(function(elem,index){
  if(elem === 1){
    arr.splice(index, 1);
  }
  else{
    console.log(elem);
  }
}

...yields:

0
3                 // '2' has been mistakenly skipped, because forEach's internal iterator isn't updated after the removal of '1'
4
undefined // forEach stupidly walks off the end of the now-shortened array

Here's an updated version of that section of code that fixed it for me - there's probably a better way to do this, but:


    }, function(err, contents) {
          settings.forEach(function (group, groupName) {
            if (!group.stale) {
              var spliceme = [];
              group.files.forEach(function (file, index) {
                if (!file.match) {
                  console.log('No match for: '+file);
                  spliceme.push(index);
                  return;
                }
                if (file.match(/^http:\/\//)) {
                  return;
                }
                fs.watchFile(group.path + file, function (old, newFile) {
                  if (old.mtime.toString() != newFile.mtime.toString()) {
                    self.generateCache(groupName);
                  }
                });
              });
              spliceme.forEach(function(elem, index){
                group.files.splice(elem, 1);
                spliceme.forEach(function(s_elem, s_index){
                  spliceme[s_index]--;
                });
              });
            }
          });
          self.generateCache();
        });