add local storage store

This commit is contained in:
wryk 2020-01-10 03:42:02 +01:00
parent 7e4038386e
commit 806cb8f242
3 changed files with 19 additions and 10 deletions

View File

@ -6,15 +6,15 @@
import { onMount } from 'svelte' import { onMount } from 'svelte'
import { get } from 'svelte/store' import { get } from 'svelte/store'
import YoutubePlayer from 'yt-player' import YoutubePlayer from 'yt-player'
import { entry, playing, volume, muted } from '/store.js' import { entry, playing, muted, volume } from '/store.js'
let element let element
let player let player
$: updateEntry($entry) $: updateEntry($entry)
$: updatePlaying($playing) $: updatePlaying($playing)
$: updateVolume($volume)
$: updateMuted($muted) $: updateMuted($muted)
$: updateVolume($volume)
const updateEntry = (entry) => { const updateEntry = (entry) => {
if (player && entry) player.load(entry.id, get(playing)) if (player && entry) player.load(entry.id, get(playing))
@ -24,14 +24,14 @@
if (player) playing ? player.play() : player.pause() if (player) playing ? player.play() : player.pause()
} }
const updateVolume = (volume) => {
if (player) player.setVolume(volume)
}
const updateMuted = (muted) => { const updateMuted = (muted) => {
if (player) muted ? player.mute() : player.unMute() if (player) muted ? player.mute() : player.unMute()
} }
const updateVolume = (volume) => {
if (player) player.setVolume(volume)
}
onMount(() => { onMount(() => {
player = new YoutubePlayer(element, { player = new YoutubePlayer(element, {
width: 300, width: 300,
@ -44,6 +44,10 @@
related: false related: false
}) })
updatePlaying($playing)
updateMuted($muted)
updateVolume($volume)
player.on('ended', () => entry.next()) player.on('ended', () => entry.next())
player.on('unplayable', () => entry.next()) player.on('unplayable', () => entry.next())
}) })

View File

@ -11,8 +11,8 @@ export const hashtags = writable([
]) ])
export const playing = writable(true) export const playing = writable(true)
export const muted = writable(false) export const muted = writableLocalStorage('muted', false)
export const volume = writable(100) export const volume = writableLocalStorage('volume', 100)
export const entries = entriesStore() export const entries = entriesStore()
export const entry = entryStore(entries) export const entry = entryStore(entries)
@ -20,6 +20,12 @@ export const entry = entryStore(entries)
function writableLocalStorage(key, value) {
const item = JSON.parse(localStorage.getItem(key))
const store = writable(item === null ? value : item)
const unsubscribe = store.subscribe(x => localStorage.setItem(key, x))
return store
}
function entryStore(entries) { function entryStore(entries) {
const store = writable(null) const store = writable(null)

View File

@ -31,8 +31,7 @@ export const statusesToEntries = pipe(
const metadata = await fetchYoutubeMetadata(id) const metadata = await fetchYoutubeMetadata(id)
return { status, url, id, tags, metadata } return { status, url, id, tags, metadata }
}), })
asyncTake(20)
) )
function fetchYoutubeMetadata(id) { function fetchYoutubeMetadata(id) {