refact store again
This commit is contained in:
@ -21,11 +21,65 @@
|
||||
|
||||
|
||||
<script>
|
||||
import { onMount, onDestroy } from 'svelte'
|
||||
import { get } from 'svelte/store'
|
||||
|
||||
import Controls from '/components/Controls.svelte'
|
||||
import Queue from '/components/Queue.svelte'
|
||||
import Viewer from '/components/Viewer.svelte'
|
||||
import { hashtagIterator } from '/services/mastodon.js'
|
||||
import { mkTracksIterator } from '/services/misc.js'
|
||||
|
||||
import { current } from '/store.js'
|
||||
import { domain, hashtags, queue, next, current, enqueueing, select } from '/store.js'
|
||||
|
||||
let nextUnsubcribe = null
|
||||
let currentUnsubcribe = null
|
||||
|
||||
onMount(async () => {
|
||||
const iterator = mkTracksIterator(hashtagIterator(get(domain), get(hashtags)[0]))
|
||||
|
||||
const { value: first } = await iterator.next()
|
||||
|
||||
queue.set([first])
|
||||
select(first)
|
||||
|
||||
nextUnsubcribe = next.subscribe(async nextValue => {
|
||||
if (nextValue === null) {
|
||||
if (!get(enqueueing)) {
|
||||
enqueueing.set(true)
|
||||
|
||||
const { value: newTrack } = await iterator.next()
|
||||
|
||||
if (newTrack) {
|
||||
queue.update(queueValue => [...queueValue, newTrack])
|
||||
next.set(newTrack)
|
||||
}
|
||||
|
||||
enqueueing.set(false)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
currentUnsubcribe = current.subscribe(currentValue => {
|
||||
if (currentValue !== null) {
|
||||
next.update(nextValue => {
|
||||
if (nextValue === currentValue) {
|
||||
return null
|
||||
} else {
|
||||
return nextValue
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
onDestroy(() => {
|
||||
for (const unsubcribe of [nextUnsubcribe, currentUnsubcribe]) {
|
||||
if (unsubcribe) {
|
||||
unsubcribe()
|
||||
}
|
||||
}
|
||||
})
|
||||
</script>
|
||||
|
||||
<style>
|
||||
|
Reference in New Issue
Block a user