Mercurial > pub > ImplabNet
comparison Implab/AbstractPromiseT.cs @ 145:706fccb85524 v2
RC: cancellation support for promises + tests
| author | cin |
|---|---|
| date | Sun, 08 Mar 2015 02:52:27 +0300 |
| parents | 8c0b95069066 |
| children | 97fbbf816844 |
comparison
equal
deleted
inserted
replaced
| 144:8c0b95069066 | 145:706fccb85524 |
|---|---|
| 12 | 12 |
| 13 public HandlerDescriptor(Action<T> success, Action<Exception> error, Action<Exception> cancel) { | 13 public HandlerDescriptor(Action<T> success, Action<Exception> error, Action<Exception> cancel) { |
| 14 m_success = success; | 14 m_success = success; |
| 15 m_error = error; | 15 m_error = error; |
| 16 m_cancel = cancel; | 16 m_cancel = cancel; |
| 17 | |
| 18 m_handler = null; | |
| 19 m_mask = 0; | |
| 17 } | 20 } |
| 18 | 21 |
| 19 public HandlerDescriptor(Action success, Action<Exception> error, Action<Exception> cancel) { | 22 public HandlerDescriptor(Action success, Action<Exception> error, Action<Exception> cancel) { |
| 20 m_handler = success; | 23 m_handler = success; |
| 24 m_success = null; | |
| 21 m_error = error; | 25 m_error = error; |
| 22 m_cancel = cancel; | 26 m_cancel = cancel; |
| 23 m_mask = PromiseEventType.Success; | 27 m_mask = PromiseEventType.Success; |
| 24 } | 28 } |
| 25 | 29 |
| 26 public HandlerDescriptor(Action handler, PromiseEventType mask) { | 30 public HandlerDescriptor(Action handler, PromiseEventType mask) { |
| 27 m_handler = handler; | 31 m_handler = handler; |
| 28 m_mask = mask; | 32 m_mask = mask; |
| 33 m_success = null; | |
| 34 m_error = null; | |
| 35 m_cancel = null; | |
| 29 } | 36 } |
| 30 | 37 |
| 31 public void SignalSuccess(T result) { | 38 public void SignalSuccess(T result) { |
| 32 if (m_success != null) { | 39 if (m_success != null) { |
| 33 try { | 40 try { |
| 34 m_success(result); | 41 m_success(result); |
| 35 } catch(Exception err) { | 42 } catch(Exception err) { |
| 36 SignalError(err); | 43 SignalError(err); |
| 37 } | 44 } |
| 38 } else if (m_mask & PromiseEventType.Success && m_handler != null) { | 45 } else if ((m_mask & PromiseEventType.Success) != 0 && m_handler != null) { |
| 39 try { | 46 try { |
| 40 m_handler(); | 47 m_handler(); |
| 41 } catch(Exception err) { | 48 } catch(Exception err) { |
| 42 // avoid calling handler twice in case of error | 49 // avoid calling handler twice in case of error |
| 43 if (m_error != null) | 50 if (m_error != null) |
| 51 try { | 58 try { |
| 52 m_error(err); | 59 m_error(err); |
| 53 // Analysis disable once EmptyGeneralCatchClause | 60 // Analysis disable once EmptyGeneralCatchClause |
| 54 } catch { | 61 } catch { |
| 55 } | 62 } |
| 56 } else if (m_mask & PromiseEventType.Error && m_handler != null) { | 63 } else if ((m_mask & PromiseEventType.Error) != 0 && m_handler != null) { |
| 57 try { | 64 try { |
| 58 m_handler(); | 65 m_handler(); |
| 59 // Analysis disable once EmptyGeneralCatchClause | 66 // Analysis disable once EmptyGeneralCatchClause |
| 60 } catch { | 67 } catch { |
| 61 } | 68 } |
| 67 try { | 74 try { |
| 68 m_cancel(reason); | 75 m_cancel(reason); |
| 69 } catch (Exception err) { | 76 } catch (Exception err) { |
| 70 SignalError(err); | 77 SignalError(err); |
| 71 } | 78 } |
| 72 } else if (m_mask & PromiseEventType.Cancelled && m_handler != null) { | 79 } else if ((m_mask & PromiseEventType.Cancelled) != 0 && m_handler != null) { |
| 73 try { | 80 try { |
| 74 m_handler(); | 81 m_handler(); |
| 75 // Analysis disable once EmptyGeneralCatchClause | 82 // Analysis disable once EmptyGeneralCatchClause |
| 76 } catch { | 83 } catch { |
| 77 } | 84 } |
| 78 } | 85 } |
| 79 } | 86 } |
| 80 } | 87 } |
| 81 | 88 |
| 82 | |
| 83 | |
| 84 public Type PromiseType { | 89 public Type PromiseType { |
| 85 get { | 90 get { |
| 86 return typeof(T); | 91 return typeof(T); |
| 87 } | 92 } |
| 88 } | 93 } |
| 89 | 94 |
| 90 public new T Join() { | 95 public T Join() { |
| 91 WaitResult(-1); | 96 WaitResult(-1); |
| 92 return m_result; | 97 return m_result; |
| 93 } | 98 } |
| 94 public new T Join(int timeout) { | 99 public T Join(int timeout) { |
| 95 WaitResult(timeout); | 100 WaitResult(timeout); |
| 96 return m_result; | 101 return m_result; |
| 97 } | 102 } |
| 98 | 103 |
| 104 void IPromise.Join() { | |
| 105 WaitResult(-1); | |
| 106 } | |
| 107 void IPromise.Join(int timeout) { | |
| 108 WaitResult(timeout); | |
| 109 } | |
| 110 | |
| 99 public IPromise<T> On(Action<T> success, Action<Exception> error, Action<Exception> cancel) { | 111 public IPromise<T> On(Action<T> success, Action<Exception> error, Action<Exception> cancel) { |
| 100 AddHandler(new HandlerDescriptor(success, error, cancel)); | 112 AddHandler(new HandlerDescriptor(success, error, cancel)); |
| 101 return this; | 113 return this; |
| 102 } | 114 } |
| 103 | 115 |
| 141 return this; | 153 return this; |
| 142 } | 154 } |
| 143 | 155 |
| 144 IPromise IPromise.On(Action success) { | 156 IPromise IPromise.On(Action success) { |
| 145 AddHandler(new HandlerDescriptor(success, null, null)); | 157 AddHandler(new HandlerDescriptor(success, null, null)); |
| 158 return this; | |
| 159 } | |
| 160 | |
| 161 IPromise IPromise.On(Action handler, PromiseEventType events) { | |
| 162 AddHandler(new HandlerDescriptor(handler, events)); | |
| 146 return this; | 163 return this; |
| 147 } | 164 } |
| 148 | 165 |
| 149 public IPromise<T2> Cast<T2>() { | 166 public IPromise<T2> Cast<T2>() { |
| 150 return (IPromise<T2>)this; | 167 return (IPromise<T2>)this; |
