diff Implab/PromiseHandler.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
children d82909310094
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Implab/PromiseHandler.cs	Tue Jan 30 01:37:17 2018 +0300
@@ -0,0 +1,102 @@
+using System;
+using System.Diagnostics;
+
+namespace Implab {
+    class PromiseHandler {
+        public static Action<T> Create<T>(Action<T> handler, Deferred next) {
+            Debug.Assert(handler != null);
+
+            return (v) => {
+                try {
+                    handler(v);
+                    next.Resolve();
+                } catch (Exception err) {
+                    next.Reject(err);
+                }
+            };
+        }
+
+        public static Action<T> Create<T>(Func<T, IPromise> handler, Deferred next) {
+            Debug.Assert(handler != null);
+
+            return (v) => {
+                try {
+                    next.Resolve(handler(v));
+                } catch (Exception err) {
+                    next.Reject(err);
+                }
+            };
+        }
+
+        public static Action<T> Create<T, T2>(Func<T, T2> handler, Deferred<T2> next) {
+            Debug.Assert(handler != null);
+
+            return (v) => {
+                try {
+                    next.Resolve(handler(v));
+                } catch (Exception err) {
+                    next.Reject(err);
+                }
+            };
+        }
+
+        public static Action<T> Create<T, T2>(Func<T, IPromise<T2>> handler, Deferred<T2> next) {
+            Debug.Assert(handler != null);
+            return (v) => {
+                try {
+                    next.Resolve(handler(v));
+                } catch (Exception err) {
+                    next.Reject(err);
+                }
+            };
+        }
+
+        public static Action Create(Action handler, Deferred next) {
+            Debug.Assert(handler != null);
+
+            return () => {
+                try {
+                    handler();
+                    next.Resolve();
+                } catch (Exception err) {
+                    next.Reject(err);
+                }
+            };
+        }
+
+        public static Action Create(Func<IPromise> handler, Deferred next) {
+            Debug.Assert(handler != null);
+
+            return () => {
+                try {
+                    next.Resolve(handler());
+                } catch (Exception err) {
+                    next.Reject(err);
+                }
+            };
+        }
+
+        public static Action Create<T2>(Func<T2> handler, Deferred<T2> next) {
+            Debug.Assert(handler != null);
+
+            return () => {
+                try {
+                    next.Resolve(handler());
+                } catch (Exception err) {
+                    next.Reject(err);
+                }
+            };
+        }
+
+        public static Action Create<T2>(Func<IPromise<T2>> handler, Deferred<T2> next) {
+            Debug.Assert(handler != null);
+            return () => {
+                try {
+                    next.Resolve(handler());
+                } catch (Exception err) {
+                    next.Reject(err);
+                }
+            };
+        }
+    }
+}
\ No newline at end of file