Skip to content

Commit

Permalink
setup SongInfo **once**
Browse files Browse the repository at this point in the history
  • Loading branch information
Araxeus committed May 10, 2021
1 parent 7942efa commit 7cf78c6
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 25 deletions.
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const { setApplicationMenu } = require("./menu");
const { fileExists, injectCSS } = require("./plugins/utils");
const { isTesting } = require("./utils/testing");
const { setUpTray } = require("./tray");
const { setupSongInfo } = require("./providers/song-info");

// Catch errors and log them
unhandled({
Expand Down Expand Up @@ -157,6 +158,7 @@ function createMainWindow() {
}

app.once("browser-window-created", (event, win) => {
setupSongInfo(win);
loadPlugins(win);

win.webContents.on("did-fail-load", (
Expand Down
4 changes: 1 addition & 3 deletions plugins/discord/back.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const Discord = require("discord-rpc");

const getSongInfo = require("../../providers/song-info");
const registerCallback = require("../../providers/song-info");

const rpc = new Discord.Client({
transport: "ipc",
Expand All @@ -12,8 +12,6 @@ const clientId = "790655993809338398";
let clearActivity;

module.exports = (win, {activityTimoutEnabled, activityTimoutTime}) => {
const registerCallback = getSongInfo(win);

// If the page is ready, register the callback
win.once("ready-to-show", () => {
rpc.once("ready", () => {
Expand Down
3 changes: 1 addition & 2 deletions plugins/downloader/back.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { join } = require("path");
const ID3Writer = require("browser-id3-writer");
const { dialog, ipcMain } = require("electron");

const getSongInfo = require("../../providers/song-info");
const registerCallback = require("../../providers/song-info");
const { injectCSS, listenAction } = require("../utils");
const { cropMaxWidth } = require("./utils");
const { ACTIONS, CHANNEL } = require("./actions.js");
Expand All @@ -25,7 +25,6 @@ let nowPlayingMetadata = {};

function handle(win) {
injectCSS(win.webContents, join(__dirname, "style.css"));
const registerCallback = getSongInfo(win);
registerCallback((info) => {
nowPlayingMetadata = info;
});
Expand Down
6 changes: 2 additions & 4 deletions plugins/last-fm/back.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const fetch = require('node-fetch');
const md5 = require('md5');
const { shell } = require('electron');
const { setOptions } = require('../../config/plugins');
const getSongInfo = require('../../providers/song-info');
const registerCallback = require('../../providers/song-info');
const defaultConfig = require('../../config/defaults');

const createFormData = params => {
Expand Down Expand Up @@ -128,9 +128,7 @@ const setNowPlaying = (songInfo, config) => {
// this will store the timeout that will trigger addScrobble
let scrobbleTimer = undefined;

const lastfm = async (win, config) => {
const registerCallback = getSongInfo(win);

const lastfm = async (_win, config) => {
if (!config.api_root) {
// settings are not present, creating them with the default values
config = defaultConfig.plugins['last-fm'];
Expand Down
3 changes: 1 addition & 2 deletions plugins/notifications/back.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { Notification } = require("electron");
const is = require("electron-is");
const getSongInfo = require("../../providers/song-info");
const registerCallback = require("../../providers/song-info");
const { notificationImage } = require("./utils");

const { setupInteractive, notifyInteractive } = require("./interactive")
Expand Down Expand Up @@ -29,7 +29,6 @@ module.exports = (win, options) => {
if (isInteractive) {
setupInteractive(win, options.unpauseNotification);
}
const registerCallback = getSongInfo(win);
let oldNotification;
let oldURL = "";
win.once("ready-to-show", () => {
Expand Down
3 changes: 1 addition & 2 deletions plugins/taskbar-mediacontrol/back.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
const getSongControls = require('../../providers/song-controls');
const getSongInfo = require('../../providers/song-info');
const registerCallback = require('../../providers/song-info');
const path = require('path');

let controls;
let currentSongInfo;

module.exports = win => {
const registerCallback = getSongInfo(win);
const { playPause, next, previous } = getSongControls(win);
controls = { playPause, next, previous };

Expand Down
3 changes: 1 addition & 2 deletions plugins/touchbar/back.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const {
TouchBarScrubber,
} = TouchBar;

const getSongInfo = require("../../providers/song-info");
const registerCallback = require("../../providers/song-info");
const getSongControls = require("../../providers/song-controls");

// Songtitle label
Expand Down Expand Up @@ -59,7 +59,6 @@ const touchBar = new TouchBar({
});

module.exports = (win) => {
const registerCallback = getSongInfo(win);
const { playPause, next, previous, like, dislike } = getSongControls(win);

// If the page is ready, register the callback
Expand Down
20 changes: 10 additions & 10 deletions providers/song-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const songInfo = {
};

const handleData = async (responseText, win) => {
console.log("handling song-info")
let data = JSON.parse(responseText);
songInfo.title = data?.videoDetails?.title;
songInfo.artist = await getArtist(win) || cleanupArtistName(data?.videoDetails?.author);
Expand All @@ -64,15 +65,15 @@ const handleData = async (responseText, win) => {
win.webContents.send("update-song-info", JSON.stringify(songInfo));
};

const registerProvider = (win) => {
// This variable will be filled with the callbacks once they register
const callbacks = [];
// This variable will be filled with the callbacks once they register
const callbacks = [];

// This function will allow plugins to register callback that will be triggered when data changes
const registerCallback = (callback) => {
callbacks.push(callback);
};
// This function will allow plugins to register callback that will be triggered when data changes
const registerCallback = (callback) => {
callbacks.push(callback);
};

const registerProvider = (win) => {
win.on("page-title-updated", async () => {
// Get and set the new data
songInfo.isPaused = await getPausedStatus(win);
Expand All @@ -93,8 +94,6 @@ const registerProvider = (win) => {
c(songInfo);
});
});

return registerCallback;
};

const suffixesToRemove = [' - Topic', 'VEVO'];
Expand All @@ -110,7 +109,8 @@ function cleanupArtistName(artist) {
return artist;
}

module.exports = registerProvider;
module.exports = registerCallback;
module.exports.setupSongInfo = registerProvider;
module.exports.getImage = getImage;
module.exports.cleanupArtistName = cleanupArtistName;

0 comments on commit 7cf78c6

Please sign in to comment.