diff Implab/Safe.cs @ 203:4d9830a9bbb8 v2

Added 'Fail' method to RunnableComponent which allows component to move from Running to Failed state. Added PollingComponent a timer based runnable component More tests Added FailPromise a thin class to wrap exceptions Fixed error handling in SuccessPromise classes.
author cin
date Tue, 18 Oct 2016 17:49:54 +0300
parents a0ff6a0e9c44
children 8200ab154c8a
line wrap: on
line diff
--- a/Implab/Safe.cs	Tue Oct 18 01:03:49 2016 +0300
+++ b/Implab/Safe.cs	Tue Oct 18 17:49:54 2016 +0300
@@ -67,55 +67,41 @@
         }
 
         [DebuggerStepThrough]
-        public static IPromise<T> WrapPromise<T>(Func<T> action) {
-            ArgumentNotNull(action, "action");
-
-            var p = new Promise<T>();
-            try {
-                p.Resolve(action());
-            } catch (Exception err) {
-                p.Reject(err);
-            }
-
-            return p;
-        }
-
-        [DebuggerStepThrough]
-        public static IPromise WrapPromise(Action action) {
-            ArgumentNotNull(action, "action");
-
-            var p = new Promise();
-            try {
-                action();
-                p.Resolve();
-            } catch (Exception err) {
-                p.Reject(err);
-            }
-
-            return p;
-        }
-
-        [DebuggerStepThrough]
-        public static IPromise InvokePromise(Func<IPromise> action) {
+        public static IPromise<T> Run<T>(Func<T> action) {
             ArgumentNotNull(action, "action");
 
             try {
-                var p = action();
-                if (p == null) {
-                    var d = new Promise();
-                    d.Reject(new Exception("The action returned null"));
-                    p = d;
-                }
-                return p;
+                return Promise<T>.FromResult(action());
             } catch (Exception err) {
-                var p = new Promise();
-                p.Reject(err);
-                return p;
+                return Promise<T>.FromException(err);
             }
         }
 
         [DebuggerStepThrough]
-        public static IPromise<T> InvokePromise<T>(Func<IPromise<T>> action) {
+        public static IPromise Run(Action action) {
+            ArgumentNotNull(action, "action");
+
+            try {
+                action();
+                return Promise.SUCCESS;
+            } catch (Exception err) {
+                return new FailedPromise(err);
+            }
+        }
+
+        [DebuggerStepThrough]
+        public static IPromise Run(Func<IPromise> action) {
+            ArgumentNotNull(action, "action");
+
+            try {
+                return action() ?? new FailedPromise(new Exception("The action returned null"));
+            } catch (Exception err) {
+                return new FailedPromise(err);
+            }
+        }
+
+        [DebuggerStepThrough]
+        public static IPromise<T> Run<T>(Func<IPromise<T>> action) {
             ArgumentNotNull(action, "action");
 
             try {