- fix: memory leak part 1
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Nebula.Launcher.ProcessHelper;
|
||||
using Nebula.Launcher.ViewModels.Popup;
|
||||
using Nebula.Shared.Services;
|
||||
@@ -6,27 +8,53 @@ namespace Nebula.Launcher.Models;
|
||||
|
||||
public sealed class ContentLogConsumer : IProcessLogConsumer
|
||||
{
|
||||
private readonly LogPopupModelView _currLog;
|
||||
private readonly PopupMessageService _popupMessageService;
|
||||
private readonly List<string> _outMessages = [];
|
||||
|
||||
private LogPopupModelView? _currentLogPopup;
|
||||
|
||||
public int MaxMessages { get; set; } = 100;
|
||||
|
||||
public ContentLogConsumer(LogPopupModelView currLog, PopupMessageService popupMessageService)
|
||||
public void Popup(PopupMessageService popupMessageService)
|
||||
{
|
||||
_currLog = currLog;
|
||||
_popupMessageService = popupMessageService;
|
||||
if(_currentLogPopup is not null)
|
||||
return;
|
||||
|
||||
_currentLogPopup = new LogPopupModelView(popupMessageService);
|
||||
_currentLogPopup.OnDisposing += OnLogPopupDisposing;
|
||||
|
||||
foreach (var message in _outMessages.ToArray())
|
||||
{
|
||||
_currentLogPopup.Append(message);
|
||||
}
|
||||
|
||||
popupMessageService.Popup(_currentLogPopup);
|
||||
}
|
||||
|
||||
private void OnLogPopupDisposing(PopupViewModelBase obj)
|
||||
{
|
||||
if(_currentLogPopup == null)
|
||||
return;
|
||||
|
||||
_currentLogPopup.OnDisposing -= OnLogPopupDisposing;
|
||||
_currentLogPopup = null;
|
||||
}
|
||||
|
||||
public void Out(string text)
|
||||
{
|
||||
_currLog.Append(text);
|
||||
_outMessages.Add(text);
|
||||
if(_outMessages.Count >= MaxMessages)
|
||||
_outMessages.RemoveAt(0);
|
||||
|
||||
_currentLogPopup?.Append(text);
|
||||
}
|
||||
|
||||
public void Error(string text)
|
||||
{
|
||||
_currLog.Append(text);
|
||||
Out(text);
|
||||
}
|
||||
|
||||
public void Fatal(string text)
|
||||
{
|
||||
_popupMessageService.Popup("Fatal error while stop instance:" + text);
|
||||
throw new Exception("Error while running programm: " + text);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user