public SubscriptionsPageViewModel( )
{
var timer = new Timer( 1000 ) {AutoReset = false};
timer.Elapsed += ( sender, args ) => ChunkMessages();
NewMessageArrived += (sender, args) => { if (!timer.Enabled) timer.Start(); };
}
public static event EventHandler? NewMessageArrived;
public ViewModelCollection<ReceivedApplicationMessageViewModel> ReceivedApplicationMessages { get; } = new ();
public ViewModelCollection<ReceivedApplicationMessageViewModel> ReceivedApplicationMessagesChunk { get; } = new();
public Task HandleApplicationMessageReceivedAsync( MqttApplicationMessageReceivedEventArgs eventArgs )
{
return Dispatcher.UIThread.InvokeAsync( () =>
{
if ( ReceivedApplicationMessages.Count >= 10000 )
{
ReceivedApplicationMessages.Clear();
_messageId = 0;
}
ReceivedApplicationMessagesChunk.Insert( 0, new ReceivedApplicationMessageViewModel(
_messageId++,
eventArgs.ApplicationMessage ) );
NewMessageArrived?.Invoke(null, EventArgs.Empty);
} );
}
private void ChunkMessages()
{
ReceivedApplicationMessages.AddOrInsertRange( ReceivedApplicationMessagesChunk, 0 );
ReceivedApplicationMessagesChunk.Clear();
}