changeset 110:1a8426e6e895 v2

added promise timeout helper
author cin
date Sun, 16 Nov 2014 02:49:12 +0300
parents 1b7ebcc52e5a
children 38d6a4db35d7
files Implab/PromiseExtensions.cs
diffstat 1 files changed, 17 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/Implab/PromiseExtensions.cs	Fri Nov 14 14:04:24 2014 +0300
+++ b/Implab/PromiseExtensions.cs	Sun Nov 16 02:49:12 2014 +0300
@@ -72,6 +72,23 @@
                 }
             };
         }
+
+        static void CancelCallback(object cookie) {
+            ((ICancellable)cookie).Cancel();
+        }
+
+        /// <summary>
+        /// Cancells promise after the specified timeout is elapsed.
+        /// </summary>
+        /// <param name="that">The promise to cancel on timeout.</param>
+        /// <param name="milliseconds">The timeout in milliseconds.</param>
+        /// <typeparam name="TPromise">The 1st type parameter.</typeparam>
+        public static TPromise Timeout<TPromise>(this TPromise that, int milliseconds) where TPromise : IPromise {
+            Safe.ArgumentNotNull(that, "that");
+            var timer = new Timer(CancelCallback, that, milliseconds, -1);
+            that.On(timer.Dispose, PromiseEventType.All);
+            return that;
+        }
             
         #if NET_4_5