diff Implab/PromiseAll.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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Implab/PromiseAll.cs	Tue Jan 30 01:37:17 2018 +0300
@@ -0,0 +1,40 @@
+using System;
+using System.Threading;
+
+namespace Implab
+{
+    class PromiseAll : IResolvable {
+        int m_count;
+
+        readonly Deferred m_deferred;
+
+        public bool Done {
+            get { return m_deferred.Promise.IsResolved; }
+        }
+
+        public IPromise ResultPromise {
+            get { return m_deferred.Promise; }
+        }
+
+        public void AddPromise(IPromise promise) {
+            Interlocked.Increment(ref m_count);
+        }
+
+        public PromiseAll(Deferred deferred) {
+            m_deferred = deferred;
+        }
+
+        public void Resolve() {
+            if (Interlocked.Decrement(ref m_count) == 0)
+                m_deferred.Resolve();
+        }
+
+        public void Complete() {
+            Resolve();
+        }
+
+        public void Reject(Exception error) {
+            m_deferred.Reject(error);
+        }
+    }
+}
\ No newline at end of file