mastoradio/src/components/App.svelte

92 lines
2.6 KiB
Svelte
Raw Normal View History

2020-02-13 20:15:52 +01:00
<svelte:head>
<title>{`${ $current ? `${$current.metadata.title} ` : ''}Eldritch Radio`}</title>
</svelte:head>
2020-02-15 20:13:33 +01:00
<div class="app container">
<Header></Header>
2020-01-10 03:02:46 +01:00
<section class="viewer">
2020-01-16 20:10:40 +01:00
{#if $current}
<Viewer></Viewer>
2020-01-20 04:49:38 +01:00
<Controls></Controls>
2020-01-16 20:10:40 +01:00
{/if}
2020-01-10 03:02:46 +01:00
</section>
<section class="queue">
2020-01-13 14:39:22 +01:00
<Queue></Queue>
2020-01-10 03:02:46 +01:00
</section>
2020-02-15 20:13:33 +01:00
<Footer></Footer>
</div>
2020-01-10 03:02:46 +01:00
<script>
2020-02-15 20:13:33 +01:00
2020-02-14 16:48:49 +01:00
import { onMount, onDestroy } from 'svelte'
import { get } from 'svelte/store'
2020-02-15 20:13:33 +01:00
import Header from '/components/layout/Header.svelte'
import Footer from '/components/layout/Footer.svelte'
2020-01-10 03:02:46 +01:00
import Controls from '/components/Controls.svelte'
2020-01-13 14:39:22 +01:00
import Queue from '/components/Queue.svelte'
2020-01-10 03:02:46 +01:00
import Viewer from '/components/Viewer.svelte'
2020-02-14 17:49:56 +01:00
import { hashtagIterator, combinedIterator } from '/services/mastodon.js'
2020-02-14 16:48:49 +01:00
import { mkTracksIterator } from '/services/misc.js'
import { domain, hashtags, queue, next, current, enqueueing, select } from '/store.js'
let nextUnsubcribe = null
let currentUnsubcribe = null
onMount(async () => {
2020-02-14 17:49:56 +01:00
// const iterator = mkTracksIterator(hashtagIterator(get(domain), get(hashtags)[0]))
const domainValue = get(domain)
const hashtagsValue = get(hashtags)
const iterator = combinedIterator(
hashtagsValue.map(hashtag => mkTracksIterator(hashtagIterator(domainValue, hashtag)))
)
2020-02-14 16:48:49 +01:00
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
}
})
}
})
})
2020-01-16 20:10:40 +01:00
2020-02-14 16:48:49 +01:00
onDestroy(() => {
for (const unsubcribe of [nextUnsubcribe, currentUnsubcribe]) {
if (unsubcribe) {
unsubcribe()
}
}
})
2020-02-15 20:13:33 +01:00
</script>