//
// ▄▄▄▄ ▄▄▄▄ ▄▄▄▄
// ▄▄▄▄▄▄█ █▄▄▄▄▄█ █▄▄▄▄▄█ █
// █__ --█ █__ --█ ◄█ - █
// █▄▄▄▄▄█▄▄█▄▄▄▄▄█▄▄█▄▄█▄▄▄▄▄█
// ┍━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ━━━━ ━ ━┉ ┉ ┉
// │ Copyright (c) JP Dillingham.
// │
// │ This program is free software: you can redistribute it and/or modify
// │ it under the terms of the GNU Affero General Public License as published
// │ by the Free Software Foundation, version 3.
// │
// │ This program is distributed in the hope that it will be useful,
// │ but WITHOUT ANY WARRANTY; without even the implied warranty of
// │ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// │ GNU Affero General Public License for more details.
// │
// │ You should have received a copy of the GNU Affero General Public License
// │ along with this program. If not, see https://www.gnu.org/licenses/.
// │
// │ This program is distributed with Additional Terms pursuant to Section 7
// │ of the AGPLv3. See the LICENSE file in the root directory of this
// │ project for the complete terms and conditions.
// │
// │ https://slskd.org
// │
// ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ╌ ╌╌╌╌ ╌
// │ SPDX-FileCopyrightText: JP Dillingham
// │ SPDX-License-Identifier: AGPL-3.0-only
// ╰───────────────────────────────────────────╶──── ─ ─── ─ ── ──┈ ┈
//
namespace slskd
{
using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
///
/// Enables arbitrary await-able things.
///
public interface IWaiter : IDisposable
{
///
/// Gets the default timeout duration.
///
int DefaultTimeout { get; }
///
/// Cancels the oldest wait matching the specified .
///
/// The unique WaitKey for the wait.
void Cancel(WaitKey key);
///
/// Cancels all waits.
///
void CancelAll();
///
/// Completes the oldest wait matching the specified with the specified .
///
/// The wait result type.
/// The unique WaitKey for the wait.
/// The wait result.
void Complete(WaitKey key, T result);
///
/// Completes the oldest wait matching the specified .
///
/// The unique WaitKey for the wait.
void Complete(WaitKey key);
///
/// Returns a value indicating whether the specified is being waited upon.
///
/// The unique WaitKey to check.
/// A value indicating whether the specified key is being waited upon.
bool IsWaitingFor(WaitKey key);
///
/// Throws the specified on the oldest wait matching the specified .
///
/// The unique WaitKey for the wait.
/// The Exception to throw.
void Throw(WaitKey key, Exception exception);
///
/// Causes the oldest wait matching the specified to time out.
///
/// The unique WaitKey for the wait.
void Timeout(WaitKey key);
///
/// Adds a new wait for the specified and with the specified .
///
/// The wait result type.
/// A unique WaitKey for the wait.
/// The wait timeout.
/// The cancellation token for the wait.
/// A Task representing the wait.
Task Wait(WaitKey key, int? timeout = null, CancellationToken? cancellationToken = null);
///
/// Adds a new wait for the specified and with the specified .
///
/// A unique WaitKey for the wait.
/// The wait timeout.
/// The cancellation token for the wait.
/// A Task representing the wait.
Task Wait(WaitKey key, int? timeout = null, CancellationToken? cancellationToken = null);
///
/// Adds a new wait for the specified which does not time out.
///
/// The wait result type.
/// A unique WaitKey for the wait.
/// The cancellation token for the wait.
/// A Task representing the wait.
Task WaitIndefinitely(WaitKey key, CancellationToken? cancellationToken = null);
///
/// Adds a new wait for the specified which does not time out.
///
/// A unique WaitKey for the wait.
/// The cancellation token for the wait.
/// A Task representing the wait.
Task WaitIndefinitely(WaitKey key, CancellationToken? cancellationToken = null);
}
///
/// Enables arbitrary await-able things.
///
public sealed class Waiter : IWaiter
{
private const int DefaultTimeoutValue = 5000;
///
/// Initializes a new instance of the class with the default timeout.
///
public Waiter()
: this(DefaultTimeoutValue)
{
}
///
/// Initializes a new instance of the class with the specified .
///
/// The default timeout duration for message waits.
public Waiter(int defaultTimeout)
{
DefaultTimeout = defaultTimeout;
}
///
/// Gets the default timeout duration, in milliseconds.
///
public int DefaultTimeout { get; private set; }
private bool Disposed { get; set; }
private ConcurrentDictionary Locks { get; } = new ConcurrentDictionary();
private ConcurrentDictionary> Waits { get; } = new ConcurrentDictionary>();
///
/// Cancels the oldest wait matching the specified .
///
/// The unique WaitKey for the wait.
public void Cancel(WaitKey key)
{
Disposition(key, wait =>
wait.TaskCompletionSource.TrySetCanceled());
}
///
/// Cancels all waits.
///
public void CancelAll()
{
var keys = Waits.Keys.ToList();
foreach (var key in keys)
{
Cancel(key);
}
}
///
/// Completes the oldest wait matching the specified with the specified .
///
/// The wait result type.
/// The unique WaitKey for the wait.
/// The wait result.
public void Complete(WaitKey key, T result)
{
Disposition(key, wait =>
((TaskCompletionSource)wait.TaskCompletionSource).TrySetResult(result));
}
///
/// Completes the oldest wait matching the specified .
///
/// The unique WaitKey for the wait.
public void Complete(WaitKey key)
{
Complete