Attempt to have a minute grace whenever collection get removed.
This commit is contained in:
@@ -70,7 +70,14 @@ internal sealed class PairHandlerAdapter : DisposableMediatorSubscriberBase, IPa
|
||||
private DateTime? _lastSuccessfulApplyAt;
|
||||
private string? _lastFailureReason;
|
||||
private IReadOnlyList<string> _lastBlockingConditions = Array.Empty<string>();
|
||||
private readonly object _visibilityGraceGate = new();
|
||||
private CancellationTokenSource? _visibilityGraceCts;
|
||||
private static readonly TimeSpan VisibilityEvictionGrace = TimeSpan.FromMinutes(1);
|
||||
private DateTime? _invisibleSinceUtc;
|
||||
private DateTime? _visibilityEvictionDueAtUtc;
|
||||
|
||||
public DateTime? InvisibleSinceUtc => _invisibleSinceUtc;
|
||||
public DateTime? VisibilityEvictionDueAtUtc => _visibilityEvictionDueAtUtc;
|
||||
public string Ident { get; }
|
||||
public bool Initialized { get; private set; }
|
||||
public bool ScheduledForDeletion { get; set; }
|
||||
@@ -80,24 +87,37 @@ internal sealed class PairHandlerAdapter : DisposableMediatorSubscriberBase, IPa
|
||||
get => _isVisible;
|
||||
private set
|
||||
{
|
||||
if (_isVisible != value)
|
||||
if (_isVisible == value) return;
|
||||
|
||||
_isVisible = value;
|
||||
|
||||
if (!_isVisible)
|
||||
{
|
||||
_isVisible = value;
|
||||
if (!_isVisible)
|
||||
{
|
||||
DisableSync();
|
||||
ResetPenumbraCollection(reason: "VisibilityLost");
|
||||
}
|
||||
else if (_charaHandler is not null && _charaHandler.Address != nint.Zero)
|
||||
{
|
||||
_ = EnsurePenumbraCollection();
|
||||
}
|
||||
var user = GetPrimaryUserData();
|
||||
Mediator.Publish(new EventMessage(new Event(PlayerName, user, nameof(PairHandlerAdapter),
|
||||
EventSeverity.Informational, "User Visibility Changed, now: " + (_isVisible ? "Is Visible" : "Is not Visible"))));
|
||||
Mediator.Publish(new RefreshUiMessage());
|
||||
Mediator.Publish(new VisibilityChange());
|
||||
DisableSync();
|
||||
|
||||
_invisibleSinceUtc = DateTime.UtcNow;
|
||||
_visibilityEvictionDueAtUtc = _invisibleSinceUtc.Value.Add(VisibilityEvictionGrace);
|
||||
|
||||
StartVisibilityGraceTask();
|
||||
}
|
||||
else
|
||||
{
|
||||
CancelVisibilityGraceTask();
|
||||
|
||||
_invisibleSinceUtc = null;
|
||||
_visibilityEvictionDueAtUtc = null;
|
||||
|
||||
ScheduledForDeletion = false;
|
||||
|
||||
if (_charaHandler is not null && _charaHandler.Address != nint.Zero)
|
||||
_ = EnsurePenumbraCollection();
|
||||
}
|
||||
|
||||
var user = GetPrimaryUserData();
|
||||
Mediator.Publish(new EventMessage(new Event(PlayerName, user, nameof(PairHandlerAdapter),
|
||||
EventSeverity.Informational, "User Visibility Changed, now: " + (_isVisible ? "Is Visible" : "Is not Visible"))));
|
||||
Mediator.Publish(new RefreshUiMessage());
|
||||
Mediator.Publish(new VisibilityChange());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -918,6 +938,46 @@ internal sealed class PairHandlerAdapter : DisposableMediatorSubscriberBase, IPa
|
||||
}
|
||||
}
|
||||
|
||||
private void CancelVisibilityGraceTask()
|
||||
{
|
||||
lock (_visibilityGraceGate)
|
||||
{
|
||||
_visibilityGraceCts?.CancelDispose();
|
||||
_visibilityGraceCts = null;
|
||||
}
|
||||
}
|
||||
|
||||
private void StartVisibilityGraceTask()
|
||||
{
|
||||
CancellationToken token;
|
||||
lock (_visibilityGraceGate)
|
||||
{
|
||||
_visibilityGraceCts = _visibilityGraceCts?.CancelRecreate() ?? new CancellationTokenSource();
|
||||
token = _visibilityGraceCts.Token;
|
||||
}
|
||||
|
||||
_visibilityGraceTask = Task.Run(async () =>
|
||||
{
|
||||
try
|
||||
{
|
||||
await Task.Delay(VisibilityEvictionGrace, token).ConfigureAwait(false);
|
||||
token.ThrowIfCancellationRequested();
|
||||
if (IsVisible) return;
|
||||
|
||||
ScheduledForDeletion = true;
|
||||
ResetPenumbraCollection(reason: "VisibilityLostTimeout");
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
// operation cancelled, do nothing
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.LogDebug(ex, "Visibility grace task failed for {handler}", GetLogIdentifier());
|
||||
}
|
||||
}, CancellationToken.None);
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
base.Dispose(disposing);
|
||||
@@ -936,7 +996,10 @@ internal sealed class PairHandlerAdapter : DisposableMediatorSubscriberBase, IPa
|
||||
_downloadCancellationTokenSource = null;
|
||||
_downloadManager.Dispose();
|
||||
_charaHandler?.Dispose();
|
||||
CancelVisibilityGraceTask();
|
||||
_charaHandler = null;
|
||||
_invisibleSinceUtc = null;
|
||||
_visibilityEvictionDueAtUtc = null;
|
||||
|
||||
if (!string.IsNullOrEmpty(name))
|
||||
{
|
||||
@@ -1265,6 +1328,7 @@ internal sealed class PairHandlerAdapter : DisposableMediatorSubscriberBase, IPa
|
||||
}
|
||||
|
||||
private Task? _pairDownloadTask;
|
||||
private Task _visibilityGraceTask;
|
||||
|
||||
private async Task DownloadAndApplyCharacterAsync(Guid applicationBase, CharacterData charaData, Dictionary<ObjectKind, HashSet<PlayerChanges>> updatedData,
|
||||
bool updateModdedPaths, bool updateManip, Dictionary<(string GamePath, string? Hash), string>? cachedModdedPaths, CancellationToken downloadToken)
|
||||
|
||||
Reference in New Issue
Block a user