Reworked reopening last tab to no longer use recursion

A reviewer of the pull request was concerned about recursion. I changed
the code to be iterative.
This commit is contained in:
Joseph-Ramos-CMU 2020-12-16 12:04:07 -05:00
parent 7b169c4f62
commit 2f8a085adf

View File

@ -445,32 +445,46 @@ namespace osu.Game.Online.Chat
return; return;
} }
// This nested loop could be eliminated if a check was added so that
// when the code opens a channel it removes from the closedChannel list.
// However, this would require adding an O(|closeChannels|) work operation
// every time the user joins a channel, which would make joining a channel
// slower. I wanted to centralize all major slowdowns so they
// can only occur if the user actually decides to use this feature.
for (int i = closedChannels.Count - 1; i >= 0; i--)
{
string lastClosedChannelName = closedChannels.Last(); string lastClosedChannelName = closedChannels.Last();
closedChannels.RemoveAt(closedChannels.Count - 1); closedChannels.RemoveAt(closedChannels.Count - 1);
bool alreadyJoined = false;
// types did not work with existing enumerable interfaces funcitons like Contains // If the user already joined the channel, do not
for (int i = 0; i < joinedChannels.Count; i++) // try to join it
for (int j = 0; j < joinedChannels.Count; j++)
{ {
// If the user already joined the channel, try the next if (joinedChannels[j].Name == lastClosedChannelName)
// channel in the list
if (joinedChannels[i].Name == lastClosedChannelName)
{ {
JoinLastClosedChannel(); alreadyJoined = true;
return; break;
} }
} }
if (alreadyJoined == false)
{
Channel lastClosedChannel = AvailableChannels.FirstOrDefault(c => c.Name == lastClosedChannelName); Channel lastClosedChannel = AvailableChannels.FirstOrDefault(c => c.Name == lastClosedChannelName);
if (lastClosedChannel == null) if (lastClosedChannel == null)
{ {
JoinLastClosedChannel(); continue;
} }
else else
{ {
JoinChannel(lastClosedChannel); JoinChannel(lastClosedChannel);
return;
} }
} }
}
}
private long lastMessageId; private long lastMessageId;