namespace UnityEngine.XR.ARSubsystems
{
///
/// A Promise is used for operations that retrieve data asynchronously. Use this object
/// to determine the status of the operation (that is, whether it has completed), and
/// the resulting data.
///
///
/// Since derives from CustomYieldInstruction,
/// you can yield return on a Promise in a coroutine. If you prefer not
/// to use the Promise as a coroutine, you can manually check
/// to determine if the operation has completed. Once the operation is complete, you can get the
/// resulting value from .
///
///
/// Example usage in a coroutine:
///
/// IEnumerator MyCoroutine()
/// {
/// var promise = GetDataAsync();
/// yield return promise;
/// Debug.LogFormat("Operation complete. Result = {0}", promise.result);
/// }
///
///
/// The type of information the asynchronous operation retrieves.
public abstract class Promise : CustomYieldInstruction
{
bool m_Complete;
///
/// Will return as long as the operation has not yet completed.
///
public override bool keepWaiting
{
get
{
OnKeepWaiting();
return !m_Complete;
}
}
///
/// The result of the asynchronous operation. Not valid until returns .
///
public T result { get; private set; }
///
/// Creates a resolved promise (that is, a promise that is already complete).
///
/// The result of the operation.
/// A completed .
public static Promise CreateResolvedPromise(T result)
{
return new ImmediatePromise(result);
}
///
/// The creator of the should call this when the asynchronous operation completes.
///
/// The result of the asynchronous operation.
protected void Resolve(T result)
{
this.result = result;
m_Complete = true;
}
///
/// Invoked whenever is queried. Implement this to perform per-frame updates.
///
protected virtual void OnKeepWaiting() { }
class ImmediatePromise : Promise
{
public ImmediatePromise(T immediateResult)
{
Resolve(immediateResult);
}
}
}
}