Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Files skipped during the "match" test loop #11

Open
tixelated opened this issue Jun 1, 2011 · 1 comment
Open

Files skipped during the "match" test loop #11

tixelated opened this issue Jun 1, 2011 · 1 comment

Comments

@tixelated
Copy link

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
@tixelated
Copy link
Author

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();
        });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

0 participants