##// END OF EJS Templates
working on promises
working on promises

File last commit:

r244:eee3e49dd1ff v3
r245:b904e0a3ba72 v3
Show More
AbstractPromise.cs
124 lines | 3.0 KiB | text/x-csharp | CSharpLexer
/ Implab / AbstractPromise.cs
cin
Promises rewritten, added improved version of AsyncQueue
r119 using System;
cin
working on promises
r243 using System.Diagnostics;
using System.Reflection;
cin
Promises rewritten, added improved version of AsyncQueue
r119 using Implab.Parallels;
namespace Implab {
cin
working on promises
r244 public class AbstractPromise : AbstractEvent<IResolvable>, IPromise {
cin
working on promises
r243
cin
working on promises
r244 class ResolvableSignal : IResolvable {
public Signal Signal { get; private set; }
public ResolvableSignal() {
Signal = new Signal();
cin
DRAFT: refactoring
r144 }
cin
Promises rewritten, added improved version of AsyncQueue
r119
cin
working on promises
r244
public void Reject(Exception error) {
Signal.Set();
cin
DRAFT: refactoring
r144 }
cin
Promises rewritten, added improved version of AsyncQueue
r119
cin
working on promises
r244 public void Resolve() {
Signal.Set();
cin
Promises rewritten, added improved version of AsyncQueue
r119 }
}
cin
working on promises
r243 PromiseState m_state;
Exception m_error;
public bool IsRejected {
get {
return m_state == PromiseState.Rejected;
}
}
cin
working on promises
r244 public bool IsFulfilled {
cin
working on promises
r243 get {
cin
working on promises
r244 return m_state == PromiseState.Fulfilled;
cin
working on promises
r243 }
}
public Exception RejectReason {
get {
return m_error;
}
}
cin
Promises rewritten, added improved version of AsyncQueue
r119
cin
working on promises
r244 internal void Resolve() {
if (BeginTransit())
CompleteResolve();
}
internal void Reject(Exception reason) {
if (BeginTransit()) {
m_error = reason;
m_state = PromiseState.Rejected;
CompleteTransit();
}
}
cin
DRAFT: refactoring
r144 #region implemented abstract members of AbstractPromise
cin
Promises rewritten, added improved version of AsyncQueue
r119
cin
working on promises
r244 protected override void SignalHandler(IResolvable handler) {
cin
working on promises
r243 switch (m_state) {
cin
working on promises
r244 case PromiseState.Fulfilled:
handler.Resolve();
cin
Promises: SignalXXX methods merged into SignalHandler method....
r156 break;
cin
working on promises
r243 case PromiseState.Rejected:
cin
working on promises
r244 handler.Reject(RejectReason);
cin
Promises: SignalXXX methods merged into SignalHandler method....
r156 break;
default:
cin
working on promises
r243 throw new InvalidOperationException(String.Format("Invalid promise signal: {0}", m_state));
cin
Promises: SignalXXX methods merged into SignalHandler method....
r156 }
cin
Promises rewritten, added improved version of AsyncQueue
r119 }
cin
Working on promises
r242 protected override Signal GetFulfillSignal() {
cin
working on promises
r244 var next = new ResolvableSignal();
Then(next);
return next.Signal;
cin
Promises rewritten, added improved version of AsyncQueue
r119 }
#endregion
cin
working on promises
r243 protected void CompleteResolve() {
cin
working on promises
r244 m_state = PromiseState.Fulfilled;
cin
working on promises
r243 CompleteTransit();
}
cin
Working on promises
r242 public Type ResultType {
cin
DRAFT: refactoring
r144 get {
return typeof(void);
cin
Promises rewritten, added improved version of AsyncQueue
r119 }
}
cin
working on promises
r243
protected void Rethrow() {
Debug.Assert(m_error != null);
if (m_error is OperationCanceledException)
throw new OperationCanceledException("Operation cancelled", m_error);
else
throw new TargetInvocationException(m_error);
}
cin
working on promises
r244 public void Then(IResolvable next) {
AddHandler(next);
cin
DRAFT: refactoring
r144 }
cin
Promises rewritten, added improved version of AsyncQueue
r119
cin
DRAFT: refactoring
r144 public IPromise<T> Cast<T>() {
throw new InvalidCastException();
cin
Promises rewritten, added improved version of AsyncQueue
r119 }
public void Join() {
WaitResult(-1);
cin
working on promises
r243 if (IsRejected)
Rethrow();
cin
Promises rewritten, added improved version of AsyncQueue
r119 }
cin
DRAFT: refactoring
r144 public void Join(int timeout) {
WaitResult(timeout);
cin
Promises rewritten, added improved version of AsyncQueue
r119 }
}
}