/// <summary> /// Creates a continuation that executes when the target <see cref="CoroutineTask" /> completes. /// </summary> /// <param name="continuationFunction"> /// An action to run when the <see cref="CoroutineTask" /> completes. When run, the delegate will be /// passed the completed task as an argument. /// </param> /// <param name="state">The parameter of the action.</param> /// <param name="continuationOptions"> /// Options for when the continuation is scheduled and how it behaves. This includes criteria, such /// as <see cref="CoroutineTaskContinuationOptions.OnCanceled">OnCanceled</see> /// , as well as execution options, such as /// <see cref="CoroutineTaskContinuationOptions.OnCompleted">OnCompleted</see>. /// </param> /// <returns>A new continuation <see cref="CoroutineTask" />.</returns> /// <remarks> /// The returned <see cref="CoroutineTask" /> will not be scheduled for execution until the current task has /// completed. /// </remarks> public CoroutineTask <TResult> ContinueWith <TResult>( Func <CoroutineTask, object, IPromise <TResult>, IEnumerator> continuationFunction, object state, CoroutineTaskContinuationOptions continuationOptions = CoroutineTaskContinuationOptions.None) { var result = new AsyncResult <TResult>(true); asyncResult.Callbackable().OnCallback(ar => { try { var executable = IsExecutable(ar, continuationOptions); if (!executable) { result.SetCancelled(); return; } Executors.RunOnCoroutine(continuationFunction(this, state, result), result); } catch (Exception e) { result.SetException(e); } }); return(new CoroutineTask <TResult>(result)); }
/// <summary> /// Creates a continuation that executes when the target <see cref="CoroutineTask" /> completes. /// </summary> /// <param name="continuationFunction"> /// An action to run when the <see cref="CoroutineTask" /> completes. When run, the delegate will be /// passed the completed task as an argument. /// </param> /// <param name="state">The parameter of the action.</param> /// <param name="continuationOptions"> /// Options for when the continuation is scheduled and how it behaves. This includes criteria, such /// as <see cref="CoroutineTaskContinuationOptions.OnCanceled">OnCanceled</see> /// , as well as execution options, such as /// <see cref="CoroutineTaskContinuationOptions.OnCompleted">OnCompleted</see>. /// </param> /// <returns>A new continuation <see cref="CoroutineTask" />.</returns> /// <remarks> /// The returned <see cref="CoroutineTask" /> will not be scheduled for execution until the current task has /// completed. /// </remarks> public CoroutineTask <TResult> ContinueWith <TResult>(Func <CoroutineTask, object, TResult> continuationFunction, object state, CoroutineTaskContinuationOptions continuationOptions = CoroutineTaskContinuationOptions.None) { var result = new AsyncResult <TResult>(true); asyncResult.Callbackable().OnCallback(ar => { try { var executable = IsExecutable(ar, continuationOptions); if (!executable) { result.SetCancelled(); return; } TResult value = continuationFunction(this, state); result.SetResult(value); } catch (Exception e) { result.SetException(e); } }); return(new CoroutineTask <TResult>(result)); }
/// <summary> /// Creates a continuation that executes when the target <see cref="CoroutineTask" /> completes. /// </summary> /// <param name="continuationAction"> /// An action to run when the <see cref="CoroutineTask" /> completes. When run, the delegate will be /// passed the completed task as an argument. /// </param> /// <param name="state">The parameter of the action.</param> /// <param name="continuationOptions"> /// Options for when the continuation is scheduled and how it behaves. This includes criteria, such /// as <see cref="CoroutineTaskContinuationOptions.OnCanceled">OnCanceled</see> /// , as well as execution options, such as /// <see cref="CoroutineTaskContinuationOptions.OnCompleted">OnCompleted</see>. /// </param> /// <returns>A new continuation <see cref="CoroutineTask" />.</returns> /// <remarks> /// The returned <see cref="CoroutineTask" /> will not be scheduled for execution until the current task has /// completed. /// </remarks> public CoroutineTask ContinueWith(Action <CoroutineTask, object> continuationAction, object state, CoroutineTaskContinuationOptions continuationOptions = CoroutineTaskContinuationOptions.None) { AsyncResult result = new AsyncResult(true); asyncResult.Callbackable().OnCallback(ar => { try { var executable = IsExecutable(ar, continuationOptions); if (!executable) { result.SetCancelled(); return; } continuationAction(this, state); result.SetResult(); } catch (Exception e) { result.SetException(e); } }); return(new CoroutineTask(result)); }
/// <summary> /// Creates a continuation that executes when the target <see cref="CoroutineTask" /> completes. /// </summary> /// <param name="continuationFunction"> /// An action to run when the <see cref="CoroutineTask" /> completes. When run, the delegate will be /// passed the completed task as an argument. /// </param> /// <param name="continuationOptions"> /// Options for when the continuation is scheduled and how it behaves. This includes criteria, such /// as <see cref="CoroutineTaskContinuationOptions.OnCanceled">OnCanceled</see> /// , as well as execution options, such as /// <see cref="CoroutineTaskContinuationOptions.OnCompleted">OnCompleted</see>. /// </param> /// <returns>A new continuation <see cref="CoroutineTask" />.</returns> /// <remarks> /// The returned <see cref="CoroutineTask" /> will not be scheduled for execution until the current task has /// completed. /// </remarks> public CoroutineTask ContinueWith(Func <CoroutineTask, IEnumerator> continuationFunction, CoroutineTaskContinuationOptions continuationOptions = CoroutineTaskContinuationOptions.None) { AsyncResult result = new AsyncResult(true); asyncResult.Callbackable().OnCallback(ar => { try { var executable = IsExecutable(ar, continuationOptions); if (!executable) { result.SetCancelled(); return; } Executors.RunOnCoroutine(continuationFunction(this), result); } catch (Exception e) { result.SetException(e); } }); return(new CoroutineTask(result)); }
protected bool IsExecutable(IAsyncResult ar, CoroutineTaskContinuationOptions continuationOptions) { bool executable = (continuationOptions == CoroutineTaskContinuationOptions.None); if (!executable) { executable = (ar.Exception == null && (continuationOptions & CoroutineTaskContinuationOptions.OnCompleted) > 0); } if (!executable) { executable = (ar.IsCancelled && (continuationOptions & CoroutineTaskContinuationOptions.OnCanceled) > 0); } if (!executable) { executable = (!ar.IsCancelled && ar.Exception != null && (continuationOptions & CoroutineTaskContinuationOptions.OnFaulted) > 0); } return(executable); }