comparison Implab/PromiseHandler.cs @ 249:d82909310094 v3

Implab.Test moved to xunit Complete set of PromiseHelpers (Then, Catch, Finally) Removed obsolete types ICancellable, ICancellationToken
author cin
date Wed, 31 Jan 2018 11:28:38 +0300
parents 5cb4826c2c2a
children
comparison
equal deleted inserted replaced
248:5cb4826c2c2a 249:d82909310094
1 using System; 1 using System;
2 using System.Diagnostics; 2 using System.Diagnostics;
3 3
4 namespace Implab { 4 namespace Implab {
5 class PromiseHandler { 5 class PromiseHandler {
6 public static Action<T> Create<T>(Action<T> handler, Deferred next) { 6 public static Action<T, Deferred> Create<T>(Action<T> handler) {
7 Debug.Assert(handler != null); 7 Debug.Assert(handler != null);
8 8
9 return (v) => { 9 return (v, next) => {
10 try { 10 try {
11 handler(v); 11 handler(v);
12 next.Resolve(); 12 next.Resolve();
13 } catch (Exception err) { 13 } catch (Exception err) {
14 next.Reject(err); 14 next.Reject(err);
15 } 15 }
16 }; 16 };
17 } 17 }
18 18
19 public static Action<T> Create<T>(Func<T, IPromise> handler, Deferred next) { 19 public static Action<T, Deferred> Create<T>(Func<T, IPromise> handler) {
20 Debug.Assert(handler != null); 20 Debug.Assert(handler != null);
21 21
22 return (v) => { 22 return (v, next) => {
23 try { 23 try {
24 next.Resolve(handler(v)); 24 next.Resolve(handler(v));
25 } catch (Exception err) { 25 } catch (Exception err) {
26 next.Reject(err); 26 next.Reject(err);
27 } 27 }
28 }; 28 };
29 } 29 }
30 30
31 public static Action<T> Create<T, T2>(Func<T, T2> handler, Deferred<T2> next) { 31 public static Action<T, Deferred<T2>> Create<T, T2>(Func<T, T2> handler) {
32 Debug.Assert(handler != null); 32 Debug.Assert(handler != null);
33 33
34 return (v) => { 34 return (v, next) => {
35 try { 35 try {
36 next.Resolve(handler(v)); 36 next.Resolve(handler(v));
37 } catch (Exception err) { 37 } catch (Exception err) {
38 next.Reject(err); 38 next.Reject(err);
39 } 39 }
40 }; 40 };
41 } 41 }
42 42
43 public static Action<T> Create<T, T2>(Func<T, IPromise<T2>> handler, Deferred<T2> next) { 43 public static Action<T, Deferred<T2>> Create<T, T2>(Func<T, IPromise<T2>> handler) {
44 Debug.Assert(handler != null); 44 Debug.Assert(handler != null);
45 return (v) => { 45 return (v, next) => {
46 try { 46 try {
47 next.Resolve(handler(v)); 47 next.Resolve(handler(v));
48 } catch (Exception err) { 48 } catch (Exception err) {
49 next.Reject(err); 49 next.Reject(err);
50 } 50 }
51 }; 51 };
52 } 52 }
53 53
54 public static Action Create(Action handler, Deferred next) { 54 public static Action<Deferred> Create(Action handler) {
55 Debug.Assert(handler != null); 55 Debug.Assert(handler != null);
56 56
57 return () => { 57 return (next) => {
58 try { 58 try {
59 handler(); 59 handler();
60 next.Resolve(); 60 next.Resolve();
61 } catch (Exception err) { 61 } catch (Exception err) {
62 next.Reject(err); 62 next.Reject(err);
63 } 63 }
64 }; 64 };
65 } 65 }
66 66
67 public static Action Create(Func<IPromise> handler, Deferred next) { 67 public static Action<Deferred> Create(Func<IPromise> handler) {
68 Debug.Assert(handler != null); 68 Debug.Assert(handler != null);
69 69
70 return () => { 70 return (next) => {
71 try { 71 try {
72 next.Resolve(handler()); 72 next.Resolve(handler());
73 } catch (Exception err) { 73 } catch (Exception err) {
74 next.Reject(err); 74 next.Reject(err);
75 } 75 }
76 }; 76 };
77 } 77 }
78 78
79 public static Action Create<T2>(Func<T2> handler, Deferred<T2> next) { 79 public static Action<Deferred<T2>> Create<T2>(Func<T2> handler) {
80 Debug.Assert(handler != null); 80 Debug.Assert(handler != null);
81 81
82 return () => { 82 return (next) => {
83 try { 83 try {
84 next.Resolve(handler()); 84 next.Resolve(handler());
85 } catch (Exception err) { 85 } catch (Exception err) {
86 next.Reject(err); 86 next.Reject(err);
87 } 87 }
88 }; 88 };
89 } 89 }
90 90
91 public static Action Create<T2>(Func<IPromise<T2>> handler, Deferred<T2> next) { 91 public static Action<Deferred<T2>> Create<T2>(Func<IPromise<T2>> handler) {
92 Debug.Assert(handler != null); 92 Debug.Assert(handler != null);
93 return () => { 93 return (next) => {
94 try { 94 try {
95 next.Resolve(handler()); 95 next.Resolve(handler());
96 } catch (Exception err) { 96 } catch (Exception err) {
97 next.Reject(err); 97 next.Reject(err);
98 } 98 }