From 6fa61558179873613498c4e3285792fcb50c0f3d Mon Sep 17 00:00:00 2001 From: njzy Date: Fri, 4 Dec 2020 00:16:18 +0800 Subject: [PATCH] fix(unblockMusic): the song information may fail to be fetched and reduce the request time 1. Increase the timeout time to prevent blocking the rendering process 2. Use the modification package to solve the problem that song information may not be available and repeatedly requested. --- package.json | 2 +- src/electron/ipcMain.js | 25 +++++++++++++++++++++---- src/store/actions.js | 2 +- vue.config.js | 2 +- yarn.lock | 10 +++++----- 5 files changed, 29 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index 4186308..417dbb2 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,7 @@ }, "main": "background.js", "dependencies": { - "@nondanee/unblockneteasemusic": "^0.25.3", + "@njzy/unblockneteasemusic": "^0.25.3", "axios": "^0.21.0", "big-integer": "^1.6.48", "core-js": "^3.6.5", diff --git a/src/electron/ipcMain.js b/src/electron/ipcMain.js index 24877ef..a631697 100644 --- a/src/electron/ipcMain.js +++ b/src/electron/ipcMain.js @@ -1,5 +1,5 @@ import { app, ipcMain } from "electron"; -import match from "@nondanee/unblockneteasemusic"; +import match from "@njzy/unblockneteasemusic"; export function initIpcMain(win) { // Make vuex copy for electron. @@ -9,10 +9,27 @@ export function initIpcMain(win) { global.vuexCopy = state; }); - ipcMain.on("unblock-music", (event, id) => { - match(id, ["qq", "kuwo", "migu"]).then((res) => { - event.returnValue = res; + ipcMain.on("unblock-music", (event, track) => { + // 兼容 unblockneteasemusic 所使用的 api 字段 + track.alias = track.alia || []; + track.duration = track.dt || 0; + track.album = track.al || []; + track.artists = track.ar || []; + + const matchPromise = match(track.id, ["qq", "kuwo", "migu"], track); + const timeoutPromise = new Promise((_, reject) => { + setTimeout(() => { + reject("timeout"); + }, 3000); }); + Promise.race([matchPromise, timeoutPromise]) + .then((res) => { + event.returnValue = res; + }) + .catch((err) => { + console.log("unblock music error: ", err); + event.returnValue = null; + }); }); ipcMain.on("close", () => { diff --git a/src/store/actions.js b/src/store/actions.js index 66a0930..23fd640 100644 --- a/src/store/actions.js +++ b/src/store/actions.js @@ -43,7 +43,7 @@ export default { if (track.playable === false) { let res = undefined; if (process.env.IS_ELECTRON === true) { - res = ipcRenderer.sendSync("unblock-music", track.id); + res = ipcRenderer.sendSync("unblock-music", track); } if (res?.url) { commitMP3(res.url); diff --git a/vue.config.js b/vue.config.js index 1f5b623..7b07594 100644 --- a/vue.config.js +++ b/vue.config.js @@ -49,7 +49,7 @@ module.exports = { pluginOptions: { // electron-builder的配置文件 electronBuilder: { - externals: ["@nondanee/unblockneteasemusic"], + externals: ["@nondanee/unblockneteasemusic", "@njzy/unblockneteasemusic"], builderOptions: { productName: "YesPlayMusic", copyright: "Copyright © YesPlayMusic", diff --git a/yarn.lock b/yarn.lock index 8da41d4..abf8cc0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1265,16 +1265,16 @@ call-me-maybe "^1.0.1" glob-to-regexp "^0.3.0" +"@njzy/unblockneteasemusic@^0.25.3": + version "0.25.3" + resolved "https://registry.yarnpkg.com/@njzy/unblockneteasemusic/-/unblockneteasemusic-0.25.3.tgz#f8223df756ab3a723af066a75b91357a2b162443" + integrity sha512-rY9SMf6VbJueIvVW22Ut4dX48PBSETvdCoVJNdbKzOMacgxiQ5K5I2ZJtbLhZ0kj27tx7bN5Zo36bSwf2KlP7g== + "@nodelib/fs.stat@^1.1.2": version "1.1.3" resolved "https://registry.npm.taobao.org/@nodelib/fs.stat/download/@nodelib/fs.stat-1.1.3.tgz#2b5a3ab3f918cca48a8c754c08168e3f03eba61b" integrity sha1-K1o6s/kYzKSKjHVMCBaOPwPrphs= -"@nondanee/unblockneteasemusic@^0.25.3": - version "0.25.3" - resolved "https://registry.yarnpkg.com/@nondanee/unblockneteasemusic/-/unblockneteasemusic-0.25.3.tgz#72d936f18f46523a2b929b8eaec8dfd2acf25e7e" - integrity sha512-Fgl0iRvuFCNhyFlovbqqnJQRomaFVjMLosS1hRUd/X47y6HeNlQsxa9c8RtmeLwNnZ27rj0DsUZlECqU7FyNPA== - "@npmcli/move-file@^1.0.1": version "1.0.1" resolved "https://registry.npm.taobao.org/@npmcli/move-file/download/@npmcli/move-file-1.0.1.tgz#de103070dac0f48ce49cf6693c23af59c0f70464"