comparison Implab/PromiseExtensions.cs @ 205:8200ab154c8a v2

Added ResetState to RunnableComponent to reset in case of failure Added StateChanged event to IRunnable Renamed Promise.SUCCESS -> Promise.Success Added Promise.FromException Renamed Bundle -> PromiseAll in PromiseExtensions
author cin
date Tue, 25 Oct 2016 17:40:33 +0300
parents 822aab37b107
children 558f34b2fb50
comparison
equal deleted inserted replaced
203:4d9830a9bbb8 205:8200ab154c8a
1 using System.Threading; 1 using System.Threading;
2 using System; 2 using System;
3 using Implab.Diagnostics; 3 using Implab.Diagnostics;
4 using System.Collections.Generic; 4 using System.Collections.Generic;
5 using System.Linq;
5 6
6 namespace Implab { 7 namespace Implab {
7 public static class PromiseExtensions { 8 public static class PromiseExtensions {
8 public static IPromise<T> DispatchToCurrentContext<T>(this IPromise<T> that) { 9 public static IPromise<T> DispatchToCurrentContext<T>(this IPromise<T> that) {
9 Safe.ArgumentNotNull(that, "that"); 10 Safe.ArgumentNotNull(that, "that");
73 74
74 static void CancelByTimeoutCallback(object cookie) { 75 static void CancelByTimeoutCallback(object cookie) {
75 ((ICancellable)cookie).Cancel(new TimeoutException()); 76 ((ICancellable)cookie).Cancel(new TimeoutException());
76 } 77 }
77 78
78 /// <summary> 79 /// <summary>
79 /// Cancells promise after the specified timeout is elapsed. 80 /// Cancells promise after the specified timeout is elapsed.
80 /// </summary> 81 /// </summary>
81 /// <param name="that">The promise to cancel on timeout.</param> 82 /// <param name="that">The promise to cancel on timeout.</param>
82 /// <param name="milliseconds">The timeout in milliseconds.</param> 83 /// <param name="milliseconds">The timeout in milliseconds.</param>
83 /// <typeparam name="TPromise">The 1st type parameter.</typeparam> 84 /// <typeparam name="TPromise">The 1st type parameter.</typeparam>
86 var timer = new Timer(CancelByTimeoutCallback, that, milliseconds, -1); 87 var timer = new Timer(CancelByTimeoutCallback, that, milliseconds, -1);
87 that.On(timer.Dispose, PromiseEventType.All); 88 that.On(timer.Dispose, PromiseEventType.All);
88 return that; 89 return that;
89 } 90 }
90 91
91 public static IPromise Bundle(this ICollection<IPromise> that) { 92 public static IPromise PromiseAll(this IEnumerable<IPromise> that) {
93 Safe.ArgumentNotNull(that, "that");
94 return PromiseAll(that.ToList());
95 }
96
97 public static IPromise<T[]> PromiseAll<T>(this IEnumerable<IPromise<T>> that) {
98 Safe.ArgumentNotNull(that, "that");
99 return PromiseAll(that.ToList());
100 }
101
102 public static IPromise PromiseAll(this ICollection<IPromise> that) {
92 Safe.ArgumentNotNull(that, "that"); 103 Safe.ArgumentNotNull(that, "that");
93 104
94 int count = that.Count; 105 int count = that.Count;
95 int errors = 0; 106 int errors = 0;
96 var medium = new Promise(); 107 var medium = new Promise();
126 ); 137 );
127 138
128 return medium; 139 return medium;
129 } 140 }
130 141
131 public static IPromise<T[]> Bundle<T>(this ICollection<IPromise<T>> that) { 142 public static IPromise<T[]> PromiseAll<T>(this ICollection<IPromise<T>> that) {
132 Safe.ArgumentNotNull(that, "that"); 143 Safe.ArgumentNotNull(that, "that");
133 144
134 int count = that.Count; 145 int count = that.Count;
135 int errors = 0; 146 int errors = 0;
136 var medium = new Promise<T[]>(); 147 var medium = new Promise<T[]>();