# HG changeset patch # User cin # Date 2014-08-14 14:08:09 # Node ID 790e8a997d303ef3a3eadd0c4ea4126eb359bbe0 # Parent 653c4e04968b16bc8635ce941831560dcd0f5afb Refactoring diff --git a/Implab.Diagnostics.Interactive/InteractiveListener.cs b/Implab.Diagnostics.Interactive/InteractiveListener.cs --- a/Implab.Diagnostics.Interactive/InteractiveListener.cs +++ b/Implab.Diagnostics.Interactive/InteractiveListener.cs @@ -16,8 +16,8 @@ namespace Implab.Diagnostics.Interactive 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 --git a/Implab/Diagnostics/TraceContext.cs b/Implab/Diagnostics/TraceContext.cs --- a/Implab/Diagnostics/TraceContext.cs +++ b/Implab/Diagnostics/TraceContext.cs @@ -211,7 +211,7 @@ namespace Implab.Diagnostics { } } - public void BindLogicalOperationToPromise(IPromiseBase promise) { + public void BindLogicalOperationToPromise(IPromise promise) { Safe.ArgumentNotNull(promise, "promise"); var ctx = DetachLogicalOperation(); diff --git a/Implab/Diagnostics/TraceLog.cs b/Implab/Diagnostics/TraceLog.cs --- a/Implab/Diagnostics/TraceLog.cs +++ b/Implab/Diagnostics/TraceLog.cs @@ -28,7 +28,7 @@ namespace Implab.Diagnostics { } [Conditional("TRACE")] - public static void BindLogicalOperationToPromise(IPromiseBase promise) { + public static void BindLogicalOperationToPromise(IPromise promise) { TraceContext.Current.BindLogicalOperationToPromise(promise); } diff --git a/Implab/IPromise.cs b/Implab/IPromise.cs --- a/Implab/IPromise.cs +++ b/Implab/IPromise.cs @@ -3,29 +3,35 @@ using System.Collections.Generic; 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 --git a/Implab/IPromiseBase.cs b/Implab/IPromiseBase.cs deleted file mode 100644 --- a/Implab/IPromiseBase.cs +++ /dev/null @@ -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 --git a/Implab/IPromiseT.cs b/Implab/IPromiseT.cs new file mode 100644 --- /dev/null +++ b/Implab/IPromiseT.cs @@ -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 --git a/Implab/Implab.csproj b/Implab/Implab.csproj --- a/Implab/Implab.csproj +++ b/Implab/Implab.csproj @@ -51,8 +51,8 @@ + - diff --git a/Implab/Parallels/AsyncPool.cs b/Implab/Parallels/AsyncPool.cs --- a/Implab/Parallels/AsyncPool.cs +++ b/Implab/Parallels/AsyncPool.cs @@ -48,7 +48,7 @@ namespace Implab.Parallels { } - public static IPromiseBase InvokeNewThread(Action func) { + public static IPromise InvokeNewThread(Action func) { var p = new Promise(); var caller = TraceContext.Snapshot(); diff --git a/Implab/Promise.cs b/Implab/Promise.cs --- a/Implab/Promise.cs +++ b/Implab/Promise.cs @@ -99,7 +99,7 @@ namespace Implab { 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 @@ namespace Implab { 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 @@ namespace Implab { /// /// Если в коллекции встречаюься 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 @@ namespace Implab { #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 --git a/Implab/Safe.cs b/Implab/Safe.cs --- a/Implab/Safe.cs +++ b/Implab/Safe.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; +using System.Diagnostics; namespace Implab { @@ -36,5 +37,30 @@ namespace Implab 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); + } + } } }