Mercurial > pub > ImplabNet
annotate Implab/AbstractPromise.cs @ 171:0f70905b4652 ref20160224
Working on regular DFA
author | cin |
---|---|
date | Thu, 10 Mar 2016 01:19:33 +0300 |
parents | 97fbbf816844 |
children | 75103928da09 |
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 |
156
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
145
diff
changeset
|
74 protected override void SignalHandler(HandlerDescriptor handler, int signal) { |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
145
diff
changeset
|
75 switch (signal) { |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
145
diff
changeset
|
76 case SUCCEEDED_STATE: |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
145
diff
changeset
|
77 handler.SignalSuccess(); |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
145
diff
changeset
|
78 break; |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
145
diff
changeset
|
79 case REJECTED_STATE: |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
145
diff
changeset
|
80 handler.SignalError(Error); |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
145
diff
changeset
|
81 break; |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
145
diff
changeset
|
82 case CANCELLED_STATE: |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
145
diff
changeset
|
83 handler.SignalCancel(CancellationReason); |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
145
diff
changeset
|
84 break; |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
145
diff
changeset
|
85 default: |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
145
diff
changeset
|
86 throw new InvalidOperationException(String.Format("Invalid promise signal: {0}", signal)); |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
145
diff
changeset
|
87 } |
119 | 88 } |
89 | |
144 | 90 protected override Signal GetResolveSignal() { |
91 var signal = new Signal(); | |
92 On(signal.Set, PromiseEventType.All); | |
145 | 93 return signal; |
119 | 94 } |
95 | |
96 #endregion | |
97 | |
144 | 98 public Type PromiseType { |
99 get { | |
100 return typeof(void); | |
119 | 101 } |
102 } | |
103 | |
144 | 104 public IPromise On(Action success, Action<Exception> error, Action<Exception> cancel) { |
105 AddHandler(new HandlerDescriptor(success, error, cancel)); | |
106 return this; | |
107 } | |
108 | |
109 public IPromise On(Action success, Action<Exception> error) { | |
110 AddHandler(new HandlerDescriptor(success, error, null)); | |
111 return this; | |
119 | 112 } |
113 | |
144 | 114 public IPromise On(Action success) { |
115 AddHandler(new HandlerDescriptor(success, null, null)); | |
116 return this; | |
117 } | |
119 | 118 |
144 | 119 public IPromise On(Action handler, PromiseEventType events) { |
120 AddHandler(new HandlerDescriptor(handler,events)); | |
121 return this; | |
122 } | |
119 | 123 |
144 | 124 public IPromise<T> Cast<T>() { |
125 throw new InvalidCastException(); | |
119 | 126 } |
127 | |
128 public void Join() { | |
129 WaitResult(-1); | |
130 } | |
131 | |
144 | 132 public void Join(int timeout) { |
133 WaitResult(timeout); | |
119 | 134 } |
135 | |
144 | 136 protected void SetResult() { |
137 BeginSetResult(); | |
138 EndSetResult(); | |
138
f75cfa58e3d4
added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents:
130
diff
changeset
|
139 } |
119 | 140 } |
141 } | |
142 |