# HG changeset patch # User cin # Date 1408025289 -14400 # Node ID 790e8a997d303ef3a3eadd0c4ea4126eb359bbe0 # Parent 653c4e04968b16bc8635ce941831560dcd0f5afb Refactoring diff -r 653c4e04968b -r 790e8a997d30 Implab.Diagnostics.Interactive/InteractiveListener.cs --- a/Implab.Diagnostics.Interactive/InteractiveListener.cs Mon Jun 30 13:55:22 2014 +0400 +++ b/Implab.Diagnostics.Interactive/InteractiveListener.cs Thu Aug 14 18:08:09 2014 +0400 @@ -16,8 +16,8 @@ SynchronizationContext m_syncGuiThread; readonly Promise m_guiStarted = new Promise(); - readonly IPromiseBase m_guiFinished; - readonly IPromiseBase m_workerFinished = new Promise(); + readonly IPromise m_guiFinished; + readonly IPromise m_workerFinished = new Promise(); readonly MTQueue m_queue = new MTQueue(); readonly AutoResetEvent m_queueEvent = new AutoResetEvent(false); diff -r 653c4e04968b -r 790e8a997d30 Implab/Diagnostics/TraceContext.cs --- a/Implab/Diagnostics/TraceContext.cs Mon Jun 30 13:55:22 2014 +0400 +++ b/Implab/Diagnostics/TraceContext.cs Thu Aug 14 18:08:09 2014 +0400 @@ -211,7 +211,7 @@ } } - public void BindLogicalOperationToPromise(IPromiseBase promise) { + public void BindLogicalOperationToPromise(IPromise promise) { Safe.ArgumentNotNull(promise, "promise"); var ctx = DetachLogicalOperation(); diff -r 653c4e04968b -r 790e8a997d30 Implab/Diagnostics/TraceLog.cs --- a/Implab/Diagnostics/TraceLog.cs Mon Jun 30 13:55:22 2014 +0400 +++ b/Implab/Diagnostics/TraceLog.cs Thu Aug 14 18:08:09 2014 +0400 @@ -28,7 +28,7 @@ } [Conditional("TRACE")] - public static void BindLogicalOperationToPromise(IPromiseBase promise) { + public static void BindLogicalOperationToPromise(IPromise promise) { TraceContext.Current.BindLogicalOperationToPromise(promise); } diff -r 653c4e04968b -r 790e8a997d30 Implab/IPromise.cs --- a/Implab/IPromise.cs Mon Jun 30 13:55:22 2014 +0400 +++ b/Implab/IPromise.cs Thu Aug 14 18:08:09 2014 +0400 @@ -3,29 +3,35 @@ using System.Linq; using System.Text; -namespace Implab -{ - public interface IPromise: IPromiseBase - { +namespace Implab { + public interface IPromise: ICancellable { + /// + /// Check whereather the promise has no more than one dependent promise. + /// + bool IsExclusive { + get; + } - new T Join(); - new T Join(int timeout); + /// + /// Тип результата, получаемого через данное обещание. + /// + Type PromiseType { get; } - IPromise Then(ResultHandler success, ErrorHandler error); - IPromise Then(ResultHandler success, ErrorHandler error); - IPromise Then(ResultHandler success); - new IPromise Error(ErrorHandler error); - IPromise Error(ErrorHandler error); + bool IsResolved { get; } + + bool IsCancelled { get; } - IPromise Map(ResultMapper mapper, ErrorHandler error); - IPromise Map(ResultMapper mapper); + IPromise Then(Action success,ErrorHandler error); + IPromise Then(Action success); + IPromise Error(ErrorHandler error); + IPromise Anyway(Action handler); + IPromise Finally(Action handler); + IPromise Cancelled(Action handler); - IPromise Chain(ChainedOperation chained, ErrorHandler error); - IPromise Chain(ChainedOperation chained); + IPromise Cast(); - new IPromise Cancelled(Action handler); - new IPromise Finally(Action handler); - new IPromise Anyway(Action handler); + void Join(); + void Join(int timeout); } } diff -r 653c4e04968b -r 790e8a997d30 Implab/IPromiseBase.cs --- a/Implab/IPromiseBase.cs Mon Jun 30 13:55:22 2014 +0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,37 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace Implab { - public interface IPromiseBase: ICancellable { - /// - /// Check whereather the promise has no more than one dependent promise. - /// - bool IsExclusive { - get; - } - - /// - /// Тип результата, получаемого через данное обещание. - /// - Type PromiseType { get; } - - bool IsResolved { get; } - - bool IsCancelled { get; } - - IPromiseBase Then(Action success,ErrorHandler error); - IPromiseBase Then(Action success); - IPromiseBase Error(ErrorHandler error); - IPromiseBase Anyway(Action handler); - IPromiseBase Finally(Action handler); - IPromiseBase Cancelled(Action handler); - - IPromise Cast(); - - void Join(); - void Join(int timeout); - - } -} diff -r 653c4e04968b -r 790e8a997d30 Implab/IPromiseT.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Implab/IPromiseT.cs Thu Aug 14 18:08:09 2014 +0400 @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Implab +{ + public interface IPromise: IPromise + { + + new T Join(); + new T Join(int timeout); + + IPromise Then(ResultHandler success, ErrorHandler error); + IPromise Then(ResultHandler success, ErrorHandler error); + IPromise Then(ResultHandler success); + new IPromise Error(ErrorHandler error); + IPromise Error(ErrorHandler error); + + IPromise Map(ResultMapper mapper, ErrorHandler error); + IPromise Map(ResultMapper mapper); + + IPromise Chain(ChainedOperation chained, ErrorHandler error); + IPromise Chain(ChainedOperation chained); + + new IPromise Cancelled(Action handler); + new IPromise Finally(Action handler); + new IPromise Anyway(Action handler); + + } +} diff -r 653c4e04968b -r 790e8a997d30 Implab/Implab.csproj --- a/Implab/Implab.csproj Mon Jun 30 13:55:22 2014 +0400 +++ b/Implab/Implab.csproj Thu Aug 14 18:08:09 2014 +0400 @@ -51,8 +51,8 @@ + - diff -r 653c4e04968b -r 790e8a997d30 Implab/Parallels/AsyncPool.cs --- a/Implab/Parallels/AsyncPool.cs Mon Jun 30 13:55:22 2014 +0400 +++ b/Implab/Parallels/AsyncPool.cs Thu Aug 14 18:08:09 2014 +0400 @@ -48,7 +48,7 @@ } - public static IPromiseBase InvokeNewThread(Action func) { + public static IPromise InvokeNewThread(Action func) { var p = new Promise(); var caller = TraceContext.Snapshot(); diff -r 653c4e04968b -r 790e8a997d30 Implab/Promise.cs --- a/Implab/Promise.cs Mon Jun 30 13:55:22 2014 +0400 +++ b/Implab/Promise.cs Thu Aug 14 18:08:09 2014 +0400 @@ -99,7 +99,7 @@ m_cancellable = true; } - public Promise(IPromiseBase parent, bool cancellable) { + public Promise(IPromise parent, bool cancellable) { m_cancellable = cancellable; if (parent != null) AddHandler( @@ -252,11 +252,11 @@ return medium; } - public IPromiseBase Then(Action success, ErrorHandler error) { + public IPromise Then(Action success, ErrorHandler error) { return Then(x => success(), error); } - public IPromiseBase Then(Action success) { + public IPromise Then(Action success) { return Then(x => success()); } @@ -682,7 +682,7 @@ /// /// Если в коллекции встречаюься null, то они воспринимаются как выполненные обещания. /// - public static IPromiseBase CreateComposite(ICollection promises) { + public static IPromise CreateComposite(ICollection promises) { if (promises == null) throw new ArgumentNullException(); if (promises.Count == 0) @@ -730,27 +730,27 @@ #region IPromiseBase explicit implementation - IPromiseBase IPromiseBase.Error(ErrorHandler error) { + IPromise IPromise.Error(ErrorHandler error) { return Error(error); } - IPromiseBase IPromiseBase.Anyway(Action handler) { + IPromise IPromise.Anyway(Action handler) { return Anyway(handler); } - IPromiseBase IPromiseBase.Finally(Action handler) { + IPromise IPromise.Finally(Action handler) { return Finally(handler); } - IPromiseBase IPromiseBase.Cancelled(Action handler) { + IPromise IPromise.Cancelled(Action handler) { return Cancelled(handler); } - void IPromiseBase.Join() { + void IPromise.Join() { Join(); } - void IPromiseBase.Join(int timeout) { + void IPromise.Join(int timeout) { Join(timeout); } diff -r 653c4e04968b -r 790e8a997d30 Implab/Safe.cs --- a/Implab/Safe.cs Mon Jun 30 13:55:22 2014 +0400 +++ b/Implab/Safe.cs Thu Aug 14 18:08:09 2014 +0400 @@ -3,6 +3,7 @@ using System.Linq; using System.Text; using System.Text.RegularExpressions; +using System.Diagnostics; namespace Implab { @@ -36,5 +37,30 @@ if (disp != null) disp.Dispose(); } + + [DebuggerStepThrough] + public static IPromise GuargPromise(Func action) { + ArgumentNotNull(action, "action"); + + var p = new Promise(); + try { + p.Resolve(action()); + } catch (Exception err) { + p.Reject(err); + } + + return p; + } + + [DebuggerStepThrough] + public static IPromise GuardPromise(Func> action) { + ArgumentNotNull(action, "action"); + + try { + return action(); + } catch (Exception err) { + return Promise.ExceptionToPromise(err); + } + } } }