public void Bind <T> (IFuture <T> future) { Reset(); future.OnComplete(v => { successes += 1; }, e => { failures += 1; }); future.OnComplete(r => { completes += 1; }); }
/// <summary> /// Instructs the future to react with the specified action when the future is canceled. /// </summary> /// <param name="future">The future to subscribe to.</param> /// <param name="onCancel">The on error reaction.</param> /// <returns>This future.</returns> public static IFuture OnCancel(this IFuture future, Action onCancel) { ArgumentAssert.IsNotNull(future, "future"); ArgumentAssert.IsNotNull(onCancel, "onCancel"); return(future.OnComplete(() => { if (future.Status == FutureStatus.Canceled) { onCancel(); } })); }
/// <summary> /// Instructs the future to react with the specified action when the future completes successfully. /// </summary> /// <param name="future">The future to subscribe to.</param> /// <param name="onSuccess">The on success reaction.</param> /// <returns>This future.</returns> public static IFuture OnSuccess(this IFuture future, Action onSuccess) { ArgumentAssert.IsNotNull(future, "future"); ArgumentAssert.IsNotNull(onSuccess, "onSuccess"); return(future.OnComplete(() => { if (future.Status == FutureStatus.Success) { onSuccess(); } })); }
/// <summary> /// Instructs the future to react with the specified error action. /// </summary> /// <param name="future">The future to subscribe to.</param> /// <param name="onError">The on error reaction.</param> /// <returns>This future.</returns> public static IFuture OnError(this IFuture future, Action <Exception> onError) { ArgumentAssert.IsNotNull(future, "future"); ArgumentAssert.IsNotNull(onError, "onError"); return(future.OnComplete(() => { if (future.Status == FutureStatus.Failure) { onError(future.Error); } })); }
/// <summary> /// Instructs the future to react with the specified action when the future completes successfully. /// </summary> /// <param name="future">The future to subscribe to.</param> /// <param name="onSuccess">The on success reaction.</param> /// <returns>This future.</returns> public static IFuture <T> OnSuccess <T>(this IFuture <T> future, Action <T> onSuccess) { ArgumentAssert.IsNotNull(future, "future"); ArgumentAssert.IsNotNull(onSuccess, "onSuccess"); future.OnComplete(() => { if (future.Status == FutureStatus.Success) { onSuccess(future.Result); } }); return(future); }
/// <summary> /// Catches any errors in the source future and handles them with the specified handler. /// If <paramref name="handler"/> throws an error the result will complete with that error. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="source">The source.</param> /// <param name="handler">The handler.</param> /// <returns>A result that will catch errors from <paramref name="source"/> with <paramref name="handler"/>.</returns> public static Future <T> Catch <T>(this IFuture <T> source, Func <Exception, T> handler) { var result = new Future <T>(); source.OnComplete(() => { if (source.Status == FutureStatus.Failure) { try { result.SetResult(handler(source.Error)); } catch (Exception ex) { result.SetError(ex); } return; } source.CopyTo(result); }); return(result); }