Reduce initial channel load overhead by only loading history on active channel

This commit is contained in:
Dean Herbert 2020-04-29 15:23:28 +09:00
parent 68fe3e12bc
commit c73d45bc01
2 changed files with 14 additions and 6 deletions

View File

@ -93,6 +93,12 @@ private void currentChannelChanged(ValueChangedEvent<Channel> e)
{
if (!(e.NewValue is ChannelSelectorTabItem.ChannelSelectorTabChannel))
JoinChannel(e.NewValue);
if (e.NewValue?.MessagesLoaded == false)
{
// let's fetch a small number of messages to bring us up-to-date with the backlog.
fetchInitalMessages(e.NewValue);
}
}
/// <summary>
@ -375,12 +381,6 @@ public Channel JoinChannel(Channel channel, bool alreadyJoined = false)
if (CurrentChannel.Value == null)
CurrentChannel.Value = channel;
if (!channel.MessagesLoaded)
{
// let's fetch a small number of messages to bring us up-to-date with the backlog.
fetchInitalMessages(channel);
}
return channel;
}

View File

@ -105,6 +105,14 @@ protected override void Dispose(bool isDisposing)
private void newMessagesArrived(IEnumerable<Message> newMessages)
{
if (newMessages.Min(m => m.Id) < chatLines.Max(c => c.Message.Id))
{
// there is a case (on initial population) that we may receive past messages and need to reorder.
// easiest way is to just combine messages and recreate drawables (less worrying about day separators etc.)
newMessages = newMessages.Concat(chatLines.Select(c => c.Message)).OrderBy(m => m.Id).ToList();
ChatLineFlow.Clear();
}
bool shouldScrollToEnd = scroll.IsScrolledToEnd(10) || !chatLines.Any() || newMessages.Any(m => m is LocalMessage);
// Add up to last Channel.MAX_HISTORY messages