diff Implab/PromiseReaction`1.cs @ 247:fb70574741a1 v3

working on promises
author cin
date Fri, 26 Jan 2018 18:46:27 +0300
parents 5aa9cfbe56c3
children 5cb4826c2c2a
line wrap: on
line diff
--- a/Implab/PromiseReaction`1.cs	Fri Jan 26 11:19:38 2018 +0300
+++ b/Implab/PromiseReaction`1.cs	Fri Jan 26 18:46:27 2018 +0300
@@ -1,25 +1,45 @@
 using System;
 
 namespace Implab {
-    public class PromiseReaction<T> : IResolvable<T> {
-        IDispatcher m_dispatcher;
+    abstract class PromiseReaction<T> : IResolvable<T> {
+        readonly IDispatcher m_dispatcher;
+
+        protected PromiseReaction(IDispatcher dispatcher) {
+            m_dispatcher = dispatcher;
+        }
 
-        Action<T> m_onFulfilledJob;
+        protected abstract bool HasFulfilHandler {
+            get;
+        }
 
-        Action<Exception> m_onRejectedJob;
+        protected abstract bool HasRejectHandler {
+            get;
+        }
 
         public void Reject(Exception error) {
-            if (m_dispatcher != null)
-                m_dispatcher.Enqueue(() => m_onRejectedJob(error));
+            if (!HasRejectHandler)
+                DefaultReject(error);
+            else if (m_dispatcher != null)
+                m_dispatcher.Enqueue(() => RejectImpl(error));
             else
-                m_onRejectedJob(error);
+                RejectImpl(error);
         }
 
         public void Resolve(T result) {
-            if (m_dispatcher != null)
-                m_dispatcher.Enqueue(() => m_onFulfilledJob(result));
+            if (!HasFulfilHandler)
+                DefaultResolve(result);
+            else if (m_dispatcher != null)
+                m_dispatcher.Enqueue(() => ResolveImpl(result));
             else
-                m_onFulfilledJob(result);
+                ResolveImpl(result);
         }
+
+        protected abstract void ResolveImpl(T result);
+
+        protected abstract void RejectImpl(Exception reason);
+
+        protected abstract void DefaultResolve(T result);
+
+        protected abstract void DefaultReject(Exception reason);
     }
 }
\ No newline at end of file