소스 검색

manually fixed n^2 looping problem

the AI merely wrote a new version of the algorithm with the same problem
Brandon Wong 1 년 전
부모
커밋
143be6a741
1개의 변경된 파일23개의 추가작업 그리고 19개의 파일을 삭제
  1. 23 19
      popup/popup.js

+ 23 - 19
popup/popup.js

@@ -9,6 +9,7 @@ document.addEventListener("alpine:init", () => {
   // - consider separating context menu items (rather than having a sub-menu)
   Alpine.data("playlistManager", () => ({
     playlists: {},
+    currentIndices: {},
 
     init() {
       this.loadPlaylists();
@@ -19,6 +20,24 @@ document.addEventListener("alpine:init", () => {
         const result = await browser.storage.local.get("playlists");
         console.log("LOAD RESULT", result.playlists);
         this.playlists = result.playlists || {};
+        if (result.playlists) {
+          this.currentIndices = Object.keys(result.playlists).reduce(
+            (acc, pln) => {
+              const ind = result.playlists[pln].findIndex(
+                (v) => v.status !== "done",
+              );
+              if (ind === -1) {
+                acc[pln] = result.playlists[pln].length;
+              } else {
+                acc[pln] = ind;
+              }
+              return acc;
+            },
+            {},
+          );
+        } else {
+          this.currentIndices = {};
+        }
       } catch (error) {
         console.error("Error loading playlists:", error);
       }
@@ -171,29 +190,14 @@ document.addEventListener("alpine:init", () => {
       },
     },
 
-    getCurrentVideoIndex(playlistName) {
-      const playlist = this.playlists[playlistName];
-      if (!playlist || !Array.isArray(playlist)) return -1;
-
-      // Find the index of the first video that is not "done"
-      return playlist.findIndex((video) => {
-        // Consider a video as not done if:
-        // 1. It has no status property, or
-        // 2. The status is not "done" (could be a number for partially watched)
-        return !video.status || video.status !== "done";
-      });
-    },
-
     isCurrentVideo(playlistName, index) {
-      const currentIndex = this.getCurrentVideoIndex(playlistName);
-      return currentIndex === index && currentIndex !== -1;
+      const currentIndex = this.currentIndices[playlistName];
+      return currentIndex === index;
     },
 
     isDoneVideo(playlistName, index) {
-      const currentIndex = this.getCurrentVideoIndex(playlistName);
-      // If there is no current video, or this video's index is less than current,
-      // and this video is marked as done, then it's a consecutive done video
-      return currentIndex === -1 || index < currentIndex;
+      const currentIndex = this.currentIndices[playlistName];
+      return index < currentIndex;
     },
 
     videoItemClass: {