diff Implab/PromiseExtensions.cs @ 72:d67b95eddaf4 v2

promises refactoring
author cin
date Thu, 04 Sep 2014 18:47:12 +0400
parents
children 4439140706d0
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Implab/PromiseExtensions.cs	Thu Sep 04 18:47:12 2014 +0400
@@ -0,0 +1,38 @@
+using System.Threading;
+
+namespace Implab {
+    public static class PromiseExtensions {
+        public static IPromise<T> DispatchToCurrentContext<T>(this IPromise<T> that) {
+            var context = SynchronizationContext.Current;
+            if (context == null)
+                return that;
+
+            var p = new SyncContextPromise<T>(context, that, true);
+
+            that.Then(
+                x => p.Resolve(x),
+                e => {
+                    p.Reject(e);
+                    return default(T);
+                }
+            );
+            return p;
+        }
+
+        public static IPromise<T> DispatchToContext<T>(this IPromise<T> that, SynchronizationContext context) {
+            Safe.ArgumentNotNull(context, "context");
+
+            var p = new SyncContextPromise<T>(context, that, true);
+
+            that.Then(
+                x => p.Resolve(x),
+                e => {
+                    p.Reject(e);
+                    return default(T);
+                }
+            );
+            return p;
+        }
+    }
+}
+