forked from Mastodon/mastoradio-la-radio-di-mastodon
wip state refact
This commit is contained in:
parent
d9b4bf98df
commit
cd10193d3e
|
@ -3018,6 +3018,11 @@
|
||||||
"locate-path": "^3.0.0"
|
"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": {
|
"for-in": {
|
||||||
"version": "1.0.2",
|
"version": "1.0.2",
|
||||||
"resolved": "https://registry.npmjs.org/for-in/-/for-in-1.0.2.tgz",
|
"resolved": "https://registry.npmjs.org/for-in/-/for-in-1.0.2.tgz",
|
||||||
|
|
|
@ -21,6 +21,7 @@
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@babel/runtime-corejs3": "^7.7.7",
|
"@babel/runtime-corejs3": "^7.7.7",
|
||||||
"date-fns": "^2.9.0",
|
"date-fns": "^2.9.0",
|
||||||
|
"folktale": "^2.3.2",
|
||||||
"get-urls": "^9.2.0",
|
"get-urls": "^9.2.0",
|
||||||
"iter-tools": "^7.0.0-rc.0",
|
"iter-tools": "^7.0.0-rc.0",
|
||||||
"yt-player": "^3.4.3"
|
"yt-player": "^3.4.3"
|
||||||
|
|
|
@ -30,7 +30,7 @@
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import { paused, volume, muted, entry } from '/store.js'
|
import { paused, volume, muted, entry } from '/stores.js'
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
|
|
|
@ -27,7 +27,7 @@
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import { onMount } from 'svelte'
|
import { onMount } from 'svelte'
|
||||||
import { paused, entry as currentEntry, entries } from '/store.js'
|
import { paused, entry as currentEntry, entries } from '/stores.js'
|
||||||
|
|
||||||
const toggleEntry = (entry) => {
|
const toggleEntry = (entry) => {
|
||||||
if (entry !== $currentEntry) {
|
if (entry !== $currentEntry) {
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
import { onMount, onDestroy } from 'svelte'
|
import { onMount, onDestroy } from 'svelte'
|
||||||
import { get } from 'svelte/store'
|
import { get } from 'svelte/store'
|
||||||
import YoutubePlayer from 'yt-player'
|
import YoutubePlayer from 'yt-player'
|
||||||
import { entry, paused, muted, volume } from '/store.js'
|
import { entry, paused, muted, volume } from '/stores.js'
|
||||||
import { secondsToElapsedTime } from '/util.js'
|
import { secondsToElapsedTime } from '/util.js'
|
||||||
|
|
||||||
let element
|
let element
|
||||||
|
|
|
@ -1,49 +1,69 @@
|
||||||
import { writable, get } from 'svelte/store'
|
import { writable, get } from 'svelte/store'
|
||||||
import * as util from '/util.js'
|
import { mkTracksIterator } from '/util.js'
|
||||||
|
|
||||||
export const domain = writableLocalStorage('domain', 'eldritch.cafe')
|
export const writableLocalStorage = (key, value) => {
|
||||||
|
|
||||||
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)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function writableLocalStorage(key, value) {
|
|
||||||
const item = JSON.parse(localStorage.getItem(key))
|
const item = JSON.parse(localStorage.getItem(key))
|
||||||
const store = writable(item === null ? value : item)
|
const store = writable(item === null ? value : item)
|
||||||
const unsubscribe = store.subscribe(x => localStorage.setItem(key, JSON.stringify(x)))
|
|
||||||
|
store.subscribe(x => localStorage.setItem(key, JSON.stringify(x)))
|
||||||
|
|
||||||
return store
|
return store
|
||||||
}
|
}
|
||||||
|
|
||||||
function entryStore(entries) {
|
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 store = writable(null)
|
||||||
const { set, update, subscribe } = store
|
const { set, update, subscribe } = store
|
||||||
|
|
||||||
const select = (entry) => {
|
const select = (entry) => {
|
||||||
update(() => entry)
|
update(() => entry)
|
||||||
|
|
||||||
const entriesList = get(entries)
|
const entriesList = get(entriesStore)
|
||||||
const index = entriesList.indexOf(entry)
|
const index = entriesList.indexOf(entry)
|
||||||
|
|
||||||
if (index === entriesList.length - 1) {
|
if (index === entriesList.length - 1) {
|
||||||
entries.load(1)
|
entriesStore.load(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const previous = () => {
|
const previous = () => {
|
||||||
const entriesList = get(entries)
|
const entriesList = get(entriesStore)
|
||||||
|
|
||||||
update(currentEntry => {
|
update(currentEntry => {
|
||||||
const index = entriesList.indexOf(currentEntry)
|
const index = entriesList.indexOf(currentEntry)
|
||||||
|
@ -53,7 +73,7 @@ function entryStore(entries) {
|
||||||
}
|
}
|
||||||
|
|
||||||
const next = () => {
|
const next = () => {
|
||||||
const entriesList = get(entries)
|
const entriesList = get(entriesStore)
|
||||||
|
|
||||||
update(oldEntry => {
|
update(oldEntry => {
|
||||||
if (entriesList.length === 0) {
|
if (entriesList.length === 0) {
|
||||||
|
@ -79,8 +99,8 @@ function entryStore(entries) {
|
||||||
return { subscribe, set: select, previous, next }
|
return { subscribe, set: select, previous, next }
|
||||||
}
|
}
|
||||||
|
|
||||||
function entriesStore(domain, hashtags) {
|
export const entriesStore = (domain, hashtags) => {
|
||||||
const tracksIterator = util.mkTracksIterator(domain, hashtags)
|
const tracksIterator = mkTracksIterator(domain, hashtags)
|
||||||
|
|
||||||
const store = writable([])
|
const store = writable([])
|
||||||
const { update, subscribe } = store
|
const { update, subscribe } = store
|
||||||
|
@ -99,4 +119,4 @@ function entriesStore(domain, hashtags) {
|
||||||
}
|
}
|
||||||
|
|
||||||
return { subscribe, load }
|
return { subscribe, load }
|
||||||
}
|
}
|
|
@ -0,0 +1,84 @@
|
||||||
|
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)
|
||||||
|
}
|
|
@ -37,6 +37,7 @@ export async function* mkStatusesIterator(initialLink) {
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
const now = Date.now()
|
const now = Date.now()
|
||||||
|
|
||||||
if (latestPreviousFetch + 5 * minute < now) {
|
if (latestPreviousFetch + 5 * minute < now) {
|
||||||
console.log('fetch newer')
|
console.log('fetch newer')
|
||||||
const previous = await fetchTimeline(previousLink)
|
const previous = await fetchTimeline(previousLink)
|
||||||
|
|
Loading…
Reference in New Issue