-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixing a performance problem in message display
- Loading branch information
1 parent
0ae563d
commit 48e3ac5
Showing
4 changed files
with
27 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 15 additions & 26 deletions
41
ExtLibs/AltitudeAngelWings/Service/Messaging/MessagesService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); | ||
} | ||
} |