Mercurial > pub > ImplabNet
annotate Implab/AbstractPromise.cs @ 246:5aa9cfbe56c3 v3
missing files
author | cin |
---|---|
date | Fri, 26 Jan 2018 11:19:38 +0300 |
parents | eee3e49dd1ff |
children | fb70574741a1 |
rev | line source |
---|---|
119 | 1 using System; |
243 | 2 using System.Diagnostics; |
3 using System.Reflection; | |
119 | 4 using Implab.Parallels; |
5 | |
6 namespace Implab { | |
244 | 7 public class AbstractPromise : AbstractEvent<IResolvable>, IPromise { |
243 | 8 |
244 | 9 class ResolvableSignal : IResolvable { |
10 public Signal Signal { get; private set; } | |
11 public ResolvableSignal() { | |
12 Signal = new Signal(); | |
144 | 13 } |
119 | 14 |
244 | 15 |
16 public void Reject(Exception error) { | |
17 Signal.Set(); | |
144 | 18 } |
119 | 19 |
244 | 20 public void Resolve() { |
21 Signal.Set(); | |
119 | 22 } |
23 } | |
24 | |
243 | 25 PromiseState m_state; |
26 | |
27 Exception m_error; | |
28 | |
29 public bool IsRejected { | |
30 get { | |
31 return m_state == PromiseState.Rejected; | |
32 } | |
33 } | |
34 | |
244 | 35 public bool IsFulfilled { |
243 | 36 get { |
244 | 37 return m_state == PromiseState.Fulfilled; |
243 | 38 } |
39 } | |
40 | |
41 public Exception RejectReason { | |
42 get { | |
43 return m_error; | |
44 } | |
45 } | |
46 | |
119 | 47 |
244 | 48 internal void Resolve() { |
49 if (BeginTransit()) | |
50 CompleteResolve(); | |
51 } | |
52 | |
53 internal void Reject(Exception reason) { | |
54 if (BeginTransit()) { | |
55 m_error = reason; | |
56 m_state = PromiseState.Rejected; | |
57 CompleteTransit(); | |
58 } | |
59 } | |
60 | |
61 | |
144 | 62 #region implemented abstract members of AbstractPromise |
119 | 63 |
244 | 64 protected override void SignalHandler(IResolvable handler) { |
243 | 65 switch (m_state) { |
244 | 66 case PromiseState.Fulfilled: |
67 handler.Resolve(); | |
156
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
145
diff
changeset
|
68 break; |
243 | 69 case PromiseState.Rejected: |
244 | 70 handler.Reject(RejectReason); |
156
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
145
diff
changeset
|
71 break; |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
145
diff
changeset
|
72 default: |
243 | 73 throw new InvalidOperationException(String.Format("Invalid promise signal: {0}", m_state)); |
156
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
145
diff
changeset
|
74 } |
119 | 75 } |
76 | |
242 | 77 protected override Signal GetFulfillSignal() { |
244 | 78 var next = new ResolvableSignal(); |
79 Then(next); | |
80 return next.Signal; | |
119 | 81 } |
82 | |
83 #endregion | |
84 | |
243 | 85 protected void CompleteResolve() { |
244 | 86 m_state = PromiseState.Fulfilled; |
243 | 87 CompleteTransit(); |
88 } | |
89 | |
242 | 90 public Type ResultType { |
144 | 91 get { |
92 return typeof(void); | |
119 | 93 } |
94 } | |
95 | |
243 | 96 |
97 protected void Rethrow() { | |
98 Debug.Assert(m_error != null); | |
99 if (m_error is OperationCanceledException) | |
100 throw new OperationCanceledException("Operation cancelled", m_error); | |
101 else | |
102 throw new TargetInvocationException(m_error); | |
103 } | |
104 | |
244 | 105 public void Then(IResolvable next) { |
106 AddHandler(next); | |
144 | 107 } |
119 | 108 |
144 | 109 public IPromise<T> Cast<T>() { |
110 throw new InvalidCastException(); | |
119 | 111 } |
112 | |
113 public void Join() { | |
114 WaitResult(-1); | |
243 | 115 if (IsRejected) |
116 Rethrow(); | |
119 | 117 } |
118 | |
144 | 119 public void Join(int timeout) { |
120 WaitResult(timeout); | |
119 | 121 } |
122 } | |
123 } | |
124 |