internal void RegisterPromise(BasePromiseInfo promise, int insertionIndex = -1)
        {
            //If we are currently in a execution, all the promises need to be on the top
            if (InPromiseExecution)
            {
                Promises.Insert(PromisesInsertionIndex++, promise);
            }
            else if (insertionIndex != -1)
            {
                Promises.Insert(insertionIndex, promise);
            }
            else
            {
                var index         = -1;
                var lastDelimiter = Promises.Where((p) => p is IDelimiterPromise).FirstOrDefault();
                if (lastDelimiter != null)
                {
                    index = Promises.IndexOf(lastDelimiter);
                }
                if (index != -1)
                {
                    Promises.Insert(index, promise);
                }
                else
                {
                    Promises.Add(promise);
                }
            }

            // Promise must be stored in the cache to create a valid reference for its surrogate
            StateManager.Current.AddNewObject(promise);

            LastPromise = promise;
            StateManager.Current.Tracker.MarkAsModified(this, "Promises");
        }
 internal void BeginPromiseExecution(BasePromiseInfo promise)
 {
     LastPromise            = null;
     ExecutingPromiseInfo   = promise;
     InPromiseExecution     = true;
     PromisesInsertionIndex = 0;
 }
        /// <summary>
        /// Verifies if the promises where created in the same scope (same AsyncBuilder).
        /// If any of them has not been created with an AsyncBuilder returns false.
        /// </summary>
        /// <param name="promise">The instance of the current promise</param>
        /// <param name="familyId">The Id of the possible sibling promise</param>
        /// <returns>Boolean value indicating if the promises are in the same scope</returns>
        internal static bool IsFamilyBySubFamily(this BasePromiseInfo promise, string familyId)
        {
            bool result = false;

            if (!String.IsNullOrEmpty(promise.FamilyId))
            {
                result = promise.FamilyId == familyId;
            }
            return(result);
        }
        internal void RemoveContinuation(BasePromiseInfo oldPromise)
        {
            var index = Promises.FindIndex(x => x.UniqueID.Equals(oldPromise.UniqueID, StringComparison.Ordinal));

            if (index != -1)
            {
                Promises.RemoveAt(index);
            }
            StateManager.Current.Tracker.MarkAsModified(this, "Promises");
        }
        /// <summary>
        /// Verifies if the promises where created in the same context (same method).
        /// If any of them has not been created with an AsyncBuilder returns false.
        /// </summary>
        /// <param name="promise">The instance of the current promise</param>
        /// <param name="familyId">The Id of the possible sibling promise</param>
        /// <returns>Boolean value indicating if the promises are related</returns>
        internal static bool IsFamily(this BasePromiseInfo promise, string familyId)
        {
            bool result = false;

            if (!String.IsNullOrEmpty(promise.FamilyId) && !String.IsNullOrEmpty(familyId))
            {
                result = string.Equals(GetFirstFragmentOfFamily(promise.FamilyId),
                                       GetFirstFragmentOfFamily(familyId));
            }
            return(result);
        }
        internal static bool IsApplicableException(this BasePromiseInfo promise, Exception exc)
        {
            bool result = false;
            var  type   = promise.GetArgumentType();

            if (type != null)
            {
                result = type.Equals(exc.GetType());
            }

            return(result);
        }