Simplify local playlist indexeddb queries

This commit is contained in:
Bnyro 2023-06-19 14:28:09 +02:00
parent d044accf5e
commit ee57a0c348

View File

@ -194,9 +194,6 @@ const mixin = {
timeAgo(time) { timeAgo(time) {
return timeAgo.format(time); return timeAgo.format(time);
}, },
async delay(millis) {
return await new Promise(r => setTimeout(r, millis));
},
urlify(string) { urlify(string) {
if (!string) return ""; if (!string) return "";
const urlRegex = /(((https?:\/\/)|(www\.))[^\s]+)/g; const urlRegex = /(((https?:\/\/)|(www\.))[^\s]+)/g;
@ -296,18 +293,17 @@ const mixin = {
store.delete(groupName); store.delete(groupName);
}, },
async getLocalPlaylist(playlistId) { async getLocalPlaylist(playlistId) {
var tx = window.db.transaction("playlists", "readonly"); return await new Promise(resolve => {
var store = tx.objectStore("playlists"); var tx = window.db.transaction("playlists", "readonly");
const req = store.openCursor(playlistId); var store = tx.objectStore("playlists");
let playlist = null; const req = store.openCursor(playlistId);
req.onsuccess = e => { let playlist = null;
playlist = e.target.result.value; req.onsuccess = e => {
}; playlist = e.target.result.value;
while (playlist == null) { playlist.videos = JSON.parse(playlist.videoIds).length;
await this.delay(10); resolve(playlist);
} };
playlist.videos = JSON.parse(playlist.videoIds).length; });
return playlist;
}, },
createOrUpdateLocalPlaylist(playlist) { createOrUpdateLocalPlaylist(playlist) {
var tx = window.db.transaction("playlists", "readwrite"); var tx = window.db.transaction("playlists", "readwrite");
@ -336,41 +332,35 @@ const mixin = {
store.put(video); store.put(video);
}, },
async getLocalPlaylistVideo(videoId) { async getLocalPlaylistVideo(videoId) {
var tx = window.db.transaction("playlist_videos", "readonly"); return await new Promise(resolve => {
var store = tx.objectStore("playlist_videos"); var tx = window.db.transaction("playlist_videos", "readonly");
const req = store.openCursor(videoId); var store = tx.objectStore("playlist_videos");
let video = null; const req = store.openCursor(videoId);
req.onsuccess = e => { req.onsuccess = e => {
video = e.target.result.value; resolve(e.target.result.value);
}; };
while (video == null) { });
await this.delay(10);
}
return video;
}, },
async getPlaylists() { async getPlaylists() {
if (!this.authenticated) { if (!this.authenticated) {
if (!window.db) return []; if (!window.db) return [];
let finished = false; return await new Promise(resolve => {
let playlists = []; let playlists = [];
var tx = window.db.transaction("playlists", "readonly"); var tx = window.db.transaction("playlists", "readonly");
var store = tx.objectStore("playlists"); var store = tx.objectStore("playlists");
const cursorRequest = store.openCursor(); const cursorRequest = store.openCursor();
cursorRequest.onsuccess = e => { cursorRequest.onsuccess = e => {
const cursor = e.target.result; const cursor = e.target.result;
if (cursor) { if (cursor) {
let playlist = cursor.value; let playlist = cursor.value;
playlist.videos = JSON.parse(playlist.videoIds).length; playlist.videos = JSON.parse(playlist.videoIds).length;
playlists.push(playlist); playlists.push(playlist);
cursor.continue(); cursor.continue();
} else { } else {
finished = true; resolve(playlists);
} }
}; };
while (!finished) { });
await this.delay(10);
}
return playlists;
} }
return await this.fetchJson(this.authApiUrl() + "/user/playlists", null, { return await this.fetchJson(this.authApiUrl() + "/user/playlists", null, {