using System;
using System.Runtime.CompilerServices;
using UnityEngine;
namespace Unity.Services.Core
{
///
/// Base class for asynchronous operations.
///
/// Implemented by: TaskAsyncOperation
///
abstract class AsyncOperationBase : CustomYieldInstruction, IAsyncOperation, INotifyCompletion
{
///
/// Indicates if coroutine should be kept suspended.
///
/// From CustomYieldInstruction.
///
public override bool keepWaiting => !IsCompleted;
///
/// Whether this operation is completed.
///
/// Required to make the operation awaitable
///
public abstract bool IsCompleted { get; }
///
/// If true, this operation either succeeded, failed, or has been canceled.
///
/// From IAsyncOperation
///
public bool IsDone => IsCompleted;
///
/// The current status of this operation.
///
/// From IAsyncOperation
///
public abstract AsyncOperationStatus Status { get; }
///
/// The exception that occured during the operation if it failed.
///
/// From IAsyncOperation
///
public abstract Exception Exception { get; }
///
/// Result of the operation.
///
/// Required to make the operation awaitable
///
public abstract void GetResult();
///
/// Awaiter on the operation.
///
/// Required to make the operation awaitable
///
public abstract AsyncOperationBase GetAwaiter();
Action m_CompletedCallback;
///
/// Event raised when the operation succeeded or failed.
/// The argument is the operation that raised the event.
///
/// From IAsyncOperation
///
public event Action Completed
{
add
{
if (IsDone)
{
value(this);
}
else
{
m_CompletedCallback += value;
}
}
remove => m_CompletedCallback -= value;
}
protected void DidComplete()
{
m_CompletedCallback?.Invoke(this);
}
/// Schedules the continuation action that's invoked when the instance completes.
/// The action to invoke when the operation completes.
///
/// From INotifyCompletion
public virtual void OnCompleted(Action continuation)
{
Completed += op => continuation?.Invoke();
}
}
///
/// Base class for asynchronous operations.
///
/// Implemented by: TaskAsyncOperation
///
///
/// The type of this operation's result
///
abstract class AsyncOperationBase : CustomYieldInstruction, IAsyncOperation, INotifyCompletion
{
///
/// Indicates if coroutine should be kept suspended.
///
/// From CustomYieldInstruction.
///
public override bool keepWaiting => !IsCompleted;
///
/// Whether this operation is completed.
///
/// Required to make the operation awaitable
///
public abstract bool IsCompleted { get; }
///
/// If true, this operation either succeeded, failed, or has been canceled.
///
/// From IAsyncOperation
///
public bool IsDone => IsCompleted;
///
/// The current status of this operation.
///
/// From IAsyncOperation
///
public abstract AsyncOperationStatus Status { get; }
///
/// The exception that occured during the operation if it failed.
///
/// From IAsyncOperation
///
public abstract Exception Exception { get; }
///
/// Result of the operation.
///
/// Required to make the operation awaitable
///
public abstract T Result { get; }
///
/// Awaiter on the operation.
///
/// Required to make the operation awaitable
///
public abstract T GetResult();
///
/// Awaiter on the operation.
///
/// Required to make the operation awaitable
///
public abstract AsyncOperationBase GetAwaiter();
Action> m_CompletedCallback;
///
/// Event raised when the operation succeeded or failed.
/// The argument is the operation that raised the event.
///
/// From IAsyncOperation
///
public event Action> Completed
{
add
{
if (IsDone)
{
value(this);
}
else
{
m_CompletedCallback += value;
}
}
remove => m_CompletedCallback -= value;
}
protected void DidComplete()
{
m_CompletedCallback?.Invoke(this);
}
/// Schedules the continuation action that's invoked when the instance completes.
/// The action to invoke when the operation completes.
///
/// From INotifyCompletion
public virtual void OnCompleted(Action continuation)
{
Completed += op => continuation?.Invoke();
}
}
}