Mercurial > pub > ImplabNet
comparison Implab/AbstractPromise.cs @ 192:f1da3afc3521 release v2.1
Слияние с v2
| author | cin |
|---|---|
| date | Fri, 22 Apr 2016 13:10:34 +0300 |
| parents | 75103928da09 |
| children | 86187b01c4e0 |
comparison
equal
deleted
inserted
replaced
| 71:1714fd8678ef | 192:f1da3afc3521 |
|---|---|
| 1 using System; | |
| 2 using Implab.Parallels; | |
| 3 | |
| 4 namespace Implab { | |
| 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; | |
| 11 | |
| 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 } | |
| 18 | |
| 19 public HandlerDescriptor(Action handler, PromiseEventType mask) { | |
| 20 m_handler = handler; | |
| 21 m_error = null; | |
| 22 m_cancel = null; | |
| 23 m_mask = mask; | |
| 24 } | |
| 25 | |
| 26 public void SignalSuccess() { | |
| 27 if ((m_mask & PromiseEventType.Success) != 0 && m_handler != null) { | |
| 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 } | |
| 37 | |
| 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 } | |
| 45 } else if ((m_mask & PromiseEventType.Error ) != 0 && m_handler != null) { | |
| 46 try { | |
| 47 m_handler(); | |
| 48 // Analysis disable once EmptyGeneralCatchClause | |
| 49 } catch { | |
| 50 } | |
| 51 } | |
| 52 } | |
| 53 | |
| 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 } | |
| 61 } else if ( (m_mask & PromiseEventType.Cancelled) != 0 && m_handler != null) { | |
| 62 try { | |
| 63 m_handler(); | |
| 64 // Analysis disable once EmptyGeneralCatchClause | |
| 65 } catch { | |
| 66 } | |
| 67 } | |
| 68 } | |
| 69 } | |
| 70 | |
| 71 | |
| 72 #region implemented abstract members of AbstractPromise | |
| 73 | |
| 74 protected override void SignalHandler(HandlerDescriptor handler, int signal) { | |
| 75 switch (signal) { | |
| 76 case SUCCEEDED_STATE: | |
| 77 handler.SignalSuccess(); | |
| 78 break; | |
| 79 case REJECTED_STATE: | |
| 80 handler.SignalError(Error); | |
| 81 break; | |
| 82 case CANCELLED_STATE: | |
| 83 handler.SignalCancel(CancellationReason); | |
| 84 break; | |
| 85 default: | |
| 86 throw new InvalidOperationException(String.Format("Invalid promise signal: {0}", signal)); | |
| 87 } | |
| 88 } | |
| 89 | |
| 90 protected override Signal GetResolveSignal() { | |
| 91 var signal = new Signal(); | |
| 92 On(signal.Set, PromiseEventType.All); | |
| 93 return signal; | |
| 94 } | |
| 95 | |
| 96 #endregion | |
| 97 | |
| 98 public Type PromiseType { | |
| 99 get { | |
| 100 return typeof(void); | |
| 101 } | |
| 102 } | |
| 103 | |
| 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; | |
| 112 } | |
| 113 | |
| 114 public IPromise On(Action success) { | |
| 115 AddHandler(new HandlerDescriptor(success, null, null)); | |
| 116 return this; | |
| 117 } | |
| 118 | |
| 119 public IPromise On(Action handler, PromiseEventType events) { | |
| 120 AddHandler(new HandlerDescriptor(handler,events)); | |
| 121 return this; | |
| 122 } | |
| 123 | |
| 124 public IPromise<T> Cast<T>() { | |
| 125 throw new InvalidCastException(); | |
| 126 } | |
| 127 | |
| 128 public void Join() { | |
| 129 WaitResult(-1); | |
| 130 } | |
| 131 | |
| 132 public void Join(int timeout) { | |
| 133 WaitResult(timeout); | |
| 134 } | |
| 135 | |
| 136 protected void SetResult() { | |
| 137 if(BeginSetResult()) | |
| 138 EndSetResult(); | |
| 139 } | |
| 140 } | |
| 141 } | |
| 142 |
