Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add settable Menu.Timeout #120

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 59 additions & 29 deletions src/Disqord.Extensions.Interactivity/Menus/MenuBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,58 @@ public ViewBase? View
}
private ViewBase? _view;

/// <summary>
/// Gets or sets the timeout of this menu.
/// </summary>
public TimeSpan Timeout
{
get => _timeout;
set
{
if (_timeout == value)
{
if (value == System.Threading.Timeout.InfiniteTimeSpan)
{
return;
}

RefreshTimeout();
}
else
{
lock (_disposeLock)
{
_timeout = value;
if (value == System.Threading.Timeout.InfiniteTimeSpan)
{
_timeoutTimer?.Dispose();
return;
}

_timeoutTimer = new Timer(TimerCallback, this, value, System.Threading.Timeout.InfiniteTimeSpan);
return;

static void TimerCallback(object? state)
{
var menu = Unsafe.As<MenuBase>(state)!;
lock (menu._disposeLock)
{
if (!menu.IsRunning)
return;

var cts = menu._cts;
if (cts == null)
return;

cts.Cancel();
menu._tcs!.Cancel(cts.Token);
}
}
}
}
}
}

private Tcs? _tcs;
private Cts? _cts;
private TimeSpan _timeout;
Expand Down Expand Up @@ -168,14 +220,14 @@ protected virtual void ValidateView()
/// Refreshes the timeout of this menu.
/// By default, is called by <see cref="HandleInteractionAsync"/>.
/// </summary>
protected void RefreshTimeout()
public void RefreshTimeout()
{
if (!IsRunning)
return;

lock (_disposeLock)
{
_timeoutTimer?.Change(_timeout, Timeout.InfiniteTimeSpan);
_timeoutTimer?.Change(_timeout, System.Threading.Timeout.InfiniteTimeSpan);
}
}

Expand Down Expand Up @@ -367,38 +419,16 @@ internal void Start(TimeSpan timeout, CancellationToken cancellationToken)
_tcs = new Tcs();
_cts = Cts.Linked(Client.StoppingToken, cancellationToken);

static void CancellationCallback(object? state, CancellationToken cancellationToken)
{
var tcs = Unsafe.As<Tcs>(state)!;
tcs.Cancel(cancellationToken);
}

_cts.Token.UnsafeRegister(CancellationCallback, _tcs);

if (timeout == Timeout.InfiniteTimeSpan)
return;
Timeout = timeout;
return;

// We store the timeout so it can be refreshed when a button is triggered in HandleInteractionAsync.
_timeout = timeout;

static void TimerCallback(object? state)
static void CancellationCallback(object? state, CancellationToken cancellationToken)
{
var menu = Unsafe.As<MenuBase>(state)!;
lock (menu._disposeLock)
{
if (!menu.IsRunning)
return;

var cts = menu._cts;
if (cts == null)
return;

cts.Cancel();
menu._tcs!.Cancel(cts.Token);
}
var tcs = Unsafe.As<Tcs>(state)!;
tcs.Cancel(cancellationToken);
}

_timeoutTimer = new Timer(TimerCallback, this, timeout, Timeout.InfiniteTimeSpan);
}

/// <summary>
Expand Down
Loading