减少后台定时器调用

This commit is contained in:
lyswhut 2023-03-21 17:15:01 +08:00
parent fe78fc251a
commit 0c92958485
2 changed files with 27 additions and 20 deletions

View File

@ -1,20 +1,20 @@
import { updateListMusics } from '@/core/list' import { updateListMusics } from '@/core/list'
import { setMaxplayTime, setNowPlayTime } from '@/core/player/progress' import { setMaxplayTime, setNowPlayTime } from '@/core/player/progress'
import { setCurrentTime, getDuration, getPosition } from '@/plugins/player' import { setCurrentTime, getDuration, getPosition } from '@/plugins/player'
import { formatPlayTime2 } from '@/utils/common' import { formatPlayTime2, throttle } from '@/utils/common'
import { savePlayInfo } from '@/utils/data' import { savePlayInfo } from '@/utils/data'
import { throttleBackgroundTimer } from '@/utils/tools' // import { throttleBackgroundTimer } from '@/utils/tools'
import BackgroundTimer from 'react-native-background-timer' // import BackgroundTimer from 'react-native-background-timer'
import playerState from '@/store/player/state' import playerState from '@/store/player/state'
import settingState from '@/store/setting/state' import settingState from '@/store/setting/state'
const delaySavePlayInfo = throttleBackgroundTimer(savePlayInfo, 2000) const delaySavePlayInfo = throttle(savePlayInfo, 2000)
export default () => { export default () => {
// const updateMusicInfo = useCommit('list', 'updateMusicInfo') // const updateMusicInfo = useCommit('list', 'updateMusicInfo')
let updateTimeout: number | null = null let updateTimeout: NodeJS.Timer | null = null
const getCurrentTime = () => { const getCurrentTime = () => {
void getPosition().then(position => { void getPosition().then(position => {
@ -51,12 +51,12 @@ export default () => {
const clearUpdateTimeout = () => { const clearUpdateTimeout = () => {
if (!updateTimeout) return if (!updateTimeout) return
BackgroundTimer.clearInterval(updateTimeout) clearInterval(updateTimeout)
updateTimeout = null updateTimeout = null
} }
const startUpdateTimeout = () => { const startUpdateTimeout = () => {
clearUpdateTimeout() clearUpdateTimeout()
updateTimeout = BackgroundTimer.setInterval(() => { updateTimeout = setInterval(() => {
getCurrentTime() getCurrentTime()
}, 1000) }, 1000)
getCurrentTime() getCurrentTime()

View File

@ -7,7 +7,9 @@ import settingState from '@/store/setting/state'
type Hook = (time: number, isPlayedStop: boolean) => void type Hook = (time: number, isPlayedStop: boolean) => void
const timeoutTools = { const timeoutTools = {
timeout: null as number | null, bgTimeout: null as number | null,
timeout: null as NodeJS.Timer | null,
startTime: 0,
time: -1, time: -1,
timeHooks: [] as Hook[], timeHooks: [] as Hook[],
exit() { exit() {
@ -18,14 +20,20 @@ const timeoutTools = {
exitApp() exitApp()
} }
}, },
getTime() {
return Math.max(this.time - Math.round((performance.now() - this.startTime) / 1000), -1)
},
callHooks() { callHooks() {
const time = this.getTime()
for (const hook of this.timeHooks) { for (const hook of this.timeHooks) {
hook(this.time, global.lx.isPlayedStop) hook(time, global.lx.isPlayedStop)
} }
}, },
clearTimeout() { clearTimeout() {
if (!this.timeout) return if (!this.bgTimeout) return
BackgroundTimer.clearInterval(this.timeout) BackgroundTimer.clearTimeout(this.bgTimeout)
clearInterval(this.timeout as NodeJS.Timer)
this.bgTimeout = null
this.timeout = null this.timeout = null
this.time = -1 this.time = -1
this.callHooks() this.callHooks()
@ -33,19 +41,18 @@ const timeoutTools = {
start(time: number) { start(time: number) {
this.clearTimeout() this.clearTimeout()
this.time = time this.time = time
this.timeout = BackgroundTimer.setInterval(() => { this.startTime = performance.now()
if (this.time > 0) { this.bgTimeout = BackgroundTimer.setTimeout(() => {
this.time--
this.callHooks()
} else {
this.clearTimeout() this.clearTimeout()
this.exit() this.exit()
} }, time * 1000)
this.timeout = setInterval(() => {
this.callHooks()
}, 1000) }, 1000)
}, },
addTimeHook(hook: Hook) { addTimeHook(hook: Hook) {
this.timeHooks.push(hook) this.timeHooks.push(hook)
hook(this.time, global.lx.isPlayedStop) hook(this.getTime(), global.lx.isPlayedStop)
}, },
removeTimeHook(hook: Hook) { removeTimeHook(hook: Hook) {
this.timeHooks.splice(this.timeHooks.indexOf(hook), 1) this.timeHooks.splice(this.timeHooks.indexOf(hook), 1)