Mercurial > pub > ImplabNet
annotate Implab/AbstractPromise.cs @ 146:e03ccec4a08d v2
minor changes
author | cin |
---|---|
date | Mon, 09 Mar 2015 17:21:20 +0300 |
parents | 706fccb85524 |
children | 97fbbf816844 |
rev | line source |
---|---|
119 | 1 using System; |
2 using Implab.Parallels; | |
3 | |
4 namespace Implab { | |
144 | 5 public abstract class AbstractPromise : AbstractEvent<AbstractPromise.HandlerDescriptor>, IPromise { |
6 public struct HandlerDescriptor { | |
7 readonly Action m_handler; | |
8 readonly Action<Exception> m_error; | |
9 readonly Action<Exception> m_cancel; | |
10 readonly PromiseEventType m_mask; | |
119 | 11 |
144 | 12 public HandlerDescriptor(Action success, Action<Exception> error, Action<Exception> cancel) { |
13 m_handler = success; | |
14 m_error = error; | |
15 m_cancel = cancel; | |
16 m_mask = PromiseEventType.Success; | |
17 } | |
125 | 18 |
144 | 19 public HandlerDescriptor(Action handler, PromiseEventType mask) { |
20 m_handler = handler; | |
145 | 21 m_error = null; |
22 m_cancel = null; | |
144 | 23 m_mask = mask; |
24 } | |
119 | 25 |
144 | 26 public void SignalSuccess() { |
145 | 27 if ((m_mask & PromiseEventType.Success) != 0 && m_handler != null) { |
144 | 28 try { |
29 m_handler(); | |
30 } catch (Exception err) { | |
31 // avoid calling handler twice in case of error | |
32 if (m_error != null) | |
33 SignalError(err); | |
34 } | |
35 } | |
36 } | |
119 | 37 |
144 | 38 public void SignalError(Exception err) { |
39 if (m_error != null) { | |
40 try { | |
41 m_error(err); | |
42 // Analysis disable once EmptyGeneralCatchClause | |
43 } catch { | |
44 } | |
145 | 45 } else if ((m_mask & PromiseEventType.Error ) != 0 && m_handler != null) { |
144 | 46 try { |
47 m_handler(); | |
48 // Analysis disable once EmptyGeneralCatchClause | |
49 } catch { | |
50 } | |
51 } | |
119 | 52 } |
53 | |
144 | 54 public void SignalCancel(Exception reason) { |
55 if (m_cancel != null) { | |
56 try { | |
57 m_cancel(reason); | |
58 } catch (Exception err) { | |
59 SignalError(err); | |
60 } | |
145 | 61 } else if ( (m_mask & PromiseEventType.Cancelled) != 0 && m_handler != null) { |
144 | 62 try { |
63 m_handler(); | |
64 // Analysis disable once EmptyGeneralCatchClause | |
65 } catch { | |
66 } | |
67 } | |
119 | 68 } |
69 } | |
70 | |
71 | |
144 | 72 #region implemented abstract members of AbstractPromise |
119 | 73 |
144 | 74 protected override void SignalSuccess(HandlerDescriptor handler) { |
75 handler.SignalSuccess(); | |
119 | 76 } |
77 | |
144 | 78 protected override void SignalError(HandlerDescriptor handler, Exception error) { |
79 handler.SignalError(error); | |
119 | 80 } |
81 | |
144 | 82 protected override void SignalCancelled(HandlerDescriptor handler, Exception reason) { |
83 handler.SignalCancel(reason); | |
119 | 84 } |
85 | |
144 | 86 protected override Signal GetResolveSignal() { |
87 var signal = new Signal(); | |
88 On(signal.Set, PromiseEventType.All); | |
145 | 89 return signal; |
119 | 90 } |
91 | |
92 #endregion | |
93 | |
144 | 94 public Type PromiseType { |
95 get { | |
96 return typeof(void); | |
119 | 97 } |
98 } | |
99 | |
144 | 100 public IPromise On(Action success, Action<Exception> error, Action<Exception> cancel) { |
101 AddHandler(new HandlerDescriptor(success, error, cancel)); | |
102 return this; | |
103 } | |
104 | |
105 public IPromise On(Action success, Action<Exception> error) { | |
106 AddHandler(new HandlerDescriptor(success, error, null)); | |
107 return this; | |
119 | 108 } |
109 | |
144 | 110 public IPromise On(Action success) { |
111 AddHandler(new HandlerDescriptor(success, null, null)); | |
112 return this; | |
113 } | |
119 | 114 |
144 | 115 public IPromise On(Action handler, PromiseEventType events) { |
116 AddHandler(new HandlerDescriptor(handler,events)); | |
117 return this; | |
118 } | |
119 | 119 |
144 | 120 public IPromise<T> Cast<T>() { |
121 throw new InvalidCastException(); | |
119 | 122 } |
123 | |
124 public void Join() { | |
125 WaitResult(-1); | |
126 } | |
127 | |
144 | 128 public void Join(int timeout) { |
129 WaitResult(timeout); | |
119 | 130 } |
131 | |
144 | 132 protected void SetResult() { |
133 BeginSetResult(); | |
134 EndSetResult(); | |
138
f75cfa58e3d4
added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents:
130
diff
changeset
|
135 } |
119 | 136 } |
137 } | |
138 |