First of sorry I'am not native english and thanks for interest! I don't know why when I perform some semi-heavy operations on arrays my browser break connection with Socket.io. Browser receives disconnect event, then it reconnects but shouldn't disconnect in the first place.
The code contains a couple nested loops, I know it's not the best practice but I did't figure out any other way of doing the task. I started testing on large arrays of users (about 10k) and the problem started occurring, so I guess it's a performance issue.
I was trying to find memory leeks but with no success, its almost like for the time of performing operations app freezes and breaks Socket.io.
Thank you for trying to help.
var pushUsersToCorrectLeagues = function(appConfig, callback, usersArray, newSeason, db) {
//determinate which league is the last one
var leaguesOrderArray = [];
_.each(newSeason.leagues, function(league) {
leaguesOrderArray.push(league.key);
return false;
});
var lastLeagueKey = _.last(leaguesOrderArray.sort(function(a, b) {
return a - b;
}));
_.each(usersArray, function(user) {
var keyUserData = {};
keyUserData.userLogin = user.userLogin;
keyUserData._id = user._id;
keyUserData.avatarUrl = user.avatarUrl;
keyUserData.isActive = true;
keyUserData.scores = {};
//now put users in correct leagues
var userPushed = false;
var endedInLeague = null;
//if found a user in league
_.each(newSeason.leagues, function(league) {
_.find(league.users, function(alredyInLeagueUser, i) {
if (alredyInLeagueUser.userLogin == user.userLogin) {
newSeason.leagues[league.key].users[i] = keyUserData;
userPushed = true;
endedInLeague = league.key;
return true;
}
});
return false;
});
//user wasnt found in any league, has to be pushed to the last one then
if (!userPushed) {
newSeason.leagues[lastLeagueKey].users.push(keyUserData);
endedInLeague = lastLeagueKey;
}
if (endedInLeague) {
process.nextTick(function() {
var callback = function(updatedUser) {
//console.log('updatedUser', updatedUser);
return false;
};
updateUserCurrentLeague(callback, user._id, endedInLeague, db);
});
}
return false;
});
callback(usersArray, newSeason);
return false;
};