changeset 143:16f926ee499d v2

DRAFT: refactoring, adding cancelation token
author cin
date Wed, 04 Mar 2015 18:05:39 +0300
parents 2100965eb97f
children 8c0b95069066
files Implab/AbstractPromise.cs Implab/ICancelationToken.cs Implab/IDeferred.cs Implab/IDeferredT.cs Implab/Implab.csproj
diffstat 5 files changed, 41 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/Implab/AbstractPromise.cs	Wed Mar 04 03:10:38 2015 +0300
+++ b/Implab/AbstractPromise.cs	Wed Mar 04 18:05:39 2015 +0300
@@ -286,18 +286,6 @@
 
         #endregion
 
-        #region ICancellable implementation
-
-        public void Cancel() {
-            SetCancelled(null);
-        }
-
-        public void Cancel(Exception reason) {
-            SetCancelled(reason);
-        }
-
-        #endregion
-
         public Exception Error {
             get {
                 return m_error;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Implab/ICancelationToken.cs	Wed Mar 04 18:05:39 2015 +0300
@@ -0,0 +1,36 @@
+using System;
+
+namespace Implab {
+    public interface ICancelationToken {
+        /// <summary>
+        /// Indicates wherther the cancellation was requested.
+        /// </summary>
+        bool IsCancelRequested { get ; }
+
+        /// <summary>
+        /// The reason why the operation should be cancelled.
+        /// </summary>
+        Exception CancelReason { get ; }
+
+        /// <summary>
+        /// Accepts if requested.
+        /// </summary>
+        /// <returns><c>true</c>, if if requested was accepted, <c>false</c> otherwise.</returns>
+        bool AcceptIfRequested();
+
+        /// <summary>
+        /// Sets the token to cancelled state.
+        /// </summary>
+        /// <param name="reason">The reason why the operation was cancelled.</param>
+        void SetCancelled(Exception reason);
+
+        /// <summary>
+        /// Adds the listener for the cancellation request, is the cancellation was requested the <paramref name="handler"/>
+        /// is executed immediatelly.
+        /// </summary>
+        /// <param name="handler">The handler which will be executed if the cancel occurs.</param>
+        void CancellationRequested(Action<Exception> handler);
+
+    }
+}
+
--- a/Implab/IDeferred.cs	Wed Mar 04 03:10:38 2015 +0300
+++ b/Implab/IDeferred.cs	Wed Mar 04 18:05:39 2015 +0300
@@ -4,7 +4,7 @@
     /// <summary>
     /// Deferred result, usually used by asynchronous services as the service part of the promise.
     /// </summary>
-    public interface IDeferred : ICancellable {
+    public interface IDeferred : ICancelationToken {
 
         void Resolve();
 
--- a/Implab/IDeferredT.cs	Wed Mar 04 03:10:38 2015 +0300
+++ b/Implab/IDeferredT.cs	Wed Mar 04 18:05:39 2015 +0300
@@ -1,7 +1,7 @@
 using System;
 
 namespace Implab {
-    public interface IDeferred<T> : ICancellable {
+    public interface IDeferred<T> : ICancelationToken {
         void Resolve(T value);
 
         void Reject(Exception error);
--- a/Implab/Implab.csproj	Wed Mar 04 03:10:38 2015 +0300
+++ b/Implab/Implab.csproj	Wed Mar 04 18:05:39 2015 +0300
@@ -7,6 +7,8 @@
     <OutputType>Library</OutputType>
     <RootNamespace>Implab</RootNamespace>
     <AssemblyName>Implab</AssemblyName>
+    <ProductVersion>8.0.30703</ProductVersion>
+    <SchemaVersion>2.0</SchemaVersion>
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
     <DebugSymbols>true</DebugSymbols>
@@ -156,6 +158,7 @@
     <Compile Include="Diagnostics\ILogWriter.cs" />
     <Compile Include="Diagnostics\ListenerBase.cs" />
     <Compile Include="Parallels\BlockingQueue.cs" />
+    <Compile Include="ICancelationToken.cs" />
   </ItemGroup>
   <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
   <ItemGroup />