diff Implab/PromiseActionReaction`1.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`1.cs	Fri Jan 26 18:46:27 2018 +0300
+++ b/Implab/PromiseActionReaction`1.cs	Tue Jan 30 01:37:17 2018 +0300
@@ -3,62 +3,47 @@
 
 namespace Implab {
     class PromiseActionReaction<T> : PromiseReaction<T> {
-        readonly Action<T> m_fulfilled;
-
-        readonly Action<Exception> m_rejected;
-
         readonly Deferred m_next;
 
-        public PromiseActionReaction(Action<T> fulfilled, Action<Exception> rejected, Deferred next, IDispatcher dispatcher) : base(dispatcher) {
+        public IPromise Promise {
+            get { return m_next.Promise; }
+        }
+
+        public PromiseActionReaction(Action<T> fulfilled, Action<Exception> rejected, IDispatcher dispatcher) : base(dispatcher) {
+            m_next = new Deferred(dispatcher);
             if (fulfilled != null)
-                m_fulfilled = (x) => {
-                    fulfilled(x);
-                    next.Resolve();
-                };
+                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<T, IPromise> fulfilled, Func<Exception, IPromise> rejected, Deferred next, IDispatcher dispatcher) : base(dispatcher) {
+        public PromiseActionReaction(Func<T, IPromise> fulfilled, Func<Exception, IPromise> rejected, IDispatcher dispatcher) : base(dispatcher) {
+            m_next = new Deferred(dispatcher);
             if (fulfilled != null)
-                m_fulfilled = (x) => { next.Resolve(fulfilled(x)); };
-            if (rejected != null)
-                m_rejected = (e) => { next.Resolve(rejected(e)); };
-            m_next = next;
-        }
-
-        public PromiseActionReaction(Action<T> fulfilled, Func<Exception, IPromise> rejected, Deferred next, IDispatcher dispatcher) : base(dispatcher) {
-            if (fulfilled != null)
-                m_fulfilled = (x) => {
-                    fulfilled(x);
-                    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<T, IPromise> fulfilled, Action<Exception> rejected, Deferred next, IDispatcher dispatcher) : base(dispatcher) {
+        public PromiseActionReaction(Action<T> fulfilled, Func<Exception, IPromise> rejected, IDispatcher dispatcher) : base(dispatcher) {
+            m_next = new Deferred(dispatcher);
             if (fulfilled != null)
-                m_fulfilled = (x) => { next.Resolve(fulfilled(x)); };
+                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);
         }
 
-        protected override bool HasFulfilHandler => m_fulfilled != null;
+        public PromiseActionReaction(Func<T, 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 HasRejectHandler => m_rejected != null;
+            if (rejected != null)
+                RejectHandler = PromiseHandler.Create(rejected, m_next);
+        }
 
         protected override void DefaultReject(Exception reason) {
             m_next.Reject(reason);
@@ -67,21 +52,5 @@
         protected override void DefaultResolve(T result) {
             m_next.Resolve();
         }
-
-        protected override void RejectImpl(Exception reason) {
-            try {
-                m_rejected(reason);
-            } catch (Exception e) {
-                m_next.Reject(e);
-            }
-        }
-
-        protected override void ResolveImpl(T result) {
-            try {
-                m_fulfilled(result);
-            } catch (Exception e) {
-                m_next.Reject(e);
-            }
-        }
     }
 }
\ No newline at end of file