Skip to content

Commit

Permalink
refactor: 优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
sigoden committed Nov 8, 2021
1 parent d57ff98 commit 67b5e92
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 23 deletions.
35 changes: 15 additions & 20 deletions src/background/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,8 @@ import {
const playlistDetailStore = {}
// 缓存歌单内歌曲
const songsMapStore = {}
/**
* 播放器
* @type HTMLAudioElement
*/
let audio
// 播放器
const audio = new Audio()
// 播放状态
let audioState = { ...EMPTY_AUDIO_STATE, volumeMute: null }
// 持久化缓存信息
Expand All @@ -43,6 +40,17 @@ let pausedAt = null
const store = proxy({ ...COMMON_PROPS, dir: 1, chinaIp: null })

export async function bootstrap () {
setInterval(async () => {
await refreshLogin()
if (Date.now() - refreshAt > 13 * 60 * 60 * 1000) {
await refreshStore()
}
}, 33 * 60 * 1000)

api.code301 = reset

setupAudio()

await persistLoad()
await refreshStore()
await detectOversea()
Expand Down Expand Up @@ -552,7 +560,6 @@ async function detectOversea () {
}

function setupAudio () {
globalThis.audio = audio
audio.volume = store.volume
audio.onprogress = () => {
if (audio.buffered.length) {
Expand Down Expand Up @@ -582,12 +589,7 @@ function setupAudio () {
}
}
async function updateAudioSrc (src, playing) {
if (audio) {
audio.src = src
} else {
audio = new Audio(src)
setupAudio()
}
audio.src = src
if (playing) {
audio.autoplay = true
} else {
Expand Down Expand Up @@ -628,15 +630,8 @@ function tracksToSongsMap (tracks) {
}, {})
return songsMap
}
setInterval(async () => {
await refreshLogin()
if (Date.now() - refreshAt > 13 * 60 * 60 * 1000) {
await refreshStore()
}
}, 33 * 60 * 1000)

api.code301 = reset

globalThis.audio = audio
globalThis.store = store
globalThis.songsMapStore = songsMapStore
globalThis.playlistDetailStore = playlistDetailStore
Expand Down
7 changes: 4 additions & 3 deletions src/popup/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,8 @@ function doAction (action, params = []) {
logger.debug(action + '.req', params)
return new Promise((resolve, reject) => {
chrome.runtime.sendMessage({ action, params }, response => {
logger.debug(action + '.res', response)
if (action === 'popupInit') {
logger.debug('store', store)
if (action !== 'loadSongsMap') {
logger.debug(action + '.res', response)
}
if (!response.isErr) {
Object.assign(store, response)
Expand Down Expand Up @@ -125,4 +124,6 @@ chrome.runtime.onMessage.addListener((request) => {
}
})

globalThis.store = store

export default store

0 comments on commit 67b5e92

Please sign in to comment.