diff Implab/PromiseFuncReaction`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/PromiseFuncReaction`1.cs	Fri Jan 26 18:46:27 2018 +0300
+++ b/Implab/PromiseFuncReaction`1.cs	Tue Jan 30 01:37:17 2018 +0300
@@ -3,51 +3,47 @@
 
 namespace Implab {
     class PromiseFuncReaction<TRet> : PromiseReaction {
-        readonly Action m_fulfilled;
-
-        readonly Action<Exception> m_rejected;
-
         readonly Deferred<TRet> m_next;
 
-        public PromiseFuncReaction(Func<TRet> fulfilled, Func<Exception, TRet> rejected, Deferred<TRet> next, IDispatcher dispatcher) : base(dispatcher) {
+        public IPromise<TRet> Promise {
+            get { return m_next.Promise; }
+        }
+
+        public PromiseFuncReaction(Func<TRet> fulfilled, Func<Exception, TRet> rejected, IDispatcher dispatcher) : base(dispatcher) {
+            m_next = new Deferred<TRet>(dispatcher);
             if (fulfilled != null)
-                m_fulfilled = () => { next.Resolve(fulfilled()); };
+                FulfilHandler = PromiseHandler.Create(fulfilled, m_next);
 
             if (rejected != null)
-                m_rejected = (e) => { next.Resolve(rejected(e)); };
-            m_next = next;
-        }
-
-        public PromiseFuncReaction(Func<IPromise<TRet>> fulfilled, Func<Exception, IPromise<TRet>> rejected, Deferred<TRet> 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 PromiseFuncReaction(Func<TRet> fulfilled, Func<Exception, IPromise<TRet>> rejected, Deferred<TRet> next, IDispatcher dispatcher) : base(dispatcher) {
+        public PromiseFuncReaction(Func<IPromise<TRet>> fulfilled, Func<Exception, IPromise<TRet>> rejected, IDispatcher dispatcher) : base(dispatcher) {
+            m_next = new Deferred<TRet>(dispatcher);
             if (fulfilled != null)
-                m_fulfilled = () => { next.Resolve(fulfilled()); };
+                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 PromiseFuncReaction(Func<IPromise<TRet>> fulfilled, Func<Exception, TRet> rejected, Deferred<TRet> next, IDispatcher dispatcher) : base(dispatcher) {
+        public PromiseFuncReaction(Func<TRet> fulfilled, Func<Exception, IPromise<TRet>> rejected, IDispatcher dispatcher) : base(dispatcher) {
+            m_next = new Deferred<TRet>(dispatcher);
             if (fulfilled != null)
-                m_fulfilled = () => { next.Resolve(fulfilled()); };
+                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 PromiseFuncReaction(Func<IPromise<TRet>> fulfilled, Func<Exception, TRet> rejected, IDispatcher dispatcher) : base(dispatcher) {
+            m_next = new Deferred<TRet>(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);
@@ -56,21 +52,5 @@
         protected override void DefaultResolve() {
             throw new NotImplementedException();
         }
-
-        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