changeset 66:790e8a997d30

Refactoring
author cin
date Thu, 14 Aug 2014 18:08:09 +0400
parents 653c4e04968b
children b4c2454d208e
files Implab.Diagnostics.Interactive/InteractiveListener.cs Implab/Diagnostics/TraceContext.cs Implab/Diagnostics/TraceLog.cs Implab/IPromise.cs Implab/IPromiseBase.cs Implab/IPromiseT.cs Implab/Implab.csproj Implab/Parallels/AsyncPool.cs Implab/Promise.cs Implab/Safe.cs
diffstat 10 files changed, 97 insertions(+), 71 deletions(-) [+]
line wrap: on
line diff
--- 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<object> m_guiStarted = new Promise<object>();
         
-        readonly IPromiseBase m_guiFinished;
-        readonly IPromiseBase m_workerFinished = new Promise<object>();
+        readonly IPromise m_guiFinished;
+        readonly IPromise m_workerFinished = new Promise<object>();
         
         readonly MTQueue<TraceViewItem> m_queue = new MTQueue<TraceViewItem>();
         readonly AutoResetEvent m_queueEvent = new AutoResetEvent(false);
--- 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();
--- 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);
         }
 
--- 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<T>: IPromiseBase
-    {
+namespace Implab {
+    public interface IPromise: ICancellable {
+        /// <summary>
+        /// Check whereather the promise has no more than one dependent promise.
+        /// </summary>
+        bool IsExclusive {
+            get;
+        }
 
-        new T Join();
-        new T Join(int timeout);
+        /// <summary>
+        /// Тип результата, получаемого через данное обещание.
+        /// </summary>
+        Type PromiseType { get; }
 
-        IPromise<T> Then(ResultHandler<T> success, ErrorHandler error);
-        IPromise<T> Then(ResultHandler<T> success, ErrorHandler<T> error);
-        IPromise<T> Then(ResultHandler<T> success);
-        new IPromise<T> Error(ErrorHandler error);
-        IPromise<T> Error(ErrorHandler<T> error);
+        bool IsResolved { get; }
+
+        bool IsCancelled { get; }
 
-        IPromise<T2> Map<T2>(ResultMapper<T,T2> mapper, ErrorHandler error);
-        IPromise<T2> Map<T2>(ResultMapper<T, T2> 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<T2> Chain<T2>(ChainedOperation<T, T2> chained, ErrorHandler error);
-        IPromise<T2> Chain<T2>(ChainedOperation<T, T2> chained);
+        IPromise<T> Cast<T>();
 
-        new IPromise<T> Cancelled(Action handler);
-        new IPromise<T> Finally(Action handler);
-        new IPromise<T> Anyway(Action handler);
+        void Join();
+        void Join(int timeout);
 
     }
 }
--- 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 {
-        /// <summary>
-        /// Check whereather the promise has no more than one dependent promise.
-        /// </summary>
-        bool IsExclusive {
-            get;
-        }
-
-        /// <summary>
-        /// Тип результата, получаемого через данное обещание.
-        /// </summary>
-        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<T> Cast<T>();
-
-        void Join();
-        void Join(int timeout);
-
-    }
-}
--- /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<T>: IPromise
+    {
+
+        new T Join();
+        new T Join(int timeout);
+
+        IPromise<T> Then(ResultHandler<T> success, ErrorHandler error);
+        IPromise<T> Then(ResultHandler<T> success, ErrorHandler<T> error);
+        IPromise<T> Then(ResultHandler<T> success);
+        new IPromise<T> Error(ErrorHandler error);
+        IPromise<T> Error(ErrorHandler<T> error);
+
+        IPromise<T2> Map<T2>(ResultMapper<T,T2> mapper, ErrorHandler error);
+        IPromise<T2> Map<T2>(ResultMapper<T, T2> mapper);
+
+        IPromise<T2> Chain<T2>(ChainedOperation<T, T2> chained, ErrorHandler error);
+        IPromise<T2> Chain<T2>(ChainedOperation<T, T2> chained);
+
+        new IPromise<T> Cancelled(Action handler);
+        new IPromise<T> Finally(Action handler);
+        new IPromise<T> Anyway(Action handler);
+
+    }
+}
--- 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 @@
     <Compile Include="ICancellable.cs" />
     <Compile Include="IProgressHandler.cs" />
     <Compile Include="IProgressNotifier.cs" />
+    <Compile Include="IPromiseT.cs" />
     <Compile Include="IPromise.cs" />
-    <Compile Include="IPromiseBase.cs" />
     <Compile Include="IServiceLocator.cs" />
     <Compile Include="ITaskController.cs" />
     <Compile Include="JSON\JSONElementContext.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<object>();
 
             var caller = TraceContext.Snapshot();
--- 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 @@
         /// <remarks>
         /// Если в коллекции встречаюься <c>null</c>, то они воспринимаются как выполненные обещания.
         /// </remarks>
-        public static IPromiseBase CreateComposite(ICollection<IPromiseBase> promises) {
+        public static IPromise CreateComposite(ICollection<IPromise> 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);
         }
 
--- 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<T> GuargPromise<T>(Func<T> action) {
+            ArgumentNotNull(action, "action");
+
+            var p = new Promise<T>();
+            try {
+                p.Resolve(action());
+            } catch (Exception err) {
+                p.Reject(err);
+            }
+
+            return p;
+        }
+
+        [DebuggerStepThrough]
+        public static IPromise<T> GuardPromise<T>(Func<IPromise<T>> action) {
+            ArgumentNotNull(action, "action");
+
+            try {
+                return action();
+            } catch (Exception err) {
+                return Promise<T>.ExceptionToPromise(err);
+            }
+        }
     }
 }