From 9e84d196919416babd1105f7742041af897d869c Mon Sep 17 00:00:00 2001 From: jerry08 Date: Thu, 3 Oct 2024 14:02:18 -0400 Subject: [PATCH] Use .NET 9 features: `Lock` and `Index()` --- Yosu.Youtube.Converter/Converter.cs | 2 +- .../Utils/Extensions/CollectionExtensions.cs | 13 ------------- Yosu.Youtube.Converter/Utils/ProgressMuxer.cs | 7 ++++--- 3 files changed, 5 insertions(+), 17 deletions(-) delete mode 100644 Yosu.Youtube.Converter/Utils/Extensions/CollectionExtensions.cs diff --git a/Yosu.Youtube.Converter/Converter.cs b/Yosu.Youtube.Converter/Converter.cs index 39faef2..b8c3a43 100644 --- a/Yosu.Youtube.Converter/Converter.cs +++ b/Yosu.Youtube.Converter/Converter.cs @@ -133,7 +133,7 @@ private async ValueTask ProcessAsync( } // Metadata for subtitles - foreach (var (subtitleInput, i) in subtitleInputs.WithIndex()) + foreach (var (i, subtitleInput) in subtitleInputs.Index()) { // Language codes can be stored in any format, but most players expect // three-letter codes, so we'll try to convert to that first. diff --git a/Yosu.Youtube.Converter/Utils/Extensions/CollectionExtensions.cs b/Yosu.Youtube.Converter/Utils/Extensions/CollectionExtensions.cs deleted file mode 100644 index 739118d..0000000 --- a/Yosu.Youtube.Converter/Utils/Extensions/CollectionExtensions.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System.Collections.Generic; - -namespace Yosu.Youtube.Converter.Utils.Extensions; - -internal static class CollectionExtensions -{ - public static IEnumerable<(T value, int index)> WithIndex(this IEnumerable source) - { - var i = 0; - foreach (var o in source) - yield return (o, i++); - } -} diff --git a/Yosu.Youtube.Converter/Utils/ProgressMuxer.cs b/Yosu.Youtube.Converter/Utils/ProgressMuxer.cs index 8fa228e..73dba13 100644 --- a/Yosu.Youtube.Converter/Utils/ProgressMuxer.cs +++ b/Yosu.Youtube.Converter/Utils/ProgressMuxer.cs @@ -1,17 +1,18 @@ using System; using System.Collections.Generic; +using System.Threading; namespace Yosu.Youtube.Converter.Utils; internal class ProgressMuxer(IProgress target) { - private readonly object _lock = new(); + private readonly Lock _lock = new(); private readonly Dictionary _splitWeights = new(); private readonly Dictionary _splitValues = new(); public IProgress CreateInput(double weight = 1) { - lock (_lock) + using (_lock.EnterScope()) { var index = _splitWeights.Count; @@ -20,7 +21,7 @@ public IProgress CreateInput(double weight = 1) return new Progress(p => { - lock (_lock) + using (_lock.EnterScope()) { _splitValues[index] = p;