fix back handler dying on unsub

also remove need for unsub
This commit is contained in:
austinried 2021-08-10 11:36:43 +09:00
parent df3e913125
commit 4ac1f3d931

View File

@ -21,30 +21,22 @@ const setCurrentTrackIdx = (idx?: number) => {
})
}
module.exports = async function () {
const unsubs: (() => void)[] = []
unsubs.push(
useStore.subscribe(
(currentTrack?: TrackExt) => {
if (currentTrack) {
useStore.getState().scrobbleTrack(currentTrack.id)
}
},
state => state.currentTrack,
),
let serviceCreated = false
const createService = async () => {
useStore.subscribe(
(currentTrack?: TrackExt) => {
if (currentTrack) {
useStore.getState().scrobbleTrack(currentTrack.id)
}
},
state => state.currentTrack,
)
const { emitter } = TrackPlayer.addEventListener(Event.RemoteStop, () => {
for (const unsub of unsubs) {
unsub()
}
TrackPlayer.addEventListener(Event.RemoteStop, () => {
reset()
trackPlayerCommands.enqueue(TrackPlayer.destroy)
})
unsubs.push(() => emitter.removeAllListeners())
TrackPlayer.addEventListener(Event.RemotePlay, () => trackPlayerCommands.enqueue(TrackPlayer.play))
TrackPlayer.addEventListener(Event.RemotePause, () => trackPlayerCommands.enqueue(TrackPlayer.pause))
@ -91,3 +83,10 @@ module.exports = async function () {
setCurrentTrackIdx(useStore.getState().currentTrackIdx)
})
}
module.exports = async function () {
if (!serviceCreated) {
createService()
serviceCreated = true
}
}