Use index for sorting Watch History.

This commit is contained in:
Kavin 2022-10-20 16:59:22 +01:00
parent 5bf24200b7
commit 05e78fed6e
No known key found for this signature in database
GPG key ID: 49451E4482CC5BCD
2 changed files with 7 additions and 4 deletions

View file

@ -42,8 +42,8 @@ export default {
}); });
if (this.getPreferenceBoolean("watchHistory", false)) if (this.getPreferenceBoolean("watchHistory", false))
if ("indexedDB" in window) { if ("indexedDB" in window) {
const request = indexedDB.open("piped-db", 1); const request = indexedDB.open("piped-db", 2);
request.onupgradeneeded = function () { request.onupgradeneeded = ev => {
const db = request.result; const db = request.result;
console.log("Upgrading object store."); console.log("Upgrading object store.");
if (!db.objectStoreNames.contains("watch_history")) { if (!db.objectStoreNames.contains("watch_history")) {
@ -51,6 +51,10 @@ export default {
store.createIndex("video_id_idx", "videoId", { unique: true }); store.createIndex("video_id_idx", "videoId", { unique: true });
store.createIndex("id_idx", "id", { unique: true, autoIncrement: true }); store.createIndex("id_idx", "id", { unique: true, autoIncrement: true });
} }
if (ev.oldVersion < 2) {
const store = request.transaction.objectStore("watch_history");
store.createIndex("watchedAt", "watchedAt", { unique: false });
}
}; };
request.onsuccess = e => { request.onsuccess = e => {
window.db = e.target.result; window.db = e.target.result;

View file

@ -39,7 +39,7 @@ export default {
if (window.db) { if (window.db) {
var tx = window.db.transaction("watch_history", "readonly"); var tx = window.db.transaction("watch_history", "readonly");
var store = tx.objectStore("watch_history"); var store = tx.objectStore("watch_history");
const cursorRequest = store.openCursor(); const cursorRequest = store.index("watchedAt").openCursor(null, "prev");
cursorRequest.onsuccess = e => { cursorRequest.onsuccess = e => {
const cursor = e.target.result; const cursor = e.target.result;
if (cursor) { if (cursor) {
@ -53,7 +53,6 @@ export default {
thumbnail: video.thumbnail, thumbnail: video.thumbnail,
watchedAt: video.watchedAt, watchedAt: video.watchedAt,
}); });
this.videos.sort((a, b) => b.watchedAt - a.watchedAt); // TODO: Optimize
if (this.videos.length < 1000) cursor.continue(); if (this.videos.length < 1000) cursor.continue();
} }
}; };