using System;
using System.Collections;
namespace Unity.Services.Core
{
///
/// Default implementation for .
///
class AsyncOperation : IAsyncOperation
{
///
public bool IsDone { get; protected set; }
///
public AsyncOperationStatus Status { get; protected set; }
///
public event Action Completed
{
add
{
if (IsDone)
value(this);
else
m_CompletedCallback += value;
}
remove => m_CompletedCallback -= value;
}
///
public Exception Exception { get; protected set; }
///
protected Action m_CompletedCallback;
///
/// Set this operation's status .
///
public void SetInProgress()
{
Status = AsyncOperationStatus.InProgress;
}
///
/// Complete this operation as a success.
///
public void Succeed()
{
if (IsDone)
{
return;
}
IsDone = true;
Status = AsyncOperationStatus.Succeeded;
m_CompletedCallback?.Invoke(this);
m_CompletedCallback = null;
}
///
/// Complete this operation as a failure using the given .
///
///
/// The exception at the source of the failure.
///
public void Fail(Exception reason)
{
if (IsDone)
{
return;
}
Exception = reason;
IsDone = true;
Status = AsyncOperationStatus.Failed;
m_CompletedCallback?.Invoke(this);
m_CompletedCallback = null;
}
///
/// Complete this operation as a cancellation.
///
public void Cancel()
{
if (IsDone)
{
return;
}
Exception = new OperationCanceledException();
IsDone = true;
Status = AsyncOperationStatus.Cancelled;
m_CompletedCallback?.Invoke(this);
m_CompletedCallback = null;
}
///
bool IEnumerator.MoveNext() => !IsDone;
///
///
/// Left empty because we don't support operation reset.
///
void IEnumerator.Reset() {}
///
object IEnumerator.Current => null;
}
///
/// Default implementation for .
///
///
/// The result's type of this operation.
///
class AsyncOperation : IAsyncOperation
{
///
public bool IsDone { get; protected set; }
///
public AsyncOperationStatus Status { get; protected set; }
///
public event Action> Completed
{
add
{
if (IsDone)
value(this);
else
m_CompletedCallback += value;
}
remove => m_CompletedCallback -= value;
}
///
public Exception Exception { get; protected set; }
///
public T Result { get; protected set; }
///
protected Action> m_CompletedCallback;
///
/// Set this operation's status .
///
public void SetInProgress()
{
Status = AsyncOperationStatus.InProgress;
}
///
/// Complete this operation as a success and set its result.
///
///
/// The result of this operation.
///
public void Succeed(T result)
{
if (IsDone)
{
return;
}
Result = result;
IsDone = true;
Status = AsyncOperationStatus.Succeeded;
m_CompletedCallback?.Invoke(this);
m_CompletedCallback = null;
}
///
/// Complete this operation as a failure using the given .
///
///
/// The exception at the source of the failure.
///
public void Fail(Exception reason)
{
if (IsDone)
{
return;
}
Exception = reason;
IsDone = true;
Status = AsyncOperationStatus.Failed;
m_CompletedCallback?.Invoke(this);
m_CompletedCallback = null;
}
///
/// Complete this operation as a cancellation.
///
public void Cancel()
{
if (IsDone)
{
return;
}
Exception = new OperationCanceledException();
IsDone = true;
Status = AsyncOperationStatus.Cancelled;
m_CompletedCallback?.Invoke(this);
m_CompletedCallback = null;
}
///
bool IEnumerator.MoveNext() => !IsDone;
///
///
/// Left empty because we don't support operation reset.
///
void IEnumerator.Reset() {}
///
object IEnumerator.Current => null;
}
}