diff Implab/PromiseFuncReaction`2.cs @ 249:d82909310094 v3

Implab.Test moved to xunit Complete set of PromiseHelpers (Then, Catch, Finally) Removed obsolete types ICancellable, ICancellationToken
author cin
date Wed, 31 Jan 2018 11:28:38 +0300
parents 5cb4826c2c2a
children
line wrap: on
line diff
--- a/Implab/PromiseFuncReaction`2.cs	Tue Jan 30 01:37:17 2018 +0300
+++ b/Implab/PromiseFuncReaction`2.cs	Wed Jan 31 11:28:38 2018 +0300
@@ -2,55 +2,96 @@
 using System.Diagnostics;
 
 namespace Implab {
-    class PromiseFuncReaction<TIn, TRet> : PromiseReaction<TIn> {
+    class PromiseFuncReaction<TIn, TRet> : IResolvable<TIn> {
         readonly Deferred<TRet> m_next;
 
-        public IPromise<TRet> Promise { 
+        readonly IDispatcher m_dispatcher;
+
+        readonly Action<TIn, Deferred<TRet>> m_fulfilled;
+
+        readonly Action<Exception, Deferred<TRet>> m_rejected;
+
+        public IPromise<TRet> Promise {
             get { return m_next.Promise; }
         }
 
-        public PromiseFuncReaction(Func<TIn, 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);
-
-            if (rejected != null)
-                RejectHandler = PromiseHandler.Create(rejected, m_next);
+        public PromiseFuncReaction(Action<TIn, Deferred<TRet>> fulfilled, Action<Exception, Deferred<TRet>> rejected, Deferred<TRet> next, IDispatcher dispatcher) {
+            m_next = next;
+            m_fulfilled = fulfilled;
+            m_rejected = rejected;
+            m_dispatcher = dispatcher;
         }
 
-        public PromiseFuncReaction(Func<TIn, IPromise<TRet>> fulfilled, Func<Exception, IPromise<TRet>> rejected, IDispatcher dispatcher) : base(dispatcher) {
-            m_next = new Deferred<TRet>(dispatcher);
-            if (fulfilled != null)
-                FulfilHandler = PromiseHandler.Create(fulfilled, m_next);
+        public void Resolve(TIn result) {
+            if (m_fulfilled != null) {
+                if (m_dispatcher != null)
+                    m_dispatcher.Enqueue(ResolveImpl, result);
+                else
+                    ResolveImpl(result);
+            } else {
+                try {
+                    m_next.Resolve((TRet)(object)result);
+                } catch(Exception error) {
+                    // handle cast exceptions
+                    m_next.Reject(error);
+                }
+            }
+        }
 
-            if (rejected != null)
-                RejectHandler = PromiseHandler.Create(rejected, m_next);
+        void ResolveImpl (TIn result) {
+            m_fulfilled(result, m_next);
+        }
+
+        public void Reject(Exception error) {
+            if (m_fulfilled != null) {
+                if (m_dispatcher != null)
+                    m_dispatcher.Enqueue(RejectImpl, error);
+                else
+                    RejectImpl(error);
+            } else {
+                m_next.Reject(error);
+            }
         }
 
-        public PromiseFuncReaction(Func<TIn, TRet> fulfilled, Func<Exception, IPromise<TRet>> rejected, IDispatcher dispatcher) : base(dispatcher) {
-            m_next = new Deferred<TRet>(dispatcher);
-            if (fulfilled != null)
-                FulfilHandler = PromiseHandler.Create(fulfilled, m_next);
+        void RejectImpl(Exception error) {
+            m_rejected(error, m_next);
+        }
+
 
-            if (rejected != null)
-                RejectHandler = PromiseHandler.Create(rejected, m_next);
+        public static PromiseFuncReaction<TIn,TRet> Create(Func<TIn,TRet> fulfilled, Func<Exception, TRet> rejected, IDispatcher dispatcher) {
+            return new PromiseFuncReaction<TIn,TRet>(
+                fulfilled != null ? PromiseHandler.Create(fulfilled) : null,
+                rejected != null ? PromiseHandler.Create(rejected) : null,
+                new Deferred<TRet>(),
+                dispatcher
+            );
         }
 
-        public PromiseFuncReaction(Func<TIn, 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);
-
-            if (rejected != null)
-                RejectHandler = PromiseHandler.Create(rejected, m_next);
+        public static PromiseFuncReaction<TIn,TRet> Create(Func<TIn,IPromise<TRet>> fulfilled, Func<Exception, TRet> rejected, IDispatcher dispatcher) {
+            return new PromiseFuncReaction<TIn,TRet>(
+                fulfilled != null ? PromiseHandler.Create(fulfilled) : null,
+                rejected != null ? PromiseHandler.Create(rejected) : null,
+                new Deferred<TRet>(),
+                dispatcher
+            );
         }
 
-        protected override void DefaultReject(Exception reason) {
-            m_next.Reject(reason);
+        public static PromiseFuncReaction<TIn,TRet> Create(Func<TIn,TRet> fulfilled, Func<Exception, IPromise<TRet>> rejected, IDispatcher dispatcher) {
+            return new PromiseFuncReaction<TIn,TRet>(
+                fulfilled != null ? PromiseHandler.Create(fulfilled) : null,
+                rejected != null ? PromiseHandler.Create(rejected) : null,
+                new Deferred<TRet>(),
+                dispatcher
+            );
         }
 
-        protected override void DefaultResolve(TIn result) {
-            m_next.Resolve((TRet)(object)result);
+        public static PromiseFuncReaction<TIn,TRet> Create(Func<TIn,IPromise<TRet>> fulfilled, Func<Exception, IPromise<TRet>> rejected, IDispatcher dispatcher) {
+            return new PromiseFuncReaction<TIn,TRet>(
+                fulfilled != null ? PromiseHandler.Create(fulfilled) : null,
+                rejected != null ? PromiseHandler.Create(rejected) : null,
+                new Deferred<TRet>(),
+                dispatcher
+            );
         }
     }
 }
\ No newline at end of file