comparison Implab.Fx/PromiseHelpers.cs @ 72:d67b95eddaf4 v2

promises refactoring
author cin
date Thu, 04 Sep 2014 18:47:12 +0400
parents 381095ad0a69
children c761fc982e1d
comparison
equal deleted inserted replaced
71:1714fd8678ef 72:d67b95eddaf4
1 using System; 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Windows.Forms; 2 using System.Windows.Forms;
6 using System.Threading; 3 using System.Threading;
7 4
8 namespace Implab.Fx 5 namespace Implab.Fx
9 { 6 {
30 if (that == null) 27 if (that == null)
31 throw new ArgumentNullException("that"); 28 throw new ArgumentNullException("that");
32 if (ctl == null) 29 if (ctl == null)
33 throw new ArgumentNullException("ctl"); 30 throw new ArgumentNullException("ctl");
34 31
35 var directed = new Promise<T>(); 32 var directed = new ControlBoundPromise<T>(ctl,that,true);
36 33
37 that.Then( 34 that.Then(
38 res => 35 directed.Resolve,
39 {
40 if (ctl.InvokeRequired)
41 ctl.Invoke(new Action<T>(directed.Resolve), res);
42 else
43 directed.Resolve(res);
44 },
45 err => 36 err =>
46 { 37 {
47 if (ctl.InvokeRequired) 38 directed.Reject(err);
48 ctl.Invoke(new Action<Exception>(directed.Reject), err); 39 return default(T);
49 else
50 directed.Reject(err);
51 } 40 }
52 ); 41 );
53 42
54 return directed; 43 return directed;
55 } 44 }
84 73
85 var d = new Promise<T>(); 74 var d = new Promise<T>();
86 75
87 that.Then( 76 that.Then(
88 res => sync.Post(state => d.Resolve(res), null), 77 res => sync.Post(state => d.Resolve(res), null),
89 err => sync.Post(state => d.Reject(err), null) 78 err => {
79 sync.Post(state => d.Reject(err), null);
80 return default(T);
81 }
90 ); 82 );
91 83
92 return d; 84 return d;
93 } 85 }
94 } 86 }