I have this bit of code that’s causing an error and i have no idea why it’s causing a maximum call stack error…
const toggleReady = socket => async (acknowledge=()=>{})=>{
acknowledge();
const lobby = getPlayersLobby(socket.id);
if(!lobby || lobby.players.length<2) return socket.emit('toggle-fail');
const player = lobby.players.find(p=>p.socketId===socket.id);
if(player){
player.ready = !player.ready;
}
if(lobby.players.reduce((acc,r)=>acc&&r.ready,true)){
startCountdown(lobby._id)
}
io.to('lobby-'+lobby._id).emit('upsert-lobby',lobby)
}
const startCountdown=lobbyId=>{
const lobby = lobbies[lobbyId];
lobby.startTimer = setTimeout(()=>{
// initialiseGame(lobby);
// delete lobbies[lobbyId];
// closeLobby(lobbyId)
},1000);
console.log(lobby.startTimer)
io.to('lobby-'+lobby._id).emit('starting-countdown');
}
I’ve found that if I change
if(lobby.players.reduce((acc,r)=>acc&&r.ready,true)){
startCountdown(lobby._id)
}
io.to('lobby-'+lobby._id).emit('upsert-lobby',lobby)
to:
io.to('lobby-'+lobby._id).emit('upsert-lobby',lobby)
if(lobby.players.reduce((acc,r)=>acc&&r.ready,true)){
startCountdown(lobby._id)
}
or if i change:
lobby.startTimer = setTimeout(()=>{
// initialiseGame(lobby);
// delete lobbies[lobbyId];
// closeLobby(lobbyId)
},1000);
to:
setTimeout(()=>{
// initialiseGame(lobby);
// delete lobbies[lobbyId];
// closeLobby(lobbyId)
},1000);
then i don’t get the error anymore, however that really makes no sense to me any help would be greatly appreciated.
edit: So the problem has something to do with emitting a timeout using socket.io, i’d still like to know more about whats going on though