소스 검색

Simplify preloader index calculation and improve readability

Ruben 4 달 전
부모
커밋
c36979df4a
2개의 변경된 파일5개의 추가작업 그리고 15개의 파일을 삭제
  1. 1 1
      src/PicView.Core/Preloading/PreLoaderConfig.cs
  2. 4 14
      src/PicView.Core/Preloading/Preloader.cs

+ 1 - 1
src/PicView.Core/Preloading/PreLoaderConfig.cs

@@ -7,7 +7,7 @@ public static class PreLoaderConfig
     
     /// Total items to preload forward and backward.
     public static int MaxCount => PositiveIterations + NegativeIterations; 
-
+    
     /// Leave a few cores for the UI thread and other system processes to ensure responsiveness.
     public static int MaxParallelism { get; } = Math.Max(1, Environment.ProcessorCount - 3);
 }

+ 4 - 14
src/PicView.Core/Preloading/Preloader.cs

@@ -560,19 +560,8 @@ public class PreLoader(Func<FileInfo, Task<ImageModel>> imageModelLoader) : IAsy
         CancellationToken token)
     {
         var count = list.Count;
-
-        int nextStartingIndex, prevStartingIndex;
-        if (reverse)
-        {
-            nextStartingIndex = (currentIndex - 1 + count) % count;
-            prevStartingIndex = currentIndex + 1;
-        }
-        else
-        {
-            nextStartingIndex = (currentIndex + 1) % count;
-            prevStartingIndex = currentIndex - 1;
-        }
-
+        var nextStartingIndex = (currentIndex + 1) % count;
+        var prevStartingIndex = (currentIndex - 1 + count) % count;
         var isPreloadListUnderMax = _preLoadList.Count < PreLoaderConfig.MaxCount;
         var options = new ParallelOptions
         {
@@ -630,9 +619,10 @@ public class PreLoader(Func<FileInfo, Task<ImageModel>> imageModelLoader) : IAsy
                 return;
             }
 
+            // Remove keys that are too far from the current index
             var keysToRemove = 
                 (from key in _preLoadList.Keys let distance = Math.Min(Math.Abs(key - currentIndex), list.Count - Math.Abs(key - currentIndex)) 
-                    where distance > PreLoaderConfig.PositiveIterations && distance > PreLoaderConfig.NegativeIterations 
+                    where distance > PreLoaderConfig.PositiveIterations && distance > PreLoaderConfig.NegativeIterations
                     where !additions.Contains(key) select key)
                 .AsValueEnumerable();