Mercurial > pub > ImplabNet
diff Implab/Promise.cs @ 7:7ea9363fef6c promises
inital progress handling
author | cin |
---|---|
date | Fri, 01 Nov 2013 16:03:08 +0400 |
parents | dfa21d507bc5 |
children | c82e0dfbb4dd |
line wrap: on
line diff
--- a/Implab/Promise.cs Mon Oct 21 02:34:31 2013 +0400 +++ b/Implab/Promise.cs Fri Nov 01 16:03:08 2013 +0400 @@ -48,30 +48,32 @@ /// только инициатор обещания иначе могут возникнуть противоречия. /// </para> /// </remarks> - public class Promise<T> { + public class Promise<T>: IPromise { struct ResultHandlerInfo { public ResultHandler<T> resultHandler; public ErrorHandler errorHandler; - } - - enum State { - Unresolved, - Resolving, - Resolved, - Cancelled + public Action cancelHandler; } LinkedList<ResultHandlerInfo> m_handlersChain = new LinkedList<ResultHandlerInfo>(); - State m_state; + PromiseState m_state; bool m_cancellable; T m_result; Exception m_error; + IPromise m_parent; + int m_childrenCount; public Promise() { m_cancellable = true; } + public Promise(IPromise parent, bool cancellable) + { + m_cancellable = cancellable; + m_parent = parent; + } + /// <summary> /// Событие, возникающее при отмене асинхронной операции. /// </summary> @@ -87,12 +89,12 @@ /// <exception cref="InvalidOperationException">Данное обещание уже выполнено</exception> public void Resolve(T result) { lock (this) { - if (m_state == State.Cancelled) + if (m_state == PromiseState.Cancelled) return; - if (m_state != State.Unresolved) + if (m_state != PromiseState.Unresolved) throw new InvalidOperationException("The promise is already resolved"); m_result = result; - m_state = State.Resolving; + m_state = PromiseState.Resolved; } ResultHandlerInfo handler; @@ -107,12 +109,12 @@ /// <exception cref="InvalidOperationException">Данное обещание уже выполнено</exception> public void Reject(Exception error) { lock (this) { - if (m_state == State.Cancelled) + if (m_state == PromiseState.Cancelled) return; - if (m_state != State.Unresolved) + if (m_state != PromiseState.Unresolved) throw new InvalidOperationException("The promise is already resolved"); m_error = error; - m_state = State.Resolving; + m_state = PromiseState.Rejected; } ResultHandlerInfo handler; @@ -126,8 +128,9 @@ /// <returns><c>true</c> Операция была отменена, обработчики не будут вызваны.<c>false</c> отмена не возможна, поскольку обещание уже выполнено и обработчики отработали.</returns> public bool Cancel() { lock (this) { - if (m_state == State.Unresolved && m_cancellable) { - m_state = State.Cancelled; + if (m_state == PromiseState.Unresolved && m_cancellable) + { + m_state = PromiseState.Cancelled; EventHandler temp = Cancelled; if (temp != null) @@ -348,14 +351,13 @@ handler = default(ResultHandlerInfo); lock (this) { - Debug.Assert(m_state == State.Resolving); + Debug.Assert(m_state != PromiseState.Unresolved); if (m_handlersChain.Count > 0) { handler = m_handlersChain.First.Value; m_handlersChain.RemoveFirst(); return true; } else { - m_state = State.Resolved; return false; } } @@ -365,7 +367,7 @@ bool invokeRequired = false; lock (this) { - if (m_state != State.Resolved) + if (m_state == PromiseState.Unresolved) m_handlersChain.AddLast(handler); else invokeRequired = true;