From d501206dec5be4070d66927ff824f5b4bb5e072a Mon Sep 17 00:00:00 2001 From: Jonas Nyrup Date: Sun, 18 Aug 2024 16:43:48 +0200 Subject: [PATCH] Replace small dictionaries with switch --- .../BrowserData/InstalledBrowser.cs | 20 +++++++++--------- lib/PuppeteerSharp/BrowserFetcher.cs | 20 ++++++++---------- lib/PuppeteerSharp/Cdp/CdpPage.cs | 21 ++++++++++--------- lib/PuppeteerSharp/Cdp/LifecycleWatcher.cs | 20 +++++++++--------- lib/PuppeteerSharp/ScreenshotOptions.cs | 19 +++++++---------- 5 files changed, 48 insertions(+), 52 deletions(-) diff --git a/lib/PuppeteerSharp/BrowserData/InstalledBrowser.cs b/lib/PuppeteerSharp/BrowserData/InstalledBrowser.cs index 7b82bd661..dff41b1b4 100644 --- a/lib/PuppeteerSharp/BrowserData/InstalledBrowser.cs +++ b/lib/PuppeteerSharp/BrowserData/InstalledBrowser.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.IO; namespace PuppeteerSharp.BrowserData @@ -9,14 +8,6 @@ namespace PuppeteerSharp.BrowserData /// public class InstalledBrowser { - private static readonly Dictionary> _executablePathByBrowser = new() - { - [SupportedBrowser.Chrome] = Chrome.RelativeExecutablePath, - [SupportedBrowser.ChromeHeadlessShell] = ChromeHeadlessShell.RelativeExecutablePath, - [SupportedBrowser.Chromium] = Chromium.RelativeExecutablePath, - [SupportedBrowser.Firefox] = Firefox.RelativeExecutablePath, - }; - /// /// Initializes a new instance of the class. /// @@ -62,7 +53,16 @@ public string GetExecutablePath() var installationDir = Cache.GetInstallationDir(Browser, Platform, BuildId); return Path.Combine( installationDir, - _executablePathByBrowser[Browser](Platform, BuildId)); + GetExecutablePath(Browser, Platform, BuildId)); } + + private static string GetExecutablePath(SupportedBrowser browser, Platform platform, string buildId) => browser switch + { + SupportedBrowser.Chrome => Chrome.RelativeExecutablePath(platform, buildId), + SupportedBrowser.ChromeHeadlessShell => ChromeHeadlessShell.RelativeExecutablePath(platform, buildId), + SupportedBrowser.Chromium => Chromium.RelativeExecutablePath(platform, buildId), + SupportedBrowser.Firefox => Firefox.RelativeExecutablePath(platform, buildId), + _ => throw new NotSupportedException(), + }; } } diff --git a/lib/PuppeteerSharp/BrowserFetcher.cs b/lib/PuppeteerSharp/BrowserFetcher.cs index ef62ed5f8..b3393b0b3 100644 --- a/lib/PuppeteerSharp/BrowserFetcher.cs +++ b/lib/PuppeteerSharp/BrowserFetcher.cs @@ -22,14 +22,6 @@ public sealed class BrowserFetcher : IBrowserFetcher { private const string PublishSingleFileLocalApplicationDataFolderName = "PuppeteerSharp"; - private static readonly Dictionary> _downloadsUrl = new() - { - [SupportedBrowser.Chrome] = Chrome.ResolveDownloadUrl, - [SupportedBrowser.ChromeHeadlessShell] = ChromeHeadlessShell.ResolveDownloadUrl, - [SupportedBrowser.Chromium] = Chromium.ResolveDownloadUrl, - [SupportedBrowser.Firefox] = Firefox.ResolveDownloadUrl, - }; - private readonly CustomFileDownloadAction _customFileDownload; private readonly ILogger _logger; @@ -206,8 +198,14 @@ internal static string GetBrowsersLocation() return assemblyDirectory.FullName; } - private static string GetDownloadURL(SupportedBrowser browser, Platform platform, string baseUrl, string buildId) - => _downloadsUrl[browser](platform, buildId, baseUrl); + private static string GetDownloadURL(SupportedBrowser browser, Platform platform, string baseUrl, string buildId) => browser switch + { + SupportedBrowser.Chrome => Chrome.ResolveDownloadUrl(platform, buildId, baseUrl), + SupportedBrowser.ChromeHeadlessShell => ChromeHeadlessShell.ResolveDownloadUrl(platform, buildId, baseUrl), + SupportedBrowser.Chromium => Chromium.ResolveDownloadUrl(platform, buildId, baseUrl), + SupportedBrowser.Firefox => Firefox.ResolveDownloadUrl(platform, buildId, baseUrl), + _ => throw new NotSupportedException(), + }; private static void ExtractTar(string zipPath, string folderPath) { @@ -236,7 +234,7 @@ private static void ExecuteSetup(string exePath, string folderPath) private async Task DownloadAsync(SupportedBrowser browser, string buildId) { - var url = _downloadsUrl[browser](Platform, buildId, BaseUrl); + var url = GetDownloadURL(browser, Platform, BaseUrl, buildId); var fileName = url.Split('/').Last(); var cache = new Cache(CacheDir); var archivePath = Path.Combine(CacheDir, fileName); diff --git a/lib/PuppeteerSharp/Cdp/CdpPage.cs b/lib/PuppeteerSharp/Cdp/CdpPage.cs index ea8f1a6c1..94db19b92 100644 --- a/lib/PuppeteerSharp/Cdp/CdpPage.cs +++ b/lib/PuppeteerSharp/Cdp/CdpPage.cs @@ -43,14 +43,6 @@ namespace PuppeteerSharp.Cdp; /// public class CdpPage : Page { - private static readonly Dictionary _unitToPixels = new() - { - ["px"] = 1, - ["in"] = 96, - ["cm"] = 37.8m, - ["mm"] = 3.78m, - }; - private readonly ConcurrentDictionary _workers = new(); private readonly ITargetManager _targetManager; private readonly EmulationManager _emulationManager; @@ -920,6 +912,15 @@ protected override async Task PerformScreenshotAsync(ScreenshotType type } } + private static decimal? GetPixels(string unit) => unit switch + { + "px" => 1, + "in" => 96, + "cm" => 37.8m, + "mm" => 3.78m, + _ => null, + }; + private void SetupPrimaryTargetListeners() { PrimaryTargetClient.Ready += OnAttachedToTarget; @@ -1230,7 +1231,7 @@ private decimal ConvertPrintParameterToInches(object parameter) var text = parameter.ToString(); var unit = text.Length > 2 ? text.Substring(text.Length - 2).ToLower(CultureInfo.CurrentCulture) : string.Empty; string valueText; - if (_unitToPixels.ContainsKey(unit)) + if (GetPixels(unit) is { }) { valueText = text.Substring(0, text.Length - 2); } @@ -1244,7 +1245,7 @@ private decimal ConvertPrintParameterToInches(object parameter) if (decimal.TryParse(valueText, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out var number)) { - pixels = number * _unitToPixels[unit]; + pixels = number * GetPixels(unit).Value; } else { diff --git a/lib/PuppeteerSharp/Cdp/LifecycleWatcher.cs b/lib/PuppeteerSharp/Cdp/LifecycleWatcher.cs index bc2d803ca..5fd628677 100644 --- a/lib/PuppeteerSharp/Cdp/LifecycleWatcher.cs +++ b/lib/PuppeteerSharp/Cdp/LifecycleWatcher.cs @@ -11,15 +11,6 @@ namespace PuppeteerSharp.Cdp { internal sealed class LifecycleWatcher : IDisposable { - private static readonly Dictionary _puppeteerToProtocolLifecycle = - new() - { - [WaitUntilNavigation.Load] = "load", - [WaitUntilNavigation.DOMContentLoaded] = "DOMContentLoaded", - [WaitUntilNavigation.Networkidle0] = "networkIdle", - [WaitUntilNavigation.Networkidle2] = "networkAlmostIdle", - }; - private static readonly WaitUntilNavigation[] _defaultWaitUntil = [WaitUntilNavigation.Load]; private readonly NetworkManager _networkManager; @@ -44,7 +35,7 @@ public LifecycleWatcher( { _expectedLifecycle = (waitUntil ?? _defaultWaitUntil).Select(w => { - var protocolEvent = _puppeteerToProtocolLifecycle.GetValue(w); + var protocolEvent = GetProtocolEvent(w); Contract.Assert(protocolEvent != null, $"Unknown value for options.waitUntil: {w}"); return protocolEvent; }); @@ -87,6 +78,15 @@ public void Dispose() _terminationCancellationToken.Dispose(); } + private static string GetProtocolEvent(WaitUntilNavigation waitUntil) => waitUntil switch + { + WaitUntilNavigation.Load => "load", + WaitUntilNavigation.DOMContentLoaded => "DOMContentLoaded", + WaitUntilNavigation.Networkidle0 => "networkIdle", + WaitUntilNavigation.Networkidle2 => "networkAlmostIdle", + _ => null, + }; + private void Navigated(object sender, FrameNavigatedEventArgs e) { if (e.Type == NavigationType.BackForwardCacheRestore) diff --git a/lib/PuppeteerSharp/ScreenshotOptions.cs b/lib/PuppeteerSharp/ScreenshotOptions.cs index 30bc342fa..f07477c05 100644 --- a/lib/PuppeteerSharp/ScreenshotOptions.cs +++ b/lib/PuppeteerSharp/ScreenshotOptions.cs @@ -1,4 +1,3 @@ -using System.Collections.Generic; using System.IO; using System.Text.Json.Serialization; using PuppeteerSharp.Media; @@ -10,14 +9,6 @@ namespace PuppeteerSharp /// public class ScreenshotOptions { - private static readonly Dictionary _extensionScreenshotTypeMap = new() - { - ["jpe"] = ScreenshotType.Jpeg, - ["jpeg"] = ScreenshotType.Jpeg, - ["jpg"] = ScreenshotType.Jpeg, - ["png"] = ScreenshotType.Png, - }; - /// /// Specifies clipping region of the page. /// @@ -87,8 +78,14 @@ public class ScreenshotOptions internal static ScreenshotType? GetScreenshotTypeFromFile(string file) { var extension = new FileInfo(file).Extension.Replace(".", string.Empty); - _extensionScreenshotTypeMap.TryGetValue(extension, out var result); - return result; + return GetScreenshotType(extension); } + + private static ScreenshotType? GetScreenshotType(string extension) => extension switch + { + "jpe" or "jpeg" or "jpg" => ScreenshotType.Jpeg, + "png" => ScreenshotType.Png, + _ => null, + }; } }