Mercurial > pub > ImplabNet
annotate Implab/AbstractPromise.cs @ 144:8c0b95069066 v2
DRAFT: refactoring
| author | cin |
|---|---|
| date | Fri, 06 Mar 2015 15:45:26 +0300 |
| parents | 16f926ee499d |
| children | 706fccb85524 |
| 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; | |
| 21 m_mask = mask; | |
| 22 } | |
| 119 | 23 |
| 144 | 24 public void SignalSuccess() { |
| 25 if (m_mask & PromiseEventType.Success && m_handler != null) { | |
| 26 try { | |
| 27 m_handler(); | |
| 28 } catch (Exception err) { | |
| 29 // avoid calling handler twice in case of error | |
| 30 if (m_error != null) | |
| 31 SignalError(err); | |
| 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 } | |
| 43 } else if (m_mask & PromiseEventType.Error && m_handler != null) { | |
| 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); | |
| 56 } catch (Exception err) { | |
| 57 SignalError(err); | |
| 58 } | |
| 59 } else if (m_mask & PromiseEventType.Cancelled && m_handler != null) { | |
| 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 |
| 144 | 72 protected override void SignalSuccess(HandlerDescriptor handler) { |
| 73 handler.SignalSuccess(); | |
| 119 | 74 } |
| 75 | |
| 144 | 76 protected override void SignalError(HandlerDescriptor handler, Exception error) { |
| 77 handler.SignalError(error); | |
| 119 | 78 } |
| 79 | |
| 144 | 80 protected override void SignalCancelled(HandlerDescriptor handler, Exception reason) { |
| 81 handler.SignalCancel(reason); | |
| 119 | 82 } |
| 83 | |
| 144 | 84 protected override Signal GetResolveSignal() { |
| 85 var signal = new Signal(); | |
| 86 On(signal.Set, PromiseEventType.All); | |
| 119 | 87 } |
| 88 | |
| 89 #endregion | |
| 90 | |
| 91 | |
| 144 | 92 public Type PromiseType { |
| 93 get { | |
| 94 return typeof(void); | |
| 119 | 95 } |
| 96 } | |
| 97 | |
| 144 | 98 public IPromise On(Action success, Action<Exception> error, Action<Exception> cancel) { |
| 99 AddHandler(new HandlerDescriptor(success, error, cancel)); | |
| 100 return this; | |
| 101 } | |
| 102 | |
| 103 public IPromise On(Action success, Action<Exception> error) { | |
| 104 AddHandler(new HandlerDescriptor(success, error, null)); | |
| 105 return this; | |
| 119 | 106 } |
| 107 | |
| 144 | 108 public IPromise On(Action success) { |
| 109 AddHandler(new HandlerDescriptor(success, null, null)); | |
| 110 return this; | |
| 111 } | |
| 119 | 112 |
| 144 | 113 public IPromise On(Action handler, PromiseEventType events) { |
| 114 AddHandler(new HandlerDescriptor(handler,events)); | |
| 115 return this; | |
| 116 } | |
| 119 | 117 |
| 144 | 118 public IPromise<T> Cast<T>() { |
| 119 throw new InvalidCastException(); | |
| 119 | 120 } |
| 121 | |
| 122 public void Join() { | |
| 123 WaitResult(-1); | |
| 124 } | |
| 125 | |
| 144 | 126 public void Join(int timeout) { |
| 127 WaitResult(timeout); | |
| 119 | 128 } |
| 129 | |
| 144 | 130 protected void SetResult() { |
| 131 BeginSetResult(); | |
| 132 EndSetResult(); | |
|
138
f75cfa58e3d4
added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents:
130
diff
changeset
|
133 } |
| 119 | 134 } |
| 135 } | |
| 136 |
