From 2f8a085adfb89ce696f6eb1212a63d9287da46fc Mon Sep 17 00:00:00 2001 From: Joseph-Ramos-CMU Date: Wed, 16 Dec 2020 12:04:07 -0500 Subject: [PATCH] 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. --- osu.Game/Online/Chat/ChannelManager.cs | 56 ++++++++++++++++---------- 1 file changed, 35 insertions(+), 21 deletions(-) diff --git a/osu.Game/Online/Chat/ChannelManager.cs b/osu.Game/Online/Chat/ChannelManager.cs index 303696c294..1bbd2c3471 100644 --- a/osu.Game/Online/Chat/ChannelManager.cs +++ b/osu.Game/Online/Chat/ChannelManager.cs @@ -445,33 +445,47 @@ namespace osu.Game.Online.Chat return; } - string lastClosedChannelName = closedChannels.Last(); - closedChannels.RemoveAt(closedChannels.Count - 1); - - // types did not work with existing enumerable interfaces funcitons like Contains - for (int i = 0; i < joinedChannels.Count; i++) + // 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--) { - // If the user already joined the channel, try the next - // channel in the list - if (joinedChannels[i].Name == lastClosedChannelName) + string lastClosedChannelName = closedChannels.Last(); + closedChannels.RemoveAt(closedChannels.Count - 1); + bool alreadyJoined = false; + // If the user already joined the channel, do not + // try to join it + for (int j = 0; j < joinedChannels.Count; j++) { - JoinLastClosedChannel(); - return; + if (joinedChannels[j].Name == lastClosedChannelName) + { + alreadyJoined = true; + break; + } + } + + if (alreadyJoined == false) + { + Channel lastClosedChannel = AvailableChannels.FirstOrDefault(c => c.Name == lastClosedChannelName); + + if (lastClosedChannel == null) + { + continue; + } + else + { + JoinChannel(lastClosedChannel); + return; + } } } - - Channel lastClosedChannel = AvailableChannels.FirstOrDefault(c => c.Name == lastClosedChannelName); - - if (lastClosedChannel == null) - { - JoinLastClosedChannel(); - } - else - { - JoinChannel(lastClosedChannel); - } } + + private long lastMessageId; private bool channelsInitialised;