Пример #1
0
        /// <summary>
        /// Creates a promise that resolves when all given promises resolve or rejects as soon
        /// as any given promise rejects
        /// </summary>
        /// <param name="others">the inner promises</param>
        /// <returns>a promise that resolves when all given promises resolve or rejects as soon
        /// as any given promise rejects</returns>
        public static Promise WhenAnyFail(List <Promise> others)
        {
            Deferred outerDeferred = Deferred.Create();

            int waitCount = others.Count;

            foreach (var promise in others)
            {
                promise.Finally((p) =>
                {
                    lock (outerDeferred)
                    {
                        if (p.Exception != null && outerDeferred.IsFulfilled == false)
                        {
                            outerDeferred.Resolve();
                        }


                        var decrementResult = Interlocked.Decrement(ref waitCount);
                        if (decrementResult == 0 && outerDeferred.IsFulfilled == false)
                        {
                            outerDeferred.Reject(new Exception("None of the promises failed"));
                        }
                    }
                });
            }

            return(outerDeferred.Promise);
        }
Пример #2
0
        /// <summary>
        /// Creates an aggregate promise that completes after all the given promises complete.
        /// The aggregate promise resolves only if all inner promises resolves. If any inner promise
        /// fails then the aggregate promise will fail.
        /// </summary>
        /// <param name="promises">the promises to aggregate</param>
        /// <returns>the aggregate promise</returns>
        public static Promise WhenAll(List <Promise> promises)
        {
            List <Exception> aggregateExceptions = new List <Exception>();
            Deferred         outerDeferred       = Deferred.Create();
            int waitCount = promises.Count;

            foreach (var promise in promises)
            {
                promise.Finally((p) =>
                {
                    if (p.Exception != null)
                    {
                        lock (aggregateExceptions)
                        {
                            aggregateExceptions.Add(p.Exception);
                        }
                    }

                    var decrementResult = Interlocked.Decrement(ref waitCount);
                    if (decrementResult == 0)
                    {
                        if (aggregateExceptions.Count == 0)
                        {
                            outerDeferred.Resolve();
                        }
                        else
                        {
                            outerDeferred.Reject(new AggregateException(aggregateExceptions.ToArray()));
                        }
                    }
                });
            }

            return(outerDeferred.Promise);
        }
Пример #3
0
        /// <summary>
        /// Gets a promise that's already failed
        /// </summary>
        public static Promise Failed(Exception ex)
        {
            var d = Deferred.Create();

            d.Reject(ex);
            return(d.Promise);
        }
Пример #4
0
        /// <summary>
        /// Registers the given cleanup code to run when the lifetime being
        /// managed by this manager ends
        /// </summary>
        /// <param name="cleanupCode">the code to run</param>
        /// <returns>a promise that resolves after the cleanup code runs</returns>
        public Promise OnDisposed(Action cleanupCode)
        {
            var d = Deferred.Create();

            _managedItems.Add(new Subscription(() =>
            {
                cleanupCode();
                d.Resolve();
            }));
            return(d.Promise);
        }
Пример #5
0
 /// <summary>
 /// Registers a disposable to be disposed when this lifetime ends
 /// </summary>
 /// <param name="cleanupCode">an object to dispose when this lifetime ends</param>
 /// <returns>a promise that will resolve when the given object is disposed</returns>
 public Promise OnDisposed(IDisposable cleanupCode)
 {
     if (IsExpired == false)
     {
         return(_manager.OnDisposed(cleanupCode));
     }
     else
     {
         cleanupCode.Dispose();
         var d = Deferred.Create();
         d.Resolve();
         return(d.Promise);
     }
 }
Пример #6
0
        public Promise Start()
        {
            var d = Deferred.Create();
            var t = new Thread(() =>
            {
                try
                {
                    backgroundImpl();
                    d.Resolve();
                }
                catch (Exception ex)
                {
                    d.Reject(ex);
                }
                finally
                {
                    this.Dispose();
                }
            });

            t.IsBackground = true;
            t.Start();
            return(d.Promise);
        }