Mercurial > pub > ImplabNet
annotate Implab/AbstractPromise.cs @ 223:27ea7f07e2e4 default
Слияние
author | cin |
---|---|
date | Tue, 22 Aug 2017 09:35:54 +0300 |
parents | 86187b01c4e0 |
children | cbe10ac0731e |
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(); | |
197
86187b01c4e0
fixed: the error handler should not handle handlers errors
cin
parents:
186
diff
changeset
|
30 // Analysis disable once EmptyGeneralCatchClause |
86187b01c4e0
fixed: the error handler should not handle handlers errors
cin
parents:
186
diff
changeset
|
31 } catch { |
144 | 32 } |
33 } | |
34 } | |
119 | 35 |
144 | 36 public void SignalError(Exception err) { |
37 if (m_error != null) { | |
38 try { | |
39 m_error(err); | |
40 // Analysis disable once EmptyGeneralCatchClause | |
41 } catch { | |
42 } | |
145 | 43 } else if ((m_mask & PromiseEventType.Error ) != 0 && m_handler != null) { |
144 | 44 try { |
45 m_handler(); | |
46 // Analysis disable once EmptyGeneralCatchClause | |
47 } catch { | |
48 } | |
49 } | |
119 | 50 } |
51 | |
144 | 52 public void SignalCancel(Exception reason) { |
53 if (m_cancel != null) { | |
54 try { | |
55 m_cancel(reason); | |
197
86187b01c4e0
fixed: the error handler should not handle handlers errors
cin
parents:
186
diff
changeset
|
56 // Analysis disable once EmptyGeneralCatchClause |
86187b01c4e0
fixed: the error handler should not handle handlers errors
cin
parents:
186
diff
changeset
|
57 } catch { |
144 | 58 } |
145 | 59 } else if ( (m_mask & PromiseEventType.Cancelled) != 0 && m_handler != null) { |
144 | 60 try { |
61 m_handler(); | |
62 // Analysis disable once EmptyGeneralCatchClause | |
63 } catch { | |
64 } | |
65 } | |
119 | 66 } |
67 } | |
68 | |
69 | |
144 | 70 #region implemented abstract members of AbstractPromise |
119 | 71 |
156
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
145
diff
changeset
|
72 protected override void SignalHandler(HandlerDescriptor handler, int signal) { |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
145
diff
changeset
|
73 switch (signal) { |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
145
diff
changeset
|
74 case SUCCEEDED_STATE: |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
145
diff
changeset
|
75 handler.SignalSuccess(); |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
145
diff
changeset
|
76 break; |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
145
diff
changeset
|
77 case REJECTED_STATE: |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
145
diff
changeset
|
78 handler.SignalError(Error); |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
145
diff
changeset
|
79 break; |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
145
diff
changeset
|
80 case CANCELLED_STATE: |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
145
diff
changeset
|
81 handler.SignalCancel(CancellationReason); |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
145
diff
changeset
|
82 break; |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
145
diff
changeset
|
83 default: |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
145
diff
changeset
|
84 throw new InvalidOperationException(String.Format("Invalid promise signal: {0}", signal)); |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
145
diff
changeset
|
85 } |
119 | 86 } |
87 | |
144 | 88 protected override Signal GetResolveSignal() { |
89 var signal = new Signal(); | |
90 On(signal.Set, PromiseEventType.All); | |
145 | 91 return signal; |
119 | 92 } |
93 | |
94 #endregion | |
95 | |
144 | 96 public Type PromiseType { |
97 get { | |
98 return typeof(void); | |
119 | 99 } |
100 } | |
101 | |
144 | 102 public IPromise On(Action success, Action<Exception> error, Action<Exception> cancel) { |
103 AddHandler(new HandlerDescriptor(success, error, cancel)); | |
104 return this; | |
105 } | |
106 | |
107 public IPromise On(Action success, Action<Exception> error) { | |
108 AddHandler(new HandlerDescriptor(success, error, null)); | |
109 return this; | |
119 | 110 } |
111 | |
144 | 112 public IPromise On(Action success) { |
113 AddHandler(new HandlerDescriptor(success, null, null)); | |
114 return this; | |
115 } | |
119 | 116 |
144 | 117 public IPromise On(Action handler, PromiseEventType events) { |
118 AddHandler(new HandlerDescriptor(handler,events)); | |
119 return this; | |
120 } | |
119 | 121 |
144 | 122 public IPromise<T> Cast<T>() { |
123 throw new InvalidCastException(); | |
119 | 124 } |
125 | |
126 public void Join() { | |
127 WaitResult(-1); | |
128 } | |
129 | |
144 | 130 public void Join(int timeout) { |
131 WaitResult(timeout); | |
119 | 132 } |
133 | |
144 | 134 protected void SetResult() { |
186 | 135 if(BeginSetResult()) |
136 EndSetResult(); | |
138
f75cfa58e3d4
added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents:
130
diff
changeset
|
137 } |
119 | 138 } |
139 } | |
140 |