Merge pull request #269 from Araxeus/fix-XHR-duplicate-callback

fix song-info callback duplication
This commit is contained in:
th-ch
2021-05-28 23:18:10 +02:00
committed by GitHub
10 changed files with 56 additions and 67 deletions

View File

@ -11,6 +11,7 @@ const { setApplicationMenu } = require("./menu");
const { fileExists, injectCSS } = require("./plugins/utils"); const { fileExists, injectCSS } = require("./plugins/utils");
const { isTesting } = require("./utils/testing"); const { isTesting } = require("./utils/testing");
const { setUpTray } = require("./tray"); const { setUpTray } = require("./tray");
const { setupSongInfo } = require("./providers/song-info");
// Catch errors and log them // Catch errors and log them
unhandled({ unhandled({
@ -157,6 +158,7 @@ function createMainWindow() {
} }
app.once("browser-window-created", (event, win) => { app.once("browser-window-created", (event, win) => {
setupSongInfo(win);
loadPlugins(win); loadPlugins(win);
win.webContents.on("did-fail-load", ( win.webContents.on("did-fail-load", (

View File

@ -1,6 +1,6 @@
const Discord = require("discord-rpc"); const Discord = require("discord-rpc");
const getSongInfo = require("../../providers/song-info"); const registerCallback = require("../../providers/song-info");
const rpc = new Discord.Client({ const rpc = new Discord.Client({
transport: "ipc", transport: "ipc",
@ -12,8 +12,6 @@ const clientId = "790655993809338398";
let clearActivity; let clearActivity;
module.exports = (win, {activityTimoutEnabled, activityTimoutTime}) => { module.exports = (win, {activityTimoutEnabled, activityTimoutTime}) => {
const registerCallback = getSongInfo(win);
// If the page is ready, register the callback // If the page is ready, register the callback
win.once("ready-to-show", () => { win.once("ready-to-show", () => {
rpc.once("ready", () => { rpc.once("ready", () => {

View File

@ -4,7 +4,7 @@ const { join } = require("path");
const ID3Writer = require("browser-id3-writer"); const ID3Writer = require("browser-id3-writer");
const { dialog, ipcMain } = require("electron"); const { dialog, ipcMain } = require("electron");
const getSongInfo = require("../../providers/song-info"); const registerCallback = require("../../providers/song-info");
const { injectCSS, listenAction } = require("../utils"); const { injectCSS, listenAction } = require("../utils");
const { cropMaxWidth } = require("./utils"); const { cropMaxWidth } = require("./utils");
const { ACTIONS, CHANNEL } = require("./actions.js"); const { ACTIONS, CHANNEL } = require("./actions.js");
@ -25,7 +25,6 @@ let nowPlayingMetadata = {};
function handle(win) { function handle(win) {
injectCSS(win.webContents, join(__dirname, "style.css")); injectCSS(win.webContents, join(__dirname, "style.css"));
const registerCallback = getSongInfo(win);
registerCallback((info) => { registerCallback((info) => {
nowPlayingMetadata = info; nowPlayingMetadata = info;
}); });

View File

@ -2,13 +2,13 @@ const { existsSync, mkdirSync } = require("fs");
const { join } = require("path"); const { join } = require("path");
const { URL } = require("url"); const { URL } = require("url");
const { dialog, ipcMain } = require("electron"); const { dialog } = require("electron");
const is = require("electron-is"); const is = require("electron-is");
const ytpl = require("ytpl"); const ytpl = require("ytpl");
const chokidar = require('chokidar'); const chokidar = require('chokidar');
const { setOptions } = require("../../config/plugins"); const { setOptions } = require("../../config/plugins");
const getSongInfo = require("../../providers/song-info"); const registerCallback = require("../../providers/song-info");
const { sendError } = require("./back"); const { sendError } = require("./back");
const { defaultMenuDownloadLabel, getFolder } = require("./utils"); const { defaultMenuDownloadLabel, getFolder } = require("./utils");
@ -18,7 +18,6 @@ let callbackIsRegistered = false;
module.exports = (win, options) => { module.exports = (win, options) => {
if (!callbackIsRegistered) { if (!callbackIsRegistered) {
const registerCallback = getSongInfo(win);
registerCallback((info) => { registerCallback((info) => {
metadataURL = info.url; metadataURL = info.url;
}); });

View File

@ -2,7 +2,7 @@ const fetch = require('node-fetch');
const md5 = require('md5'); const md5 = require('md5');
const { shell } = require('electron'); const { shell } = require('electron');
const { setOptions } = require('../../config/plugins'); const { setOptions } = require('../../config/plugins');
const getSongInfo = require('../../providers/song-info'); const registerCallback = require('../../providers/song-info');
const defaultConfig = require('../../config/defaults'); const defaultConfig = require('../../config/defaults');
const createFormData = params => { const createFormData = params => {
@ -128,9 +128,7 @@ const setNowPlaying = (songInfo, config) => {
// this will store the timeout that will trigger addScrobble // this will store the timeout that will trigger addScrobble
let scrobbleTimer = undefined; let scrobbleTimer = undefined;
const lastfm = async (win, config) => { const lastfm = async (_win, config) => {
const registerCallback = getSongInfo(win);
if (!config.api_root) { if (!config.api_root) {
// settings are not present, creating them with the default values // settings are not present, creating them with the default values
config = defaultConfig.plugins['last-fm']; config = defaultConfig.plugins['last-fm'];

View File

@ -1,9 +1,9 @@
const { Notification } = require("electron"); const { Notification } = require("electron");
const is = require("electron-is"); const is = require("electron-is");
const getSongInfo = require("../../providers/song-info"); const registerCallback = require("../../providers/song-info");
const { notificationImage } = require("./utils"); const { notificationImage } = require("./utils");
const { setupInteractive, notifyInteractive } = require("./interactive") const setupInteractive = require("./interactive")
const notify = (info, options) => { const notify = (info, options) => {
@ -23,38 +23,24 @@ const notify = (info, options) => {
return currentNotification; return currentNotification;
}; };
module.exports = (win, options) => { const setup = (options) => {
const isInteractive = is.windows() && options.interactive;
//setup interactive notifications for windows
if (isInteractive) {
setupInteractive(win, options.unpauseNotification);
}
const registerCallback = getSongInfo(win);
let oldNotification; let oldNotification;
let oldURL = ""; let currentUrl;
win.once("ready-to-show", () => {
// Register the callback for new song information
registerCallback(songInfo => { registerCallback(songInfo => {
// on pause - reset url? and skip notification if (!songInfo.isPaused && (songInfo.url !== currentUrl || options.unpauseNotification)) {
if (songInfo.isPaused) {
//reset oldURL if unpause notification option is on
if (options.unpauseNotification) {
oldURL = "";
}
return;
}
// If url isn't the same as last one - send notification
if (songInfo.url !== oldURL) {
oldURL = songInfo.url;
if (isInteractive) {
notifyInteractive(songInfo);
} else {
// Close the old notification // Close the old notification
oldNotification?.close(); oldNotification?.close();
currentUrl = songInfo.url;
// This fixes a weird bug that would cause the notification to be updated instead of showing // This fixes a weird bug that would cause the notification to be updated instead of showing
setTimeout(() => { oldNotification = notify(songInfo, options) }, 10); setTimeout(() => { oldNotification = notify(songInfo, options) }, 10);
} }
});
} }
});
}); module.exports = (win, options) => {
// Register the callback for new song information
is.windows() && options.interactive ?
setupInteractive(win, options.unpauseNotification) :
setup(options);
}; };

View File

@ -1,17 +1,27 @@
const { notificationImage, icons } = require("./utils"); const { notificationImage, icons } = require("./utils");
const getSongControls = require('../../providers/song-controls'); const getSongControls = require('../../providers/song-controls');
const registerCallback = require("../../providers/song-info");
const notifier = require("node-notifier"); const notifier = require("node-notifier");
//store song controls reference on launch //store song controls reference on launch
let controls; let controls;
let notificationOnPause; let notificationOnUnpause;
module.exports = (win, unpauseNotification) => {
//Save controls and onPause option //Save controls and onPause option
module.exports.setupInteractive = (win, unpauseNotification) => {
const { playPause, next, previous } = getSongControls(win); const { playPause, next, previous } = getSongControls(win);
controls = { playPause, next, previous }; controls = { playPause, next, previous };
notificationOnUnpause = unpauseNotification;
notificationOnPause = unpauseNotification; let currentUrl;
// Register songInfoCallback
registerCallback(songInfo => {
if (!songInfo.isPaused && (songInfo.url !== currentUrl || notificationOnUnpause)) {
currentUrl = songInfo.url;
sendToaster(songInfo);
}
});
win.webContents.once("closed", () => { win.webContents.once("closed", () => {
deleteNotification() deleteNotification()
@ -33,7 +43,7 @@ function deleteNotification() {
} }
//New notification //New notification
module.exports.notifyInteractive = function sendToaster(songInfo) { function sendToaster(songInfo) {
deleteNotification(); deleteNotification();
//download image and get path //download image and get path
let imgSrc = notificationImage(songInfo, true); let imgSrc = notificationImage(songInfo, true);
@ -71,7 +81,7 @@ module.exports.notifyInteractive = function sendToaster(songInfo) {
// dont delete notification on play/pause // dont delete notification on play/pause
toDelete = undefined; toDelete = undefined;
//manually send notification if not sending automatically //manually send notification if not sending automatically
if (!notificationOnPause) { if (!notificationOnUnpause) {
songInfo.isPaused = false; songInfo.isPaused = false;
sendToaster(songInfo); sendToaster(songInfo);
} }

View File

@ -1,12 +1,11 @@
const getSongControls = require('../../providers/song-controls'); const getSongControls = require('../../providers/song-controls');
const getSongInfo = require('../../providers/song-info'); const registerCallback = require('../../providers/song-info');
const path = require('path'); const path = require('path');
let controls; let controls;
let currentSongInfo; let currentSongInfo;
module.exports = win => { module.exports = win => {
const registerCallback = getSongInfo(win);
const { playPause, next, previous } = getSongControls(win); const { playPause, next, previous } = getSongControls(win);
controls = { playPause, next, previous }; controls = { playPause, next, previous };

View File

@ -7,7 +7,7 @@ const {
TouchBarScrubber, TouchBarScrubber,
} = TouchBar; } = TouchBar;
const getSongInfo = require("../../providers/song-info"); const registerCallback = require("../../providers/song-info");
const getSongControls = require("../../providers/song-controls"); const getSongControls = require("../../providers/song-controls");
// Songtitle label // Songtitle label
@ -59,7 +59,6 @@ const touchBar = new TouchBar({
}); });
module.exports = (win) => { module.exports = (win) => {
const registerCallback = getSongInfo(win);
const { playPause, next, previous, like, dislike } = getSongControls(win); const { playPause, next, previous, like, dislike } = getSongControls(win);
// If the page is ready, register the callback // If the page is ready, register the callback

View File

@ -64,7 +64,6 @@ const handleData = async (responseText, win) => {
win.webContents.send("update-song-info", JSON.stringify(songInfo)); win.webContents.send("update-song-info", JSON.stringify(songInfo));
}; };
const registerProvider = (win) => {
// This variable will be filled with the callbacks once they register // This variable will be filled with the callbacks once they register
const callbacks = []; const callbacks = [];
@ -73,6 +72,7 @@ const registerProvider = (win) => {
callbacks.push(callback); callbacks.push(callback);
}; };
const registerProvider = (win) => {
win.on("page-title-updated", async () => { win.on("page-title-updated", async () => {
// Get and set the new data // Get and set the new data
songInfo.isPaused = await getPausedStatus(win); songInfo.isPaused = await getPausedStatus(win);
@ -93,8 +93,6 @@ const registerProvider = (win) => {
c(songInfo); c(songInfo);
}); });
}); });
return registerCallback;
}; };
const suffixesToRemove = [' - Topic', 'VEVO']; const suffixesToRemove = [' - Topic', 'VEVO'];
@ -110,7 +108,8 @@ function cleanupArtistName(artist) {
return artist; return artist;
} }
module.exports = registerProvider; module.exports = registerCallback;
module.exports.setupSongInfo = registerProvider;
module.exports.getImage = getImage; module.exports.getImage = getImage;
module.exports.cleanupArtistName = cleanupArtistName; module.exports.cleanupArtistName = cleanupArtistName;