减少后台定时器调用

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

View File

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