diff Implab/PromiseActionReaction.cs @ 248:5cb4826c2c2a v3

Added awaiters to promises Added static methods to Promise Resolve, Reject, All. Updated promise helpers
author cin
date Tue, 30 Jan 2018 01:37:17 +0300
parents fb70574741a1
children d82909310094
line wrap: on
line diff
--- a/Implab/PromiseActionReaction.cs	Fri Jan 26 18:46:27 2018 +0300
+++ b/Implab/PromiseActionReaction.cs	Tue Jan 30 01:37:17 2018 +0300
@@ -3,63 +3,48 @@
 
 namespace Implab {
     class PromiseActionReaction : PromiseReaction {
-        readonly Action m_fulfilled;
-
-        readonly Action<Exception> m_rejected;
 
         readonly Deferred m_next;
 
-        public PromiseActionReaction(Action fulfilled, Action<Exception> rejected, Deferred next, IDispatcher dispatcher) : base(dispatcher) {
+        public IPromise Promise {
+            get { return m_next.Promise; }
+        }
+
+        public PromiseActionReaction(Action fulfilled, Action<Exception> rejected, IDispatcher dispatcher) : base(dispatcher) {
+            m_next = new Deferred(dispatcher);
             if (fulfilled != null)
-                m_fulfilled = () => {
-                    fulfilled();
-                    next.Resolve();
-                };
+                FulfilHandler = PromiseHandler.Create(fulfilled, m_next);
 
             if (rejected != null)
-                m_rejected = (x) => {
-                    rejected(x);
-                    next.Resolve();
-                };
-            m_next = next;
-        }
-
-        public PromiseActionReaction(Func<IPromise> fulfilled, Func<Exception, IPromise> rejected, Deferred next, IDispatcher dispatcher) : base(dispatcher) {
-            if (fulfilled != null)
-                m_fulfilled = () => { next.Resolve(fulfilled()); };
-            if (rejected != null)
-                m_rejected = (e) => { next.Resolve(rejected(e)); };
-            m_next = next;
+                RejectHandler = PromiseHandler.Create(rejected, m_next);
         }
 
-        public PromiseActionReaction(Action fulfilled, Func<Exception, IPromise> rejected, Deferred next, IDispatcher dispatcher) : base(dispatcher) {
+        public PromiseActionReaction(Func<IPromise> fulfilled, Func<Exception, IPromise> rejected, IDispatcher dispatcher) : base(dispatcher) {
+            m_next = new Deferred(dispatcher);
             if (fulfilled != null)
-                m_fulfilled = () => {
-                    fulfilled();
-                    next.Resolve();
-                };
+                FulfilHandler = PromiseHandler.Create(fulfilled, m_next);
 
             if (rejected != null)
-                m_rejected = (e) => { next.Resolve(rejected(e)); };
-            m_next = next;
+                RejectHandler = PromiseHandler.Create(rejected, m_next);
         }
 
-        public PromiseActionReaction(Func<IPromise> fulfilled, Action<Exception> rejected, Deferred next, IDispatcher dispatcher) : base(dispatcher) {
+        public PromiseActionReaction(Action fulfilled, Func<Exception, IPromise> rejected, IDispatcher dispatcher) : base(dispatcher) {
+            m_next = new Deferred(dispatcher);
             if (fulfilled != null)
-                m_fulfilled = () => { next.Resolve(fulfilled()); };
+                FulfilHandler = PromiseHandler.Create(fulfilled, m_next);
 
             if (rejected != null)
-                m_rejected = (x) => {
-                    rejected(x);
-                    next.Resolve();
-                };
-            m_next = next;
+                RejectHandler = PromiseHandler.Create(rejected, m_next);
         }
 
+        public PromiseActionReaction(Func<IPromise> fulfilled, Action<Exception> rejected, IDispatcher dispatcher) : base(dispatcher) {
+            m_next = new Deferred(dispatcher);
+            if (fulfilled != null)
+                FulfilHandler = PromiseHandler.Create(fulfilled, m_next);
 
-        protected override bool HasFulfilHandler => m_fulfilled != null;
-
-        protected override bool HasRejectHandler => m_rejected != null;
+            if (rejected != null)
+                RejectHandler = PromiseHandler.Create(rejected, m_next);
+        }
 
         protected override void DefaultReject(Exception reason) {
             m_next.Reject(reason);
@@ -68,21 +53,5 @@
         protected override void DefaultResolve() {
             m_next.Resolve();
         }
-
-        protected override void RejectImpl(Exception reason) {
-            try {
-                m_rejected(reason);
-            } catch (Exception e){
-                m_next.Reject(e);
-            }
-        }
-
-        protected override void ResolveImpl() {
-            try {
-                m_fulfilled();
-            } catch (Exception e){
-                m_next.Reject(e);
-            }
-        }
     }
 }
\ No newline at end of file