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

@ -1,5 +1,6 @@
const { ipcRenderer } = require("electron");
const { getImage } = require("./song-info");
const { singleton } = require("../providers/decorators");
global.songInfo = {};
@ -14,17 +15,8 @@ ipcRenderer.on("update-song-info", async (_, extractedSongInfo) => {
// used because 'loadeddata' or 'loadedmetadata' weren't firing on song start for some users (https://github.com/th-ch/youtube-music/issues/473)
const srcChangedEvent = new CustomEvent('srcChanged');
const singleton = (fn) => {
let called = false;
return (...args) => {
if (called) return;
called = true;
return fn(...args);
}
}
module.exports.setupSeekedListener = singleton(() => {
document.querySelector('video')?.addEventListener('seeked', v => ipcRenderer.send('seeked', v.target.currentTime));
$('video')?.addEventListener('seeked', v => ipcRenderer.send('seeked', v.target.currentTime));
});
module.exports.setupTimeChangedListener = singleton(() => {

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));
}
};