|
| 1 | +// |
| 2 | +// Copyright (c) Microsoft. All rights reserved. |
| 3 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 4 | +// |
| 5 | + |
| 6 | +using System; |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.Diagnostics; |
| 9 | +using System.Linq; |
| 10 | +using System.Threading.Tasks; |
| 11 | +using Microsoft.PowerShell.EditorServices.Utility; |
| 12 | + |
| 13 | +namespace Microsoft.PowerShell.EditorServices.Components |
| 14 | +{ |
| 15 | + public abstract class FeatureComponentBase<TProvider> |
| 16 | + where TProvider : IFeatureProvider |
| 17 | + { |
| 18 | + /// <summary> |
| 19 | + /// Gets the collection of IFeatureProviders registered with |
| 20 | + /// this feature component. |
| 21 | + /// </summary> |
| 22 | + public IFeatureProviderCollection<TProvider> Providers { get; private set; } |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// Gets the ILogger implementation to use for writing log |
| 26 | + /// messages. |
| 27 | + /// </summary> |
| 28 | + protected ILogger Logger { get; private set; } |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Creates an instance of the FeatureComponentBase class with |
| 32 | + /// the specified ILoggger. |
| 33 | + /// </summary> |
| 34 | + /// <param name="logger">The ILogger to use for this instance.</param> |
| 35 | + public FeatureComponentBase(ILogger logger) |
| 36 | + { |
| 37 | + this.Providers = new FeatureProviderCollection<TProvider>(); |
| 38 | + this.Logger = logger; |
| 39 | + } |
| 40 | + |
| 41 | + protected IEnumerable<TResult> InvokeProviders<TResult>( |
| 42 | + Func<TProvider, TResult> invokeFunc) |
| 43 | + { |
| 44 | + Stopwatch invokeTimer = new Stopwatch(); |
| 45 | + List<TResult> providerResults = new List<TResult>(); |
| 46 | + |
| 47 | + foreach (var provider in this.Providers) |
| 48 | + { |
| 49 | + try |
| 50 | + { |
| 51 | + invokeTimer.Restart(); |
| 52 | + |
| 53 | + providerResults.Add(invokeFunc(provider)); |
| 54 | + |
| 55 | + invokeTimer.Stop(); |
| 56 | + |
| 57 | + this.Logger.Write( |
| 58 | + LogLevel.Verbose, |
| 59 | + $"Invocation of provider '{provider.ProviderId}' completed in {invokeTimer.ElapsedMilliseconds}ms."); |
| 60 | + } |
| 61 | + catch (Exception e) |
| 62 | + { |
| 63 | + this.Logger.WriteException( |
| 64 | + $"Exception caught while invoking provider {provider.ProviderId}:", |
| 65 | + e); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + return providerResults; |
| 70 | + } |
| 71 | + |
| 72 | + protected async Task<IEnumerable<TResult>> InvokeProvidersAsync<TResult>( |
| 73 | + Func<TProvider, Task<TResult>> invokeFunc) |
| 74 | + { |
| 75 | + Stopwatch invokeTimer = new Stopwatch(); |
| 76 | + List<TResult> providerResults = new List<TResult>(); |
| 77 | + |
| 78 | + foreach (var provider in this.Providers) |
| 79 | + { |
| 80 | + try |
| 81 | + { |
| 82 | + invokeTimer.Restart(); |
| 83 | + |
| 84 | + providerResults.Add( |
| 85 | + await invokeFunc(provider)); |
| 86 | + |
| 87 | + invokeTimer.Stop(); |
| 88 | + |
| 89 | + this.Logger.Write( |
| 90 | + LogLevel.Verbose, |
| 91 | + $"Invocation of provider '{provider.ProviderId}' completed in {invokeTimer.ElapsedMilliseconds}ms."); |
| 92 | + } |
| 93 | + catch (Exception e) |
| 94 | + { |
| 95 | + this.Logger.WriteException( |
| 96 | + $"Exception caught while invoking provider {provider.ProviderId}:", |
| 97 | + e); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + return providerResults; |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments