changeset 104:5f10d54b45df v2

renamed Promise.Last -> Promise.On Promise.On doesn't changes Promise.IsExclusive property
author cin
date Sun, 09 Nov 2014 23:03:45 +0300
parents b3f5bc613905
children 4d308952fd5e
files Implab.Fx/PromiseHelpers.cs Implab/IPromise.cs Implab/IPromiseT.cs Implab/Implab.csproj Implab/Parallels/ArrayTraits.cs Implab/Promise.cs Implab/PromiseEventType.cs Implab/PromiseExtensions.cs MonoPlay/Program.cs
diffstat 9 files changed, 124 insertions(+), 79 deletions(-) [+]
line wrap: on
line diff
--- a/Implab.Fx/PromiseHelpers.cs	Sat Nov 08 10:02:47 2014 +0300
+++ b/Implab.Fx/PromiseHelpers.cs	Sun Nov 09 23:03:45 2014 +0300
@@ -29,7 +29,7 @@
 
             var directed = new ControlBoundPromise<T>(ctl,that);
 
-            that.Last(
+            that.On(
                 directed.Resolve,
                 directed.Reject,
                 directed.Cancel
--- a/Implab/IPromise.cs	Sat Nov 08 10:02:47 2014 +0300
+++ b/Implab/IPromise.cs	Sun Nov 09 23:03:45 2014 +0300
@@ -41,9 +41,10 @@
         /// <param name="success">Success.</param>
         /// <param name="error">Error.</param>
         /// <param name="cancel">Cancel.</param>
-        void Last(Action success, Action<Exception> error, Action cancel);
-        void Last(Action success, Action<Exception> error);
-        void Last(Action success);
+        void On(Action success, Action<Exception> error, Action cancel);
+        void On(Action success, Action<Exception> error);
+        void On(Action success);
+        void On(Action success, PromiseEventType events);
 
         IPromise Error(Action<Exception> error);
         /// <summary>
--- a/Implab/IPromiseT.cs	Sat Nov 08 10:02:47 2014 +0300
+++ b/Implab/IPromiseT.cs	Sun Nov 09 23:03:45 2014 +0300
@@ -7,11 +7,11 @@
 
         new T Join(int timeout);
 
-        void Last(Action<T> success, Action<Exception> error, Action cancel);
+        void On(Action<T> success, Action<Exception> error, Action cancel);
         
-        void Last(Action<T> success, Action<Exception> error);
+        void On(Action<T> success, Action<Exception> error);
 
-        void Last(Action<T> success);
+        void On(Action<T> success);
 
         IPromise<T> Then(Action<T> success, Func<Exception,T> error, Action cancel);
 
--- a/Implab/Implab.csproj	Sat Nov 08 10:02:47 2014 +0300
+++ b/Implab/Implab.csproj	Sun Nov 09 23:03:45 2014 +0300
@@ -147,6 +147,7 @@
     <Compile Include="Diagnostics\Extensions.cs" />
     <Compile Include="IComponentContainer.cs" />
     <Compile Include="MTComponentContainer.cs" />
+    <Compile Include="PromiseEventType.cs" />
   </ItemGroup>
   <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
   <ItemGroup />
--- a/Implab/Parallels/ArrayTraits.cs	Sat Nov 08 10:02:47 2014 +0300
+++ b/Implab/Parallels/ArrayTraits.cs	Sun Nov 09 23:03:45 2014 +0300
@@ -183,7 +183,7 @@
                                     Monitor.Pulse(locker);
                                 }
                             })
-                            .Last(
+                            .On(
                                 x => {
                                     res[idx] = x;
                                     var left = Interlocked.Decrement(ref pending);
--- a/Implab/Promise.cs	Sat Nov 08 10:02:47 2014 +0300
+++ b/Implab/Promise.cs	Sun Nov 09 23:03:45 2014 +0300
@@ -117,7 +117,8 @@
                         if (parent.IsExclusive)
                             parent.Cancel();
                     },
-                    null
+                    null,
+                    false
                 );
         }
 
@@ -220,7 +221,7 @@
 
             var medium = new Promise<T>(this);
 
-            AddHandler(success, error, cancel, medium);
+            AddHandler(success, error, cancel, medium, true);
 
             return medium;
         }
@@ -238,7 +239,7 @@
 
             var medium = new Promise<T>(this);
 
-            AddHandler(success, error, null, medium);
+            AddHandler(success, error, null, medium, true);
 
             return medium;
         }
@@ -252,7 +253,7 @@
 
             var medium = new Promise<T>(this);
 
-            AddHandler(success, null, null, medium);
+            AddHandler(success, null, null, medium, true);
 
             return medium;
         }
@@ -274,7 +275,7 @@
         /// всей цепи обещаний снизу (с самого последнего обещания).
         /// </para>
         /// </remarks>
-        public void Last(Action<T> success, Action<Exception> error, Action cancel) {
+        public void On(Action<T> success, Action<Exception> error, Action cancel) {
             if (success == null && error == null && cancel == null)
                 return;
 
@@ -284,15 +285,28 @@
                     error(err);
                     return default(T);
                 };
-            AddHandler(success, errorHandler, cancel, null);
+            AddHandler(success, errorHandler, cancel, null, false);
+        }
+
+        public void On(Action<T> success, Action<Exception> error) {
+            On(success, error, null);
+        }
+
+        public void On(Action<T> success) {
+            On(success, null, null);
         }
 
-        public void Last(Action<T> success, Action<Exception> error) {
-            Last(success, error, null);
-        }
+        public void On(Action handler, PromiseEventType events) {
+            Safe.ArgumentNotNull(handler, "handler");
 
-        public void Last(Action<T> success) {
-            Last(success, null, null);
+            Action<T> success = events.HasFlag(PromiseEventType.Success) ? new Action<T>(x => handler()) : null;
+            Func<Exception,T> error = events.HasFlag(PromiseEventType.Error) ? new Func<Exception,T>(e => {
+                handler();
+                return default(T);
+            }) : null;
+            Action cancel = events.HasFlag(PromiseEventType.Cancelled) ? handler : null;
+
+            AddHandler(success, error, cancel, null, false);
         }
 
         public IPromise Error(Action<Exception> error) {
@@ -308,7 +322,8 @@
                     return default(T);
                 },
                 null,
-                medium
+                medium,
+                true
             );
 
             return medium;
@@ -328,7 +343,7 @@
 
             var medium = new Promise<T>(this);
 
-            AddHandler(null, handler, null, medium);
+            AddHandler(null, handler, null, medium, true);
 
             return medium;
         }
@@ -380,7 +395,8 @@
                 resultHandler,
                 errorHandler,
                 cancelHandler,
-                null
+                null,
+                true
             );
 
             return medium;
@@ -421,7 +437,7 @@
 
                 var promise = chained(result);
 
-                promise.Last(
+                promise.On(
                     medium.Resolve,
                     medium.Reject,
                     () => medium.Reject(new OperationCanceledException()) // внешняя отмена связанной операции рассматривается как ошибка
@@ -430,7 +446,7 @@
                 // notify chained operation that it's not needed anymore
                 // порядок вызова Then, Cancelled важен, поскольку от этого
                 // зависит IsExclusive
-                medium.Last(
+                medium.On(
                     null,
                     null,
                     () => {
@@ -447,7 +463,7 @@
                     try {
                         var promise = error(e);
 
-                        promise.Last(
+                        promise.On(
                             medium.Resolve,
                             medium.Reject,
                             () => medium.Reject(new OperationCanceledException()) // внешняя отмена связанной операции рассматривается как ошибка
@@ -486,7 +502,8 @@
                 resultHandler,
                 errorHandler,
                 cancelHandler,
-                null
+                null,
+                true
             );
 
             return medium;
@@ -502,7 +519,7 @@
 
         public IPromise<T> Cancelled(Action handler) {
             var medium = new Promise<T>(this);
-            AddHandler(null, null, handler, medium);
+            AddHandler(null, null, handler, medium, false);
             return medium;
         }
 
@@ -513,7 +530,9 @@
         /// <returns>self</returns>
         public IPromise<T> Anyway(Action handler) {
             Safe.ArgumentNotNull(handler, "handler");
-            
+
+            var medium = new Promise<T>(this);
+
             AddHandler(
                 x => handler(),
                 e => {
@@ -521,9 +540,11 @@
                     throw new TransientPromiseException(e);
                 },
                 handler,
-                null
+                medium,
+                true
             );
-            return this;
+
+            return medium;
         }
 
         /// <summary>
@@ -579,8 +600,8 @@
             return Join(Timeout.Infinite);
         }
 
-        void AddHandler(Action<T> success, Func<Exception,T> error, Action cancel, Promise<T> medium) {
-            if (success != null || error != null)
+        void AddHandler(Action<T> success, Func<Exception,T> error, Action cancel, Promise<T> medium, bool inc) {
+            if (inc)
                 Interlocked.Increment(ref m_childrenCount);
 
             var handler = new HandlerDescriptor {
@@ -784,39 +805,39 @@
         }
 
         IPromise ChainNoResult(Func<IPromise> chained, Func<Exception,IPromise> error, Action cancel) {
-                Safe.ArgumentNotNull(chained, "chained");
+            Safe.ArgumentNotNull(chained, "chained");
 
             var medium = new Promise<object>(this);
 
             Action<T> resultHandler = delegate {
-                    if (medium.IsCancelled)
-                        return;
+                if (medium.IsCancelled)
+                    return;
 
-                    var promise = chained();
+                var promise = chained();
 
-                    promise.Last(
-                        medium.Resolve,
-                        medium.Reject,
-                        () => medium.Reject(new OperationCanceledException()) // внешняя отмена связанной операции рассматривается как ошибка
-                    );
+                promise.On(
+                    medium.Resolve,
+                    medium.Reject,
+                    () => medium.Reject(new OperationCanceledException()) // внешняя отмена связанной операции рассматривается как ошибка
+                );
 
-                    // notify chained operation that it's not needed anymore
-                    // порядок вызова Then, Cancelled важен, поскольку от этого
-                    // зависит IsExclusive
-                    medium.Cancelled(() => {
-                        if (promise.IsExclusive)
-                            promise.Cancel();
-                    });
-                };
+                // notify chained operation that it's not needed anymore
+                // порядок вызова Then, Cancelled важен, поскольку от этого
+                // зависит IsExclusive
+                medium.Cancelled(() => {
+                    if (promise.IsExclusive)
+                        promise.Cancel();
+                });
+            };
 
             Func<Exception,T> errorHandler;
 
-                if (error != null)
-                    errorHandler = delegate(Exception e) {
+            if (error != null)
+                errorHandler = delegate(Exception e) {
                     try {
                         var promise = error(e);
 
-                        promise.Last(
+                        promise.On(
                             medium.Resolve,
                             medium.Reject,
                             () => medium.Reject(new OperationCanceledException()) // внешняя отмена связанной операции рассматривается как ошибка
@@ -834,50 +855,53 @@
                     }
                     return default(T);
                 };
-                else
-                    errorHandler = err => {
+            else
+                errorHandler = err => {
                     medium.Reject(err);
                     return default(T);
                 };
 
 
-                Action cancelHandler;
-                if (cancel != null)
-                    cancelHandler = () => {
+            Action cancelHandler;
+            if (cancel != null)
+                cancelHandler = () => {
                     if (cancel != null)
                         cancel();
                     medium.Cancel();
                 };
-                else
-                    cancelHandler = medium.Cancel;
+            else
+                cancelHandler = medium.Cancel;
 
-                AddHandler(
-                    resultHandler,
-                    errorHandler,
-                    cancelHandler,
-                    null
-                );
+            AddHandler(
+                resultHandler,
+                errorHandler,
+                cancelHandler,
+                null,
+                true
+            );
 
-                return medium;
+            return medium;
         }
+
         IPromise IPromise.Chain(Func<IPromise> chained, Func<Exception,IPromise> error) {
             return ChainNoResult(chained, error, null);
         }
+
         IPromise IPromise.Chain(Func<IPromise> chained) {
             return ChainNoResult(chained, null, null);
-        }            
+        }
 
 
-        void IPromise.Last(Action success, Action<Exception> error, Action cancel) {
-            Last(x => success(), error, cancel);
+        void IPromise.On(Action success, Action<Exception> error, Action cancel) {
+            On(x => success(), error, cancel);
         }
 
-        void IPromise.Last(Action success, Action<Exception> error) {
-            Last(x => success(), error, null);
+        void IPromise.On(Action success, Action<Exception> error) {
+            On(x => success(), error, null);
         }
 
-        void IPromise.Last(Action success) {
-            Last(x => success(), null, null);
+        void IPromise.On(Action success) {
+            On(x => success(), null, null);
         }
 
         IPromise IPromise.Error(Action<Exception> error) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Implab/PromiseEventType.cs	Sun Nov 09 23:03:45 2014 +0300
@@ -0,0 +1,19 @@
+using System;
+
+namespace Implab {
+    [Flags]
+    public enum PromiseEventType {
+        Success = 1,
+        Error = 2,
+        Cancelled = 4,
+        /// <summary>
+        /// Завершено успешно, либо возникла ошибка, 
+        /// </summary>
+        All = 7,
+        /// <summary>
+        /// Заврешено успешно, либо возникла ошибка.
+        /// </summary>
+        Complete = 3
+    }
+}
+
--- a/Implab/PromiseExtensions.cs	Sat Nov 08 10:02:47 2014 +0300
+++ b/Implab/PromiseExtensions.cs	Sun Nov 09 23:03:45 2014 +0300
@@ -14,7 +14,7 @@
 
             var p = new SyncContextPromise<T>(context, that);
 
-            that.Last(
+            that.On(
                 p.Resolve,
                 p.Reject,
                 p.Cancel
@@ -28,7 +28,7 @@
 
             var p = new SyncContextPromise<T>(context, that);
 
-            that.Last(
+            that.On(
                 p.Resolve,
                 p.Reject,
                 p.Cancel
@@ -49,7 +49,7 @@
             Safe.ArgumentNotNull(that, "that");
             Safe.ArgumentNotNull(head, "head");
 
-            that.Last(null,null,() => head.Last(cleanup));
+            that.On(null,null,() => head.On(cleanup));
 
             return that;
         }
@@ -72,7 +72,7 @@
             Safe.ArgumentNotNull(that, "that");
             var tcs = new TaskCompletionSource<T>();
 
-            that.Last(tcs.SetResult, tcs.SetException, tcs.SetCanceled);
+            that.On(tcs.SetResult, tcs.SetException, tcs.SetCanceled);
 
             return tcs.Task;
         }
--- a/MonoPlay/Program.cs	Sat Nov 08 10:02:47 2014 +0300
+++ b/MonoPlay/Program.cs	Sun Nov 09 23:03:45 2014 +0300
@@ -12,7 +12,7 @@
                 throw new ArgumentNullException("args");
 
             var q1 = new MTQueue<int>();
-            var q2 = new ConcurrentQueue<int>();
+            var q2 = new Queue<int>();
 
             const int count = 10000000;
 
@@ -32,7 +32,7 @@
             t2 = Environment.TickCount;
             Console.WriteLine("LinkedList: {0} ms", t2 - t1);
 
-            q2 = new ConcurrentQueue<int>();
+            q2 = new Queue<int>();
 
             t1 = Environment.TickCount;