Skip to content

Commit

Permalink
refactor: unload before loading
Browse files Browse the repository at this point in the history
  • Loading branch information
CyanSalt committed Jan 19, 2024
1 parent 62a02e9 commit 7db1c46
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions src/renderer/components/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ let music = $ref<string>()
let isConnected = $ref(false)
let connectedInfo = $ref<MusicInfo>()
let connectedArgs = $ref<[any, any]>()
let connectedSong = $ref<any>()
const audio = $ref<HTMLAudioElement>()
Expand Down Expand Up @@ -237,8 +237,8 @@ function connect() {
isConnected = !isConnected
}
async function prepare(args: [any, any], autoplay = false) {
music = await service.prepare?.(args[0], args[1])
async function prepare(song: any, detail: any, autoplay = false) {
music = await service.prepare?.(song, detail)
if (!music) return
if (autoplay) {
currentTime = 0
Expand All @@ -253,7 +253,15 @@ async function prepare(args: [any, any], autoplay = false) {
}
}
function unload() {
info = undefined
data = undefined
music = undefined
connectedSong = undefined
}
async function load(query: string, properties?: MusicInfo) {
unload()
const songs = await service.search(query)
const song = songs.find(item => {
const transformed = service.transform(item)
Expand All @@ -264,11 +272,10 @@ async function load(query: string, properties?: MusicInfo) {
if (!song) return
info = service.transform(song)
data = await service.load(song)
const args: [any, any] = [song, data.detail]
if (properties) {
connectedArgs = args
connectedSong = song
} else {
await prepare(args, true)
await prepare(song, data.detail, true)
}
}
Expand Down Expand Up @@ -308,7 +315,6 @@ function handleEnded() {
function reset() {
isPlaying = false
music = undefined
connectedInfo = undefined
}
Expand All @@ -326,16 +332,17 @@ watchEffect(onInvalidate => {
}
} else {
reset()
unload()
}
}, 1000 * CONNECTING_INTERVAL_TIME)
onInvalidate(() => {
clearInterval(timer)
})
} else {
reset()
if (connectedArgs) {
prepare(connectedArgs)
connectedArgs = undefined
if (connectedSong && data) {
prepare(connectedSong, data.detail)
connectedSong = undefined
}
}
})
Expand Down

0 comments on commit 7db1c46

Please sign in to comment.