Mercurial > pub > ImplabNet
comparison Implab/Promise.cs @ 260:547a2fc0d93e v3 v3.0.6
minor fixes
author | cin |
---|---|
date | Fri, 13 Apr 2018 19:14:59 +0300 |
parents | d82909310094 |
children |
comparison
equal
deleted
inserted
replaced
259:7d52dc684bbd | 260:547a2fc0d93e |
---|---|
1 using System; | 1 using System; |
2 using System.Collections.Generic; | 2 using System.Collections.Generic; |
3 using System.Diagnostics; | 3 using System.Diagnostics; |
4 using System.Reflection; | 4 using System.Reflection; |
5 using System.Threading; | |
5 using System.Threading.Tasks; | 6 using System.Threading.Tasks; |
6 using Implab.Parallels; | 7 using Implab.Parallels; |
7 | 8 |
8 namespace Implab { | 9 namespace Implab { |
9 public class Promise : AbstractEvent<IResolvable>, IPromise { | 10 public class Promise : AbstractEvent<IResolvable>, IPromise { |
150 public static RejectedPromise<T> Reject<T>(Exception reason) { | 151 public static RejectedPromise<T> Reject<T>(Exception reason) { |
151 return new RejectedPromise<T>(reason); | 152 return new RejectedPromise<T>(reason); |
152 } | 153 } |
153 | 154 |
154 public static IPromise Create(PromiseExecutor executor) { | 155 public static IPromise Create(PromiseExecutor executor) { |
156 return Create(executor, CancellationToken.None); | |
157 } | |
158 | |
159 public static IPromise Create(PromiseExecutor executor, CancellationToken ct) { | |
155 Safe.ArgumentNotNull(executor, nameof(executor)); | 160 Safe.ArgumentNotNull(executor, nameof(executor)); |
156 | 161 if (!ct.CanBeCanceled) |
157 var p = new Promise(); | 162 return Create(executor); |
158 var d = new Deferred(p, DefaultDispatcher); | 163 |
159 | 164 var d = new Deferred(); |
165 | |
166 ct.Register(d.Cancel); | |
167 | |
160 try { | 168 try { |
161 executor(d); | 169 if (!ct.IsCancellationRequested) |
162 } catch (Exception e) { | 170 executor(d); |
171 } catch(Exception e) { | |
163 d.Reject(e); | 172 d.Reject(e); |
164 } | 173 } |
165 | |
166 return d.Promise; | 174 return d.Promise; |
167 } | 175 } |
168 | 176 |
169 public static IPromise<T> Create<T>(PromiseExecutor<T> executor) { | 177 public static IPromise<T> Create<T>(PromiseExecutor<T> executor) { |
178 return Create(executor, CancellationToken.None); | |
179 } | |
180 | |
181 public static IPromise<T> Create<T>(PromiseExecutor<T> executor, CancellationToken ct) { | |
170 Safe.ArgumentNotNull(executor, nameof(executor)); | 182 Safe.ArgumentNotNull(executor, nameof(executor)); |
171 | 183 |
172 var d = new Deferred<T>(); | 184 var d = new Deferred<T>(); |
173 | 185 |
186 ct.Register(d.Cancel); | |
187 | |
174 try { | 188 try { |
175 executor(d); | 189 if (!ct.IsCancellationRequested) |
176 } catch (Exception e) { | 190 executor(d); |
191 } catch(Exception e) { | |
177 d.Reject(e); | 192 d.Reject(e); |
178 } | 193 } |
179 | |
180 return d.Promise; | 194 return d.Promise; |
181 } | 195 } |
182 | 196 |
183 public static IPromise All(IEnumerable<IPromise> promises) { | 197 public static IPromise All(IEnumerable<IPromise> promises) { |
184 var d = new Deferred(); | 198 var d = new Deferred(); |