Skip to content

Commit

Permalink
Fixing a performance problem in message display
Browse files Browse the repository at this point in the history
  • Loading branch information
rupertbenbrook-aa authored and meee1 committed Nov 17, 2023
1 parent 0ae563d commit 48e3ac5
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 32 deletions.
13 changes: 10 additions & 3 deletions ExtLibs/AltitudeAngelWings.Plugin/ControlOverlayMessageDisplay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,22 @@ public void RemoveMessage(Message message)
if (!_parent.Visible) return;
_parent.SuspendLayout();
var labels = GetMessageLabels();
var removed = false;
foreach (var label in labels)
{
if (label.Tag != message) continue;
_parent.Controls.Remove(label);
labels.Remove(label);
label.Dispose();
removed = true;
break;
}
LayoutLabels(labels);
if (removed)
{
LayoutLabels(labels);
}
_parent.ResumeLayout();
});
}
Expand Down Expand Up @@ -110,7 +117,7 @@ private static Color GetColorForMessage(Message message)
}
}

private void LayoutLabels(ICollection<Label> labels)
private void LayoutLabels(IEnumerable<Label> labels)
{
var totalHeight = 0;
foreach (var label in labels)
Expand All @@ -122,4 +129,4 @@ private void LayoutLabels(ICollection<Label> labels)

private IList<Label> GetMessageLabels() => _parent.Controls.OfType<Label>().Where(l => l.Tag is Message).ToList();
}
}
}
4 changes: 2 additions & 2 deletions ExtLibs/AltitudeAngelWings/ObservableProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace AltitudeAngelWings
{
public class ObservableProperty<T> : INotifyPropertyChanged, IObservable<T>, IDisposable
{
private readonly ISubject<T> _subject;
private readonly ReplaySubject<T> _subject;
private T _value;

public ObservableProperty()
Expand Down Expand Up @@ -68,7 +68,7 @@ protected virtual void Dispose(bool disposing)
{
if (disposing)
{
((IDisposable)_subject)?.Dispose();
_subject?.Dispose();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ namespace AltitudeAngelWings.Service.Messaging
{
public interface IMessagesService
{
ObservableProperty<Message> Messages { get; }
Task AddMessageAsync(Message message);
}
}
41 changes: 15 additions & 26 deletions ExtLibs/AltitudeAngelWings/Service/Messaging/MessagesService.cs
Original file line number Diff line number Diff line change
@@ -1,43 +1,32 @@
using System;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Threading.Tasks;
using AltitudeAngelWings.Model;

namespace AltitudeAngelWings.Service.Messaging
{
public class MessagesService : IMessagesService, IDisposable
public class MessagesService : IMessagesService
{
private readonly CompositeDisposable _disposer = new CompositeDisposable();
private readonly IMessageDisplay _messageDisplay;

public MessagesService(IMessageDisplay messageDisplay)
{
Messages = new ObservableProperty<Message>(0);
_disposer.Add(Messages);
_disposer.Add(Messages
.Do(messageDisplay.AddMessage)
.SelectMany(m => Observable.Interval(TimeSpan.FromMilliseconds(100))
.SkipWhile(i => !m.HasExpired())
.Select(i => m))
.Subscribe(messageDisplay.RemoveMessage));
_messageDisplay = messageDisplay;
}

public ObservableProperty<Message> Messages { get; }

public Task AddMessageAsync(Message message) => Task.Factory.StartNew(() => Messages.Value = message);

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
public Task AddMessageAsync(Message message) => Task.Factory.StartNew(async () =>
{
if (disposing)
try
{
_disposer?.Dispose();
_messageDisplay.AddMessage(message);
do
{
await Task.Delay(TimeSpan.FromMilliseconds(200)).ConfigureAwait(false);
} while (!message.HasExpired());
}
}
finally
{
_messageDisplay.RemoveMessage(message);
}
});
}
}

0 comments on commit 48e3ac5

Please sign in to comment.