using VideoDownloader.Browser.Tabs;
namespace VideoDownloader.App.Browser.Features;
///
/// 탭 절전(T2-2, Edge Sleeping Tabs 패턴): 유휴 비활성 탭을 TrySuspendAsync 로
/// 서스펜드해 메모리를 회수한다. 실제 서스펜드 수행은 주입(suspendAction) —
/// WebView2 TrySuspendAsync 는 뷰가 숨겨진(비활성) 탭에서만 동작.
///
public sealed class TabSuspendService
{
private readonly TimeSpan _idleTimeout;
private readonly Func _suspendAction;
private readonly Func _canSuspend;
public TabSuspendService(TimeSpan idleTimeout, Func suspendAction, Func canSuspend)
{
_idleTimeout = idleTimeout;
_suspendAction = suspendAction;
_canSuspend = canSuspend;
}
/// 유휴 비활성 탭 서스펜드. 반환: 서스펜드된 탭 수.
public int SuspendIdleTabs(IEnumerable tabs, TabItem active, DateTime now)
{
var count = 0;
foreach (var tab in tabs)
{
if (ReferenceEquals(tab, active)) continue;
if (tab.IsSuspended) continue;
if (!_canSuspend(tab)) continue;
if (now - tab.LastActivated < _idleTimeout) continue;
try
{
if (_suspendAction(tab)) { tab.IsSuspended = true; count++; }
}
catch { /* 서스펜드 실패는 건너뜀 */ }
}
return count;
}
}