Skip to content

Commit

Permalink
added new processChatRoom function to reduce function nesting level d…
Browse files Browse the repository at this point in the history
…own from 4
  • Loading branch information
dominicteh1 committed Sep 12, 2024
1 parent 39e6ab3 commit 9628970
Showing 1 changed file with 27 additions and 19 deletions.
46 changes: 27 additions & 19 deletions src/upgrades/1.0.0/chat_room_hashes.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,36 @@ module.exports = {
if (err) {
return callback(err);
}
let currentChatRoomId = 1;
async.whilst((next) => {
next(null, currentChatRoomId <= nextChatRoomId);
}, (next) => {
db.getSortedSetRange(`chat:room:${currentChatRoomId}:uids`, 0, 0, (err, uids) => {
const currentChatRoomId = 1;
async.whilst(
(next) => {
next(null, currentChatRoomId <= nextChatRoomId);
},
(next) => {
processChatRoom(currentChatRoomId, next);
},
callback
);
});

function processChatRoom(currentChatRoomId, next) {
db.getSortedSetRange(`chat:room:${currentChatRoomId}:uids`, 0, 0, (err, uids) => {
if (err) {
return next(err);
}
if (!Array.isArray(uids) || !uids.length || !uids[0]) {
currentChatRoomId += 1;
return next();
}

db.setObject(`chat:room:${currentChatRoomId}`, { owner: uids[0], roomId: currentChatRoomId }, (err) => {
if (err) {
return next(err);
}
if (!Array.isArray(uids) || !uids.length || !uids[0]) {
currentChatRoomId += 1;
return next();
}

db.setObject(`chat:room:${currentChatRoomId}`, { owner: uids[0], roomId: currentChatRoomId }, (err) => {
if (err) {
return next(err);
}
currentChatRoomId += 1;
next();
});
currentChatRoomId += 1;
next();
});
}, callback);
});
});
}
},
};

0 comments on commit 9628970

Please sign in to comment.