/// <summary> /// Initializes a new instance of the <see cref="CoTaskDecorator"/> class. /// </summary> /// <param name="coTask">The CoTask to decorate.</param> protected CoTaskDecorator(ICoTask coTask) { if (coTask == null) throw new ArgumentNullException(nameof(coTask)); _innerCoTask = coTask; }
/// <summary> /// Initializes a new instance of the <see cref="ContinueCoTaskDecorator"/> class. /// </summary> /// <param name="coTask">The CoTask to decorate.</param> /// <param name="coroutine">The coroutine to execute when <paramref name="coTask"/> was canceled.</param> public ContinueCoTaskDecorator(ICoTask coTask, Func<ICoTask> coroutine) : base(coTask) { if (coroutine == null) throw new ArgumentNullException(nameof(coroutine)); _coroutine = coroutine; }
/// <summary> /// Initializes a new instance of the <see cref="CoTaskDecorator"/> class. /// </summary> /// <param name="coTask">The CoTask to decorate.</param> protected CoTaskDecorator(ICoTask coTask) { if (coTask is null) { throw new ArgumentNullException(nameof(coTask)); } _innerCoTask = coTask; }
/// <summary> /// Initializes a new instance of the <see cref="RescueCoTaskDecorator<TException>"/> class. /// </summary> /// <param name="coTask">The CoTask to decorate.</param> /// <param name="coroutine">The rescue coroutine.</param> /// <param name="cancelCoTask">Set to true to cancel the CoTask after executing rescue coroutine.</param> public RescueCoTaskDecorator(ICoTask coTask, Func <TException, ICoTask> coroutine, bool cancelCoTask = true) : base(coTask) { if (coroutine == null) { throw new ArgumentNullException(nameof(coroutine)); } _coroutine = coroutine; _cancelCoTask = cancelCoTask; }
private static Task <TResult> InternalExecuteAsync <TResult>(ICoTask coTask, CoroutineExecutionContext context) { var taskSource = new TaskCompletionSource <TResult>(); EventHandler <CoTaskCompletedEventArgs> completed = null; completed = (s, e) => { ((ICoTask)s).Completed -= completed; if (e.Error != null) { taskSource.TrySetException(e.Error); } else if (e.WasCancelled) { taskSource.TrySetCanceled(); } else { var rr = s as ICoTask <TResult>; taskSource.TrySetResult(rr != null ? rr.Result : default(TResult)); } }; try { coTask.Completed += completed; coTask.BeginExecute(context ?? new CoroutineExecutionContext()); } catch (Exception ex) { coTask.Completed -= completed; taskSource.TrySetException(ex); } return(taskSource.Task); }
/// <summary> /// Executes an <see cref="ICoTask<TResult>"/> asynchronous. /// </summary> /// <typeparam name="TResult">The type of the result.</typeparam> /// <param name="coTask">The coroutine to execute.</param> /// <param name="context">The context to execute the coroutine within.</param> /// <returns>A task that represents the asynchronous coroutine.</returns> public static Task <TResult> ExecuteAsync <TResult>(this ICoTask <TResult> coTask, CoroutineExecutionContext context = null) { return(InternalExecuteAsync <TResult>(coTask, context)); }
/// <summary> /// Initializes a new instance of the <see cref="OverrideCancelCoTaskDecorator"/> class. /// </summary> /// <param name="coTask">The CoTask to decorate.</param> public OverrideCancelCoTaskDecorator(ICoTask coTask) : base(coTask) { }
/// <summary> /// Rescues <typeparamref name="TException"/> from the decorated <paramref name="coTask"/> by executing a <paramref name="rescue"/> coroutine. /// </summary> /// <typeparam name = "TException">The type of the exception we want to perform the rescue on.</typeparam> /// <param name="coTask">The CoTask to decorate.</param> /// <param name="rescue">The rescue coroutine.</param> /// <param name="cancelCoTask">Set to true to cancel the CoTask after executing rescue.</param> /// <returns></returns> public static ICoTask Rescue <TException>(this ICoTask coTask, Func <TException, ICoTask> rescue, bool cancelCoTask = true) where TException : Exception { return(new RescueCoTaskDecorator <TException>(coTask, rescue, cancelCoTask)); }
/// <summary> /// Rescues any exception from the decorated <paramref name="coTask"/> by executing a <paramref name="rescue"/> coroutine. /// </summary> /// <param name="coTask">The CoTask to decorate.</param> /// <param name="rescue">The rescue coroutine.</param> /// <param name="cancelCoTask">Set to true to cancel the CoTask after executing rescue.</param> /// <returns></returns> public static ICoTask Rescue(this ICoTask coTask, Func <Exception, ICoTask> rescue, bool cancelCoTask = true) { return(Rescue <Exception>(coTask, rescue, cancelCoTask)); }
/// <summary> /// Overrides <see cref="CoTaskCompletedEventArgs.WasCancelled"/> of the decorated <paramref name="coTask"/> instance. /// </summary> /// <typeparam name="TResult">The type of the result.</typeparam> /// <param name="coTask">The CoTask to decorate.</param> /// <param name="cancelResult">The canceled result value.</param> /// <returns></returns> public static ICoTask <TResult> OverrideCancel <TResult>(this ICoTask <TResult> coTask, TResult cancelResult = default(TResult)) { return(new OverrideCancelCoTaskDecorator <TResult>(coTask, cancelResult)); }
/// <summary> /// Executes an <see cref="ICoTask"/> asynchronous. /// </summary> /// <param name="coTask">The coroutine to execute.</param> /// <param name="context">The context to execute the coroutine within.</param> /// <returns>A task that represents the asynchronous coroutine.</returns> public static Task ExecuteAsync(this ICoTask coTask, CoroutineExecutionContext context = null) { return(InternalExecuteAsync <object>(coTask, context)); }
/// <summary> /// Adds behavior to the CoTask which is executed when the <paramref name ="coTask"/> was cancelled. /// </summary> /// <param name="coTask">The CoTask to decorate.</param> /// <param name="coroutine">The coroutine to execute when <paramref name="coTask"/> was canceled.</param> /// <returns></returns> public static ICoTask WhenCancelled(this ICoTask coTask, Func <ICoTask> coroutine) { return(new ContinueCoTaskDecorator(coTask, coroutine)); }
/// <summary> /// Overrides <see cref="CoTaskCompletedEventArgs.WasCancelled"/> of the decorated <paramref name="coTask"/> instance. /// </summary> /// <param name="coTask">The CoTask to decorate.</param> /// <returns></returns> public static ICoTask OverrideCancel(this ICoTask coTask) { return(new OverrideCancelCoTaskDecorator(coTask)); }
/// <summary> /// Called when the execution of the decorated CoTask has completed. /// </summary> /// <param name="context">The context.</param> /// <param name="innerCoTask">The decorated CoTask.</param> /// <param name="args">The <see cref="CoTaskCompletedEventArgs"/> instance containing the event data.</param> protected abstract void OnInnerResultCompleted(CoroutineExecutionContext context, ICoTask innerCoTask, CoTaskCompletedEventArgs args);
/// <summary> /// Called when the execution of the decorated CoTask has completed. /// </summary> /// <param name="context">The context.</param> /// <param name="innerCoTask">The decorated CoTask.</param> /// <param name="args">The <see cref="Caliburn.Coroutines.CoTaskCompletedEventArgs" /> instance containing the event data.</param> protected override void OnInnerResultCompleted(CoroutineExecutionContext context, ICoTask innerCoTask, CoTaskCompletedEventArgs args) { OnCompleted(new CoTaskCompletedEventArgs(args.Error, false)); }
/// <summary> /// Called when the execution of the decorated CoTask has completed. /// </summary> /// <param name="context">The context.</param> /// <param name="innerCoTask">The decorated CoTask.</param> /// <param name="args">The <see cref="CoTaskCompletedEventArgs" /> instance containing the event data.</param> protected override void OnInnerResultCompleted(CoroutineExecutionContext context, ICoTask innerCoTask, CoTaskCompletedEventArgs args) { if (args.Error != null || !args.WasCancelled) { OnCompleted(new CoTaskCompletedEventArgs(args.Error, false)); } else { Continue(context); } }
/// <summary> /// Called when the execution of the decorated CoTask has completed. /// </summary> /// <param name="context">The context.</param> /// <param name="innerCoTask">The decorated CoTask.</param> /// <param name="args">The <see cref="Caliburn.Light.CoTaskCompletedEventArgs" /> instance containing the event data.</param> protected override void OnInnerResultCompleted(CoroutineExecutionContext context, ICoTask innerCoTask, CoTaskCompletedEventArgs args) { OnCompleted(new CoTaskCompletedEventArgs(args.Error, false)); }
/// <summary> /// Called when the execution of the decorated CoTask has completed. /// </summary> /// <param name="context">The context.</param> /// <param name="innerCoTask">The decorated coTask.</param> /// <param name="args">The <see cref="Caliburn.Light.CoTaskCompletedEventArgs" /> instance containing the event data.</param> protected override void OnInnerResultCompleted(CoroutineExecutionContext context, ICoTask innerCoTask, CoTaskCompletedEventArgs args) { var error = args.Error as TException; if (error == null) { OnCompleted(args); } else { Rescue(context, error); } }