mirror of
https://github.com/ikun0014/lx-music-mobile.git
synced 2025-07-04 03:12:09 +08:00
减少后台定时器调用
This commit is contained in:
parent
fe78fc251a
commit
0c92958485
@ -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()
|
||||||
|
@ -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.clearTimeout()
|
||||||
this.callHooks()
|
this.exit()
|
||||||
} else {
|
}, time * 1000)
|
||||||
this.clearTimeout()
|
this.timeout = setInterval(() => {
|
||||||
this.exit()
|
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)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user