- {#each $entries as entry}
-
+ {#each $queue as track, i}
+
-
-
{entry.metadata.title}
+
{track.metadata.title}
- {entry.status.account.username} {entry.status.account.acct}
- {entry.data.url}
+ {track.status.account.username} {track.status.account.acct}
+ {track.data.url}
{/each}
-
entries.load(5)}>LOAD 5 MOAR
+ {#if $enqueueing}
+ LOADING ...
+ {/if}
\ No newline at end of file
diff --git a/src/services/store.js b/src/services/store.js
deleted file mode 100644
index 75edd0e..0000000
--- a/src/services/store.js
+++ /dev/null
@@ -1,122 +0,0 @@
-import { writable, get } from 'svelte/store'
-import { mkTracksIterator } from '/util.js'
-
-export const writableLocalStorage = (key, value) => {
- const item = JSON.parse(localStorage.getItem(key))
- const store = writable(item === null ? value : item)
-
- store.subscribe(x => localStorage.setItem(key, JSON.stringify(x)))
-
- return store
-}
-
-export const stackStore = (domain, hashtags) => {
- const tracksIterator = mkTracksIterator(domain, hashtags)
-
- const store = writable([])
- const { update, subscribe } = store
-
- let promise = Promise.resolve()
- const buffer = []
-
- const load = async () => {
-
-
- const n = 5 - buffer.length
-
- for (let i = 0; i < n; i++) {
- const iteratorResult = await tracksIterator.next()
-
- if (iteratorResult.value) {
- update(entries => [...entries, iteratorResult.value])
- } else {
- // iterator don't have new entries for now
- break
- }
- }
- }
-
- const unshift = async () => {
- let promise = promise.then(() => {
-
- })
- }
-
- promise = load()
-
- return { subscribe, unshift }
-}
-
-export const entryStore = (entriesStore) => {
- const store = writable(null)
- const { set, update, subscribe } = store
-
- const select = (entry) => {
- update(() => entry)
-
- const entriesList = get(entriesStore)
- const index = entriesList.indexOf(entry)
-
- if (index === entriesList.length - 1) {
- entriesStore.load(1)
- }
- }
-
- const previous = () => {
- const entriesList = get(entriesStore)
-
- update(currentEntry => {
- const index = entriesList.indexOf(currentEntry)
-
- return index > 0 ? entriesList[index - 1] : null
- })
- }
-
- const next = () => {
- const entriesList = get(entriesStore)
-
- update(oldEntry => {
- if (entriesList.length === 0) {
- return null
- }
-
- const index = entriesList.indexOf(oldEntry)
-
- if (index === -1) {
- return null
- }
-
- const nextIndex = index + 1
-
- if (nextIndex === entriesList.length - 1) {
- entries.load(1)
- }
-
- return entriesList[nextIndex]
- })
- }
-
- return { subscribe, set: select, previous, next }
-}
-
-export const entriesStore = (domain, hashtags) => {
- const tracksIterator = mkTracksIterator(domain, hashtags)
-
- const store = writable([])
- const { update, subscribe } = store
-
- const load = async (number) => {
- for (let i = 0; i < number; i++) {
- const iteratorResult = await tracksIterator.next()
-
- if (iteratorResult.value) {
- update(entries => [...entries, iteratorResult.value])
- } else {
- // iterator don't have new entries for now
- break
- }
- }
- }
-
- return { subscribe, load }
-}
\ No newline at end of file
diff --git a/src/store.js b/src/store.js
new file mode 100644
index 0000000..c485586
--- /dev/null
+++ b/src/store.js
@@ -0,0 +1,46 @@
+import { writable, derived, get } from 'svelte/store'
+import { writableLocalStorage, mkTracksIterator } from '/util.js'
+
+export const domain = writableLocalStorage('domain', 'eldritch.cafe')
+
+export const hashtags = writableLocalStorage('hashtags', [
+ 'np',
+ 'nowplaying',
+ 'tootradio',
+ 'pouetradio'
+])
+
+export const paused = writable(false)
+export const muted = writableLocalStorage('muted', false)
+export const volume = writableLocalStorage('volume', 100)
+
+export const index = writable(null)
+export const queue = writable([])
+export const enqueueing = writable(false)
+export const current = derived([queue, index], ([$queue, $index]) => $queue[$index])
+
+export const canPrevious = derived([index], ([$index]) => $index !== null && $index > 0)
+export const canNext = derived([index, queue], ([$index, $queue]) => $index !== null && $index < $queue.length - 1)
+
+export const loading = writable(false)
+
+
+
+const tracksIterator = mkTracksIterator(get(domain), get(hashtags))
+
+export const selectPrevious = () => { if (get(canPrevious)) index.update($index => $index - 1) }
+export const selectNext = () => { if (get(canNext)) index.update($index => $index + 1) }
+
+export const enqueue = async () => {
+ if (!get(enqueueing)) {
+ enqueueing.set(true)
+
+ const { value: newTrack } = await tracksIterator.next()
+
+ if (newTrack) {
+ queue.update($queue => [...$queue, newTrack])
+ }
+
+ enqueueing.set(false)
+ }
+}
\ No newline at end of file
diff --git a/src/stores.js b/src/stores.js
deleted file mode 100644
index 986af98..0000000
--- a/src/stores.js
+++ /dev/null
@@ -1,84 +0,0 @@
-import { writable, get } from 'svelte/store'
-import { writableLocalStorage, entriesStore, entryStore } from '/services/store.js'
-
-export const domain = writableLocalStorage('domain', 'eldritch.cafe')
-
-export const hashtags = writableLocalStorage('hashtags', [
- 'np',
- 'nowplaying',
- 'tootradio',
- 'pouetradio'
-])
-
-export const paused = writable(false)
-export const muted = writableLocalStorage('muted', false)
-export const volume = writableLocalStorage('volume', 100)
-
-export const entries = entriesStore(get(domain), get(hashtags))
-export const entry = entryStore(entries)
-
-
-
-
-const tracksIterator = mkTracksIterator(get(domain), get(hashtags))
-
-
-
-export const track = writable(null)
-export const queue = writable([])
-export const stack = writable([])
-
-export const state = writable({
- current: null,
- queue: []
-})
-
-export const enqueue = () => {
- const { value: newTrack } = await tracksIterator.next()
-
- if (!newTrack) {
- state.update(s => ({ ...s, queue: [...s.queue, newTrack] }))
- }
-}
-
-export const select = (track) => {
- state.update(s => ({ ...s, current: track }))
-}
-
-export const selectPrevious = () => {
- state.update(s => {
- if (s.current === null) return s
-
- const
- return
- })
-
- const tracks = get(queue)
-
- track.update(oldTrack => {
- const index = tracks.indexOf(oldTrack)
- return index > 0 ? tracks[index - 1] : null
- })
-}
-
-export const selectNext = () => {
- const tracks = get(queue)
- const oldTrack = get(track)
-
-
-
- track.update(oldTrack => {
- const index = tracks.indexOf(oldTrack)
-
- if (index !== -1 && ) {
- return null
- }
-
- return index !== -1 && index + 1 < tracks.length
- ? tracks[index + 1]
- : null
- })
-
-
- enqueueIfTrack(track)
-}
diff --git a/src/util.js b/src/util.js
index 5640ca3..8bd52c3 100644
--- a/src/util.js
+++ b/src/util.js
@@ -1,6 +1,16 @@
+import { writable } from 'svelte/store'
import getUrls from 'get-urls'
import { execPipe, asyncFilter, asyncMap } from 'iter-tools'
+export const writableLocalStorage = (key, value) => {
+ const item = JSON.parse(localStorage.getItem(key))
+ const store = writable(item === null ? value : item)
+
+ store.subscribe(x => localStorage.setItem(key, JSON.stringify(x)))
+
+ return store
+}
+
const millisecond = 1
const second = 1000 * millisecond
const minute = 60 * second