Mercurial > pub > ImplabNet
annotate Implab/AbstractPromise.cs @ 242:cbe10ac0731e v3
Working on promises
author | cin |
---|---|
date | Wed, 24 Jan 2018 03:03:21 +0300 |
parents | 86187b01c4e0 |
children | b1e0ffdf3451 |
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 { |
242 | 6 public class HandlerDescriptor { |
144 | 7 readonly Action m_handler; |
8 readonly Action<Exception> m_error; | |
242 | 9 public HandlerDescriptor(Action success, Action<Exception> error) { |
144 | 10 m_handler = success; |
11 m_error = error; | |
12 } | |
119 | 13 |
144 | 14 public void SignalSuccess() { |
242 | 15 if (m_handler != null) { |
144 | 16 try { |
17 m_handler(); | |
197
86187b01c4e0
fixed: the error handler should not handle handlers errors
cin
parents:
186
diff
changeset
|
18 // Analysis disable once EmptyGeneralCatchClause |
86187b01c4e0
fixed: the error handler should not handle handlers errors
cin
parents:
186
diff
changeset
|
19 } catch { |
144 | 20 } |
21 } | |
22 } | |
119 | 23 |
144 | 24 public void SignalError(Exception err) { |
25 if (m_error != null) { | |
26 try { | |
27 m_error(err); | |
28 // Analysis disable once EmptyGeneralCatchClause | |
29 } catch { | |
30 } | |
31 } | |
119 | 32 } |
33 } | |
34 | |
35 | |
144 | 36 #region implemented abstract members of AbstractPromise |
119 | 37 |
156
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
145
diff
changeset
|
38 protected override void SignalHandler(HandlerDescriptor handler, int signal) { |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
145
diff
changeset
|
39 switch (signal) { |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
145
diff
changeset
|
40 case SUCCEEDED_STATE: |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
145
diff
changeset
|
41 handler.SignalSuccess(); |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
145
diff
changeset
|
42 break; |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
145
diff
changeset
|
43 case REJECTED_STATE: |
242 | 44 handler.SignalError(RejectReason); |
156
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
145
diff
changeset
|
45 break; |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
145
diff
changeset
|
46 default: |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
145
diff
changeset
|
47 throw new InvalidOperationException(String.Format("Invalid promise signal: {0}", signal)); |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
145
diff
changeset
|
48 } |
119 | 49 } |
50 | |
242 | 51 protected override Signal GetFulfillSignal() { |
144 | 52 var signal = new Signal(); |
242 | 53 On(signal.Set, e => signal.Set()); |
145 | 54 return signal; |
119 | 55 } |
56 | |
57 #endregion | |
58 | |
242 | 59 public Type ResultType { |
144 | 60 get { |
61 return typeof(void); | |
119 | 62 } |
63 } | |
64 | |
242 | 65 public void On(Action success, Action<Exception> error) { |
66 AddHandler(new HandlerDescriptor(success, error)); | |
144 | 67 } |
119 | 68 |
144 | 69 public IPromise<T> Cast<T>() { |
70 throw new InvalidCastException(); | |
119 | 71 } |
72 | |
73 public void Join() { | |
74 WaitResult(-1); | |
75 } | |
76 | |
144 | 77 public void Join(int timeout) { |
78 WaitResult(timeout); | |
119 | 79 } |
80 | |
144 | 81 protected void SetResult() { |
186 | 82 if(BeginSetResult()) |
83 EndSetResult(); | |
138
f75cfa58e3d4
added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents:
130
diff
changeset
|
84 } |
119 | 85 } |
86 } | |
87 |