#if UNITY
using System;
using System.Collections;
using UnityEngine;
namespace Foundation.Tasks
{
public static class TaskExtensions
{
///
/// will throw if faulted
///
///
public static T ThrowIfFaulted(this T self) where T : AsyncTask
{
if (self.IsFaulted)
throw self.Exception;
return self;
}
///
/// Waits for the task to complete
///
public static T ContinueWith(this T self, Action continuation) where T : AsyncTask
{
if (self.IsCompleted)
{
continuation(self);
}
else
{
self.AddContinue(continuation);
}
return self;
}
///
/// Adds a timeout to the task. Will raise an exception if still running
///
///
///
///
///
///
public static T AddTimeout(this T self, int seconds, Action onTimeout = null) where T : AsyncTask
{
TaskManager.StartRoutine(TimeOutAsync(self, seconds, onTimeout));
return self;
}
static IEnumerator TimeOutAsync(AsyncTask task, int seconds, Action onTimeout = null)
{
yield return new WaitForSeconds(seconds);
if (task.IsRunning)
{
if (onTimeout != null)
{
onTimeout(task);
}
task.Complete(new Exception("Timeout"));
}
}
}
}
#endif