##// END OF EJS Templates
renamed Promise.Last -> Promise.On...
cin -
r104:5f10d54b45df v2
parent child
Show More
@@ -0,0 +1,19
1 using System;
2
3 namespace Implab {
4 [Flags]
5 public enum PromiseEventType {
6 Success = 1,
7 Error = 2,
8 Cancelled = 4,
9 /// <summary>
10 /// Завершено успешно, либо возникла ошибка,
11 /// </summary>
12 All = 7,
13 /// <summary>
14 /// Заврешено успешно, либо возникла ошибка.
15 /// </summary>
16 Complete = 3
17 }
18 }
19
@@ -29,7 +29,7 namespace Implab.Fx
29 29
30 30 var directed = new ControlBoundPromise<T>(ctl,that);
31 31
32 that.Last(
32 that.On(
33 33 directed.Resolve,
34 34 directed.Reject,
35 35 directed.Cancel
@@ -41,9 +41,10 namespace Implab {
41 41 /// <param name="success">Success.</param>
42 42 /// <param name="error">Error.</param>
43 43 /// <param name="cancel">Cancel.</param>
44 void Last(Action success, Action<Exception> error, Action cancel);
45 void Last(Action success, Action<Exception> error);
46 void Last(Action success);
44 void On(Action success, Action<Exception> error, Action cancel);
45 void On(Action success, Action<Exception> error);
46 void On(Action success);
47 void On(Action success, PromiseEventType events);
47 48
48 49 IPromise Error(Action<Exception> error);
49 50 /// <summary>
@@ -7,11 +7,11 namespace Implab {
7 7
8 8 new T Join(int timeout);
9 9
10 void Last(Action<T> success, Action<Exception> error, Action cancel);
10 void On(Action<T> success, Action<Exception> error, Action cancel);
11 11
12 void Last(Action<T> success, Action<Exception> error);
12 void On(Action<T> success, Action<Exception> error);
13 13
14 void Last(Action<T> success);
14 void On(Action<T> success);
15 15
16 16 IPromise<T> Then(Action<T> success, Func<Exception,T> error, Action cancel);
17 17
@@ -147,6 +147,7
147 147 <Compile Include="Diagnostics\Extensions.cs" />
148 148 <Compile Include="IComponentContainer.cs" />
149 149 <Compile Include="MTComponentContainer.cs" />
150 <Compile Include="PromiseEventType.cs" />
150 151 </ItemGroup>
151 152 <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
152 153 <ItemGroup />
@@ -183,7 +183,7 namespace Implab.Parallels {
183 183 Monitor.Pulse(locker);
184 184 }
185 185 })
186 .Last(
186 .On(
187 187 x => {
188 188 res[idx] = x;
189 189 var left = Interlocked.Decrement(ref pending);
@@ -117,7 +117,8 namespace Implab {
117 117 if (parent.IsExclusive)
118 118 parent.Cancel();
119 119 },
120 null
120 null,
121 false
121 122 );
122 123 }
123 124
@@ -220,7 +221,7 namespace Implab {
220 221
221 222 var medium = new Promise<T>(this);
222 223
223 AddHandler(success, error, cancel, medium);
224 AddHandler(success, error, cancel, medium, true);
224 225
225 226 return medium;
226 227 }
@@ -238,7 +239,7 namespace Implab {
238 239
239 240 var medium = new Promise<T>(this);
240 241
241 AddHandler(success, error, null, medium);
242 AddHandler(success, error, null, medium, true);
242 243
243 244 return medium;
244 245 }
@@ -252,7 +253,7 namespace Implab {
252 253
253 254 var medium = new Promise<T>(this);
254 255
255 AddHandler(success, null, null, medium);
256 AddHandler(success, null, null, medium, true);
256 257
257 258 return medium;
258 259 }
@@ -274,7 +275,7 namespace Implab {
274 275 /// всей цепи обещаний снизу (с самого последнего обещания).
275 276 /// </para>
276 277 /// </remarks>
277 public void Last(Action<T> success, Action<Exception> error, Action cancel) {
278 public void On(Action<T> success, Action<Exception> error, Action cancel) {
278 279 if (success == null && error == null && cancel == null)
279 280 return;
280 281
@@ -284,15 +285,28 namespace Implab {
284 285 error(err);
285 286 return default(T);
286 287 };
287 AddHandler(success, errorHandler, cancel, null);
288 AddHandler(success, errorHandler, cancel, null, false);
289 }
290
291 public void On(Action<T> success, Action<Exception> error) {
292 On(success, error, null);
293 }
294
295 public void On(Action<T> success) {
296 On(success, null, null);
288 297 }
289 298
290 public void Last(Action<T> success, Action<Exception> error) {
291 Last(success, error, null);
292 }
299 public void On(Action handler, PromiseEventType events) {
300 Safe.ArgumentNotNull(handler, "handler");
293 301
294 public void Last(Action<T> success) {
295 Last(success, null, null);
302 Action<T> success = events.HasFlag(PromiseEventType.Success) ? new Action<T>(x => handler()) : null;
303 Func<Exception,T> error = events.HasFlag(PromiseEventType.Error) ? new Func<Exception,T>(e => {
304 handler();
305 return default(T);
306 }) : null;
307 Action cancel = events.HasFlag(PromiseEventType.Cancelled) ? handler : null;
308
309 AddHandler(success, error, cancel, null, false);
296 310 }
297 311
298 312 public IPromise Error(Action<Exception> error) {
@@ -308,7 +322,8 namespace Implab {
308 322 return default(T);
309 323 },
310 324 null,
311 medium
325 medium,
326 true
312 327 );
313 328
314 329 return medium;
@@ -328,7 +343,7 namespace Implab {
328 343
329 344 var medium = new Promise<T>(this);
330 345
331 AddHandler(null, handler, null, medium);
346 AddHandler(null, handler, null, medium, true);
332 347
333 348 return medium;
334 349 }
@@ -380,7 +395,8 namespace Implab {
380 395 resultHandler,
381 396 errorHandler,
382 397 cancelHandler,
383 null
398 null,
399 true
384 400 );
385 401
386 402 return medium;
@@ -421,7 +437,7 namespace Implab {
421 437
422 438 var promise = chained(result);
423 439
424 promise.Last(
440 promise.On(
425 441 medium.Resolve,
426 442 medium.Reject,
427 443 () => medium.Reject(new OperationCanceledException()) // внешняя отмена связанной операции рассматривается как ошибка
@@ -430,7 +446,7 namespace Implab {
430 446 // notify chained operation that it's not needed anymore
431 447 // порядок вызова Then, Cancelled важен, поскольку от этого
432 448 // зависит IsExclusive
433 medium.Last(
449 medium.On(
434 450 null,
435 451 null,
436 452 () => {
@@ -447,7 +463,7 namespace Implab {
447 463 try {
448 464 var promise = error(e);
449 465
450 promise.Last(
466 promise.On(
451 467 medium.Resolve,
452 468 medium.Reject,
453 469 () => medium.Reject(new OperationCanceledException()) // внешняя отмена связанной операции рассматривается как ошибка
@@ -486,7 +502,8 namespace Implab {
486 502 resultHandler,
487 503 errorHandler,
488 504 cancelHandler,
489 null
505 null,
506 true
490 507 );
491 508
492 509 return medium;
@@ -502,7 +519,7 namespace Implab {
502 519
503 520 public IPromise<T> Cancelled(Action handler) {
504 521 var medium = new Promise<T>(this);
505 AddHandler(null, null, handler, medium);
522 AddHandler(null, null, handler, medium, false);
506 523 return medium;
507 524 }
508 525
@@ -513,7 +530,9 namespace Implab {
513 530 /// <returns>self</returns>
514 531 public IPromise<T> Anyway(Action handler) {
515 532 Safe.ArgumentNotNull(handler, "handler");
516
533
534 var medium = new Promise<T>(this);
535
517 536 AddHandler(
518 537 x => handler(),
519 538 e => {
@@ -521,9 +540,11 namespace Implab {
521 540 throw new TransientPromiseException(e);
522 541 },
523 542 handler,
524 null
543 medium,
544 true
525 545 );
526 return this;
546
547 return medium;
527 548 }
528 549
529 550 /// <summary>
@@ -579,8 +600,8 namespace Implab {
579 600 return Join(Timeout.Infinite);
580 601 }
581 602
582 void AddHandler(Action<T> success, Func<Exception,T> error, Action cancel, Promise<T> medium) {
583 if (success != null || error != null)
603 void AddHandler(Action<T> success, Func<Exception,T> error, Action cancel, Promise<T> medium, bool inc) {
604 if (inc)
584 605 Interlocked.Increment(ref m_childrenCount);
585 606
586 607 var handler = new HandlerDescriptor {
@@ -784,39 +805,39 namespace Implab {
784 805 }
785 806
786 807 IPromise ChainNoResult(Func<IPromise> chained, Func<Exception,IPromise> error, Action cancel) {
787 Safe.ArgumentNotNull(chained, "chained");
808 Safe.ArgumentNotNull(chained, "chained");
788 809
789 810 var medium = new Promise<object>(this);
790 811
791 812 Action<T> resultHandler = delegate {
792 if (medium.IsCancelled)
793 return;
813 if (medium.IsCancelled)
814 return;
794 815
795 var promise = chained();
816 var promise = chained();
796 817
797 promise.Last(
798 medium.Resolve,
799 medium.Reject,
800 () => medium.Reject(new OperationCanceledException()) // внешняя отмена связанной операции рассматривается как ошибка
801 );
818 promise.On(
819 medium.Resolve,
820 medium.Reject,
821 () => medium.Reject(new OperationCanceledException()) // внешняя отмена связанной операции рассматривается как ошибка
822 );
802 823
803 // notify chained operation that it's not needed anymore
804 // порядок вызова Then, Cancelled важен, поскольку от этого
805 // зависит IsExclusive
806 medium.Cancelled(() => {
807 if (promise.IsExclusive)
808 promise.Cancel();
809 });
810 };
824 // notify chained operation that it's not needed anymore
825 // порядок вызова Then, Cancelled важен, поскольку от этого
826 // зависит IsExclusive
827 medium.Cancelled(() => {
828 if (promise.IsExclusive)
829 promise.Cancel();
830 });
831 };
811 832
812 833 Func<Exception,T> errorHandler;
813 834
814 if (error != null)
815 errorHandler = delegate(Exception e) {
835 if (error != null)
836 errorHandler = delegate(Exception e) {
816 837 try {
817 838 var promise = error(e);
818 839
819 promise.Last(
840 promise.On(
820 841 medium.Resolve,
821 842 medium.Reject,
822 843 () => medium.Reject(new OperationCanceledException()) // внешняя отмена связанной операции рассматривается как ошибка
@@ -834,50 +855,53 namespace Implab {
834 855 }
835 856 return default(T);
836 857 };
837 else
838 errorHandler = err => {
858 else
859 errorHandler = err => {
839 860 medium.Reject(err);
840 861 return default(T);
841 862 };
842 863
843 864
844 Action cancelHandler;
845 if (cancel != null)
846 cancelHandler = () => {
865 Action cancelHandler;
866 if (cancel != null)
867 cancelHandler = () => {
847 868 if (cancel != null)
848 869 cancel();
849 870 medium.Cancel();
850 871 };
851 else
852 cancelHandler = medium.Cancel;
872 else
873 cancelHandler = medium.Cancel;
853 874
854 AddHandler(
855 resultHandler,
856 errorHandler,
857 cancelHandler,
858 null
859 );
875 AddHandler(
876 resultHandler,
877 errorHandler,
878 cancelHandler,
879 null,
880 true
881 );
860 882
861 return medium;
883 return medium;
862 884 }
885
863 886 IPromise IPromise.Chain(Func<IPromise> chained, Func<Exception,IPromise> error) {
864 887 return ChainNoResult(chained, error, null);
865 888 }
889
866 890 IPromise IPromise.Chain(Func<IPromise> chained) {
867 891 return ChainNoResult(chained, null, null);
868 }
892 }
869 893
870 894
871 void IPromise.Last(Action success, Action<Exception> error, Action cancel) {
872 Last(x => success(), error, cancel);
895 void IPromise.On(Action success, Action<Exception> error, Action cancel) {
896 On(x => success(), error, cancel);
873 897 }
874 898
875 void IPromise.Last(Action success, Action<Exception> error) {
876 Last(x => success(), error, null);
899 void IPromise.On(Action success, Action<Exception> error) {
900 On(x => success(), error, null);
877 901 }
878 902
879 void IPromise.Last(Action success) {
880 Last(x => success(), null, null);
903 void IPromise.On(Action success) {
904 On(x => success(), null, null);
881 905 }
882 906
883 907 IPromise IPromise.Error(Action<Exception> error) {
@@ -14,7 +14,7 namespace Implab {
14 14
15 15 var p = new SyncContextPromise<T>(context, that);
16 16
17 that.Last(
17 that.On(
18 18 p.Resolve,
19 19 p.Reject,
20 20 p.Cancel
@@ -28,7 +28,7 namespace Implab {
28 28
29 29 var p = new SyncContextPromise<T>(context, that);
30 30
31 that.Last(
31 that.On(
32 32 p.Resolve,
33 33 p.Reject,
34 34 p.Cancel
@@ -49,7 +49,7 namespace Implab {
49 49 Safe.ArgumentNotNull(that, "that");
50 50 Safe.ArgumentNotNull(head, "head");
51 51
52 that.Last(null,null,() => head.Last(cleanup));
52 that.On(null,null,() => head.On(cleanup));
53 53
54 54 return that;
55 55 }
@@ -72,7 +72,7 namespace Implab {
72 72 Safe.ArgumentNotNull(that, "that");
73 73 var tcs = new TaskCompletionSource<T>();
74 74
75 that.Last(tcs.SetResult, tcs.SetException, tcs.SetCanceled);
75 that.On(tcs.SetResult, tcs.SetException, tcs.SetCanceled);
76 76
77 77 return tcs.Task;
78 78 }
@@ -12,7 +12,7 namespace MonoPlay {
12 12 throw new ArgumentNullException("args");
13 13
14 14 var q1 = new MTQueue<int>();
15 var q2 = new ConcurrentQueue<int>();
15 var q2 = new Queue<int>();
16 16
17 17 const int count = 10000000;
18 18
@@ -32,7 +32,7 namespace MonoPlay {
32 32 t2 = Environment.TickCount;
33 33 Console.WriteLine("LinkedList: {0} ms", t2 - t1);
34 34
35 q2 = new ConcurrentQueue<int>();
35 q2 = new Queue<int>();
36 36
37 37 t1 = Environment.TickCount;
38 38
General Comments 0
You need to be logged in to leave comments. Login now