comparison Implab/Promise.cs @ 71:1714fd8678ef

code cleanup
author cin
date Wed, 03 Sep 2014 18:34:02 +0400
parents 790e8a997d30
children d67b95eddaf4
comparison
equal deleted inserted replaced
70:0349189d2564 71:1714fd8678ef
78 } catch { 78 } catch {
79 } 79 }
80 } 80 }
81 } 81 }
82 82
83 const int UnresolvedSate = 0; 83 const int UNRESOLVED_SATE = 0;
84 const int TransitionalState = 1; 84 const int TRANSITIONAL_STATE = 1;
85 const int SucceededState = 2; 85 const int SUCCEEDED_STATE = 2;
86 const int RejectedState = 3; 86 const int REJECTED_STATE = 3;
87 const int CancelledState = 4; 87 const int CANCELLED_STATE = 4;
88 88
89 readonly bool m_cancellable; 89 readonly bool m_cancellable;
90 90
91 int m_childrenCount = 0; 91 int m_childrenCount = 0;
92 int m_state; 92 int m_state;
111 } 111 }
112 ); 112 );
113 } 113 }
114 114
115 bool BeginTransit() { 115 bool BeginTransit() {
116 return UnresolvedSate == Interlocked.CompareExchange(ref m_state, TransitionalState, UnresolvedSate); 116 return UNRESOLVED_SATE == Interlocked.CompareExchange(ref m_state, TRANSITIONAL_STATE, UNRESOLVED_SATE);
117 } 117 }
118 118
119 void CompleteTransit(int state) { 119 void CompleteTransit(int state) {
120 if (TransitionalState != Interlocked.CompareExchange(ref m_state, state, TransitionalState)) 120 if (TRANSITIONAL_STATE != Interlocked.CompareExchange(ref m_state, state, TRANSITIONAL_STATE))
121 throw new InvalidOperationException("Can't complete transition when the object isn't in the transitional state"); 121 throw new InvalidOperationException("Can't complete transition when the object isn't in the transitional state");
122 } 122 }
123 123
124 void WaitTransition() { 124 void WaitTransition() {
125 while (m_state == TransitionalState) { 125 while (m_state == TRANSITIONAL_STATE) {
126 /* noop */ 126 /* noop */
127 } 127 }
128 } 128 }
129 129
130 public bool IsResolved { 130 public bool IsResolved {
133 } 133 }
134 } 134 }
135 135
136 public bool IsCancelled { 136 public bool IsCancelled {
137 get { 137 get {
138 return m_state == CancelledState; 138 return m_state == CANCELLED_STATE;
139 } 139 }
140 } 140 }
141 141
142 public Type PromiseType { 142 public Type PromiseType {
143 get { return typeof(T); } 143 get { return typeof(T); }
149 /// <param name="result">Результат выполнения.</param> 149 /// <param name="result">Результат выполнения.</param>
150 /// <exception cref="InvalidOperationException">Данное обещание уже выполнено</exception> 150 /// <exception cref="InvalidOperationException">Данное обещание уже выполнено</exception>
151 public void Resolve(T result) { 151 public void Resolve(T result) {
152 if (BeginTransit()) { 152 if (BeginTransit()) {
153 m_result = result; 153 m_result = result;
154 CompleteTransit(SucceededState); 154 CompleteTransit(SUCCEEDED_STATE);
155 OnStateChanged(); 155 OnStateChanged();
156 } else { 156 } else {
157 WaitTransition(); 157 WaitTransition();
158 if (m_state != CancelledState) 158 if (m_state != CANCELLED_STATE)
159 throw new InvalidOperationException("The promise is already resolved"); 159 throw new InvalidOperationException("The promise is already resolved");
160 } 160 }
161 } 161 }
162 162
163 /// <summary> 163 /// <summary>
181 /// <param name="error">Исключение возникшее при выполнении операции</param> 181 /// <param name="error">Исключение возникшее при выполнении операции</param>
182 /// <exception cref="InvalidOperationException">Данное обещание уже выполнено</exception> 182 /// <exception cref="InvalidOperationException">Данное обещание уже выполнено</exception>
183 public void Reject(Exception error) { 183 public void Reject(Exception error) {
184 if (BeginTransit()) { 184 if (BeginTransit()) {
185 m_error = error; 185 m_error = error;
186 CompleteTransit(RejectedState); 186 CompleteTransit(REJECTED_STATE);
187 OnStateChanged(); 187 OnStateChanged();
188 } else { 188 } else {
189 WaitTransition(); 189 WaitTransition();
190 if (m_state == SucceededState) 190 if (m_state == SUCCEEDED_STATE)
191 throw new InvalidOperationException("The promise is already resolved"); 191 throw new InvalidOperationException("The promise is already resolved");
192 } 192 }
193 } 193 }
194 194
195 /// <summary> 195 /// <summary>
196 /// Отменяет операцию, если это возможно. 196 /// Отменяет операцию, если это возможно.
197 /// </summary> 197 /// </summary>
198 /// <returns><c>true</c> Операция была отменена, обработчики не будут вызваны.<c>false</c> отмена не возможна, поскольку обещание уже выполнено и обработчики отработали.</returns> 198 /// <returns><c>true</c> Операция была отменена, обработчики не будут вызваны.<c>false</c> отмена не возможна, поскольку обещание уже выполнено и обработчики отработали.</returns>
199 public bool Cancel() { 199 public bool Cancel() {
200 if (BeginTransit()) { 200 if (BeginTransit()) {
201 CompleteTransit(CancelledState); 201 CompleteTransit(CANCELLED_STATE);
202 OnStateChanged(); 202 OnStateChanged();
203 return true; 203 return true;
204 } else { 204 } else {
205 return false; 205 return false;
206 } 206 }
543 543
544 if (!evt.WaitOne(timeout, true)) 544 if (!evt.WaitOne(timeout, true))
545 throw new TimeoutException(); 545 throw new TimeoutException();
546 546
547 switch (m_state) { 547 switch (m_state) {
548 case SucceededState: 548 case SUCCEEDED_STATE:
549 return m_result; 549 return m_result;
550 case CancelledState: 550 case CANCELLED_STATE:
551 throw new OperationCanceledException(); 551 throw new OperationCanceledException();
552 case RejectedState: 552 case REJECTED_STATE:
553 throw new TargetInvocationException(m_error); 553 throw new TargetInvocationException(m_error);
554 default: 554 default:
555 throw new ApplicationException(String.Format("Invalid promise state {0}", m_state)); 555 throw new ApplicationException(String.Format("Invalid promise state {0}", m_state));
556 } 556 }
557 } 557 }
590 InvokeHandler(handler); 590 InvokeHandler(handler);
591 } 591 }
592 592
593 protected virtual void InvokeHandler(HandlerDescriptor handler) { 593 protected virtual void InvokeHandler(HandlerDescriptor handler) {
594 switch (m_state) { 594 switch (m_state) {
595 case SucceededState: 595 case SUCCEEDED_STATE:
596 handler.Resolve(m_result); 596 handler.Resolve(m_result);
597 break; 597 break;
598 case RejectedState: 598 case REJECTED_STATE:
599 handler.Reject(m_error); 599 handler.Reject(m_error);
600 break; 600 break;
601 case CancelledState: 601 case CANCELLED_STATE:
602 handler.Cancel(); 602 handler.Cancel();
603 break; 603 break;
604 default: 604 default:
605 // do nothing 605 // do nothing
606 return; 606 return;