public static void DisposeExceptionManager(this IAsyncBuilder builder) { if (builder.HasExceptionManager()) { builder.ExceptionManager.Dispose(); } }
public static void InsertPromisesToParent(this IAsyncBuilder builder) { if (builder.Parent != null) { builder.Parent.Delegates.AddRange(builder.Delegates); } }
/// <summary> /// Mark the promises as initial state. The promises are always going to be executed unless a there is an explicit return. /// </summary> /// <param name="builder">The instance of the current AsyncBuilder</param> public static void SetPromisesState(this IAsyncBuilder builder) { //The appended promises are marked as pending foreach (var delegateinfo in builder.Delegates) { var promise = delegateinfo.CreatedPromise as PromiseInfo; if (promise != null && promise.State != PromiseState.Skipped) { promise.State = PromiseState.Default; } } }
/// <summary> /// Adds a delegate and its respective promise to the list of promises to be validated. /// </summary> /// <param name="builder">The instance of the current AsyncBuilder</param> /// <param name="del">The original Delgate</param> /// <param name="promise">The promise associeted to the Delegate</param> /// <param name="promiseType">Indicates the type of promise added. By default is CodeExecution</param> public static void AddPromise(this IAsyncBuilder builder, Delegate del, IPromise promise, PromiseType promiseType = PromiseType.CodeExecution) { var delPromise = new DelegateOrPromise() { Delegate = del }; delPromise.CreatedPromise = promise; builder.Delegates.Add(delPromise); builder.SetPromiseInfo(delPromise.CreatedPromise, builder.FamilyId, promiseType); builder.HasAppendedPromises = true; }
/// <summary> /// Sets the FamilyId from the current builder to the promise. /// All of the promises within a method belong to the same family. /// </summary> /// <param name="builder">The instance of the current AsyncBuilder</param> /// <param name="promise">The promise to add to the family</param> public static void SetPromiseInfo(this IAsyncBuilder builder, IPromise promise, string familyId, PromiseType promiseType) { var basePomise = promise as BasePromiseInfo; if (basePomise != null) { basePomise.FamilyId = familyId + UniqueIDGenerator.UniqueIdSeparator + builder.UniqueId; basePomise.PromiseType = promiseType; if (promiseType == PromiseType.Catch) { basePomise.State = PromiseState.Skipped; } if (promiseType == PromiseType.Finally) { basePomise.State = PromiseState.Default; } } }
/// <summary> /// Sets the initial values for an AsyncBuilder /// </summary> /// <param name="topBuilder">Indicates if the builder creates a new context</param> /// <param name="builder">The AsyncBuilder instance</param> private void InitBuilder(bool topBuilder, IAsyncBuilder builder) { if (_currentBuilders == null) { _currentBuilders = new Stack <IAsyncBuilder>(); } if (_currentBuilders.Count > 0) { builder.Parent = _currentBuilders.Peek(); } builder.UniqueId = _stateManager.UniqueIDGenerator.GetBuilderUniqueID(); builder.DisposeBuilder += RemoveFromStack; if (topBuilder) { #if DEBUG if (_currentBuilders.Count > 0) { UpgradeHelpers.WebMap.Server.Common.TraceUtil.TraceInformation("A new Top Async Builder has been open within another."); } #endif //A new scope has been open, all of the following builder are grouped in the current family. builder.FamilyId = _stateManager.UniqueIDGenerator.GetFamilyUniqueID(); } else if (builder.Parent != null) { //Inherits the FamilyId from the last builder builder.FamilyId = builder.Parent.FamilyId; } else { //The FamilyId is calculated based on the current executing promise if (_viewManager.State.ExecutingPromiseInfo != null) { builder.FamilyId = _viewManager.State.ExecutingPromiseInfo.FamilyId; } } }
/// <summary> /// Triggers the logic associated to a catch block on synchronic code. Looks for a suitable excpetion through the same family /// </summary> /// <typeparam name="T">The type of the exception</typeparam> /// <param name="ex">The instance of the exception</param> /// <param name="builder">The instance of the parent builder</param> private void HandleException <T>(T ex, IAsyncBuilder builder) where T : Exception { if (builder != null && builder.HasExceptionManager()) { var exceptionManager = builder.ExceptionManager as ExceptionManager; Delegate genericException = null; Delegate specificException = null; exceptionManager.Exceptions.TryGetValue(TYPE_OF_EXCEPTION, out genericException); //If System.Exception no need to look for a more specific exception. if (typeof(T) != TYPE_OF_EXCEPTION) { exceptionManager.Exceptions.TryGetValue(typeof(T), out specificException); if (specificException != null) { //The type of the thrown exception is different than System.Exception and has catch that handles it specifically. (specificException as Action <T>)(ex); return; } } if (genericException != null) { //If no specific excption is declared then the generic catch handles it. (genericException as Action <T>)(ex); return; } else if (builder == null) { //If no more parents the exception must be handled asynchronically. return; } else { //Look for another exception in the chain HandleException <T>(ex, builder.Parent); } } }