diff --git a/package-lock.json b/package-lock.json index 26bb1e9..9f16a98 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3018,11 +3018,6 @@ "locate-path": "^3.0.0" } }, - "folktale": { - "version": "2.3.2", - "resolved": "https://registry.npmjs.org/folktale/-/folktale-2.3.2.tgz", - "integrity": "sha512-+8GbtQBwEqutP0v3uajDDoN64K2ehmHd0cjlghhxh0WpcfPzAIjPA03e1VvHlxL02FVGR0A6lwXsNQKn3H1RNQ==" - }, "for-in": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/for-in/-/for-in-1.0.2.tgz", diff --git a/package.json b/package.json index a008b50..e20d5b4 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,6 @@ "dependencies": { "@babel/runtime-corejs3": "^7.7.7", "date-fns": "^2.9.0", - "folktale": "^2.3.2", "get-urls": "^9.2.0", "iter-tools": "^7.0.0-rc.0", "yt-player": "^3.4.3" diff --git a/src/components/App.svelte b/src/components/App.svelte index bd23834..a32ef6b 100644 --- a/src/components/App.svelte +++ b/src/components/App.svelte @@ -4,7 +4,9 @@
- + {#if $current} + + {/if}
@@ -23,6 +25,8 @@ import Controls from '/components/Controls.svelte' import Queue from '/components/Queue.svelte' import Viewer from '/components/Viewer.svelte' + + import { current } from '/store.js' \ No newline at end of file diff --git a/src/components/Queue.svelte b/src/components/Queue.svelte index e133d9e..9c98848 100644 --- a/src/components/Queue.svelte +++ b/src/components/Queue.svelte @@ -1,53 +1,58 @@
- {#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} - + {#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