diff Implab/AbstractPromise.cs @ 242:cbe10ac0731e v3

Working on promises
author cin
date Wed, 24 Jan 2018 03:03:21 +0300
parents 86187b01c4e0
children b1e0ffdf3451
line wrap: on
line diff
--- a/Implab/AbstractPromise.cs	Tue Jan 23 19:39:21 2018 +0300
+++ b/Implab/AbstractPromise.cs	Wed Jan 24 03:03:21 2018 +0300
@@ -3,28 +3,16 @@
 
 namespace Implab {
     public abstract class AbstractPromise : AbstractEvent<AbstractPromise.HandlerDescriptor>, IPromise {
-        public struct HandlerDescriptor {
+        public class HandlerDescriptor {
             readonly Action m_handler;
             readonly Action<Exception> m_error;
-            readonly Action<Exception> m_cancel;
-            readonly PromiseEventType m_mask;
-
-            public HandlerDescriptor(Action success, Action<Exception> error, Action<Exception> cancel) {
+            public HandlerDescriptor(Action success, Action<Exception> error) {
                 m_handler = success;
                 m_error = error;
-                m_cancel = cancel;
-                m_mask = PromiseEventType.Success;
-            }
-
-            public HandlerDescriptor(Action handler, PromiseEventType mask) {
-                m_handler = handler;
-                m_error = null;
-                m_cancel = null;
-                m_mask = mask;
             }
 
             public void SignalSuccess() {
-                if ((m_mask & PromiseEventType.Success) != 0 && m_handler != null) {
+                if (m_handler != null) {
                     try {
                         m_handler();
                         // Analysis disable once EmptyGeneralCatchClause
@@ -40,28 +28,6 @@
                         // Analysis disable once EmptyGeneralCatchClause
                     } catch {
                     }
-                } else if ((m_mask & PromiseEventType.Error ) != 0 && m_handler != null) {
-                    try {
-                        m_handler();
-                        // Analysis disable once EmptyGeneralCatchClause
-                    } catch {
-                    }
-                }
-            }
-
-            public void SignalCancel(Exception reason) {
-                if (m_cancel != null) {
-                    try {
-                        m_cancel(reason);
-                        // Analysis disable once EmptyGeneralCatchClause
-                    } catch {
-                    }
-                } else if ( (m_mask & PromiseEventType.Cancelled) != 0 && m_handler != null) {
-                    try {
-                        m_handler();
-                        // Analysis disable once EmptyGeneralCatchClause
-                    } catch {
-                    }
                 }
             }
         }
@@ -75,48 +41,29 @@
                     handler.SignalSuccess();
                     break;
                 case REJECTED_STATE:
-                    handler.SignalError(Error);
-                    break;
-                case CANCELLED_STATE:
-                    handler.SignalCancel(CancellationReason);
+                    handler.SignalError(RejectReason);
                     break;
                 default:
                     throw new InvalidOperationException(String.Format("Invalid promise signal: {0}", signal));
             }
         }
 
-        protected override Signal GetResolveSignal() {
+        protected override Signal GetFulfillSignal() {
             var signal = new Signal();
-            On(signal.Set, PromiseEventType.All);
+            On(signal.Set, e => signal.Set());
             return signal;
         }
 
         #endregion
 
-        public Type PromiseType {
+        public Type ResultType {
             get {
                 return typeof(void);
             }
         }
 
-        public IPromise On(Action success, Action<Exception> error, Action<Exception> cancel) {
-            AddHandler(new HandlerDescriptor(success, error, cancel));
-            return this;
-        }
-
-        public IPromise On(Action success, Action<Exception> error) {
-            AddHandler(new HandlerDescriptor(success, error, null));
-            return this;
-        }
-
-        public IPromise On(Action success) {
-            AddHandler(new HandlerDescriptor(success, null, null));
-            return this;
-        }
-
-        public IPromise On(Action handler, PromiseEventType events) {
-            AddHandler(new HandlerDescriptor(handler,events));
-            return this;
+        public void On(Action success, Action<Exception> error) {
+            AddHandler(new HandlerDescriptor(success, error));
         }
 
         public IPromise<T> Cast<T>() {