use pseudo decorators

This commit is contained in:
Araxeus
2023-03-12 21:41:15 +02:00
parent 7abc67b456
commit f1073e37b5
6 changed files with 82 additions and 105 deletions

View File

@ -4,6 +4,8 @@ const fetch = require("node-fetch");
const config = require("../config");
const { cache } = require("../providers/decorators")
// Fill songInfo with empty values
/**
* @typedef {songInfo} SongInfo
@ -25,16 +27,21 @@ const songInfo = {
};
// Grab the native image using the src
const getImage = async (src) => {
const result = await fetch(src);
const buffer = await result.buffer();
const output = nativeImage.createFromBuffer(buffer);
if (output.isEmpty() && !src.endsWith(".jpg") && src.includes(".jpg")) { // fix hidden webp files (https://github.com/th-ch/youtube-music/issues/315)
return getImage(src.slice(0, src.lastIndexOf(".jpg") + 4));
} else {
return output;
const getImage = cache (
/**
* @returns {Promise<Electron.NativeImage>}
*/
async (src) => {
const result = await fetch(src);
const buffer = await result.buffer();
const output = nativeImage.createFromBuffer(buffer);
if (output.isEmpty() && !src.endsWith(".jpg") && src.includes(".jpg")) { // fix hidden webp files (https://github.com/th-ch/youtube-music/issues/315)
return getImage(src.slice(0, src.lastIndexOf(".jpg") + 4));
} else {
return output;
}
}
};
);
const handleData = async (responseText, win) => {
const data = JSON.parse(responseText);
@ -60,13 +67,10 @@ const handleData = async (responseText, win) => {
songInfo.videoId = videoDetails.videoId;
songInfo.album = data?.videoDetails?.album; // Will be undefined if video exist
const oldUrl = songInfo.imageSrc;
const thumbnails = videoDetails.thumbnail?.thumbnails;
songInfo.imageSrc = thumbnails[thumbnails.length - 1]?.url.split("?")[0];
if (oldUrl !== songInfo.imageSrc) {
songInfo.image = await getImage(songInfo.imageSrc);
}
songInfo.image = await getImage(songInfo.imageSrc);
win.webContents.send("update-song-info", JSON.stringify(songInfo));
}
};