A clean and minimal youtube frontend, without all the ads and whistles
JavaScript
refactor: introduce state wip
Christian Fei5ec393b8e83c
diff --git a/index.js b/index.jsindex 4f0d2da..dd6315f 100644--- a/index.js+++ b/index.js@@ -5,8 +5,9 @@ import { createServer } from './lib/server.js'async function main ({port = 3000, connections = []} = {}) {const repo = new Repository()+ const state = {}- createServer({repo, port, connections})+ createServer({repo, state, port, connections}).listen(port, () => {console.log(`Server running at http://${os.hostname()}:${port} (or http://localhost:${port})`)})diff --git a/lib/server.js b/lib/server.jsindex 091f074..bc6f580 100644--- a/lib/server.js+++ b/lib/server.js@@ -7,12 +7,12 @@ import { handleSSE, broadcastSSE } from './sse.js'import { downloadVideo, extractIdFromUrl, isUnsupportedUrl, isYouTubeUrl } from './youtube.js'import { updateAndPersistVideos, updateAndPersistVideosForChannel } from './update-videos.js'-export function createServer ({repo, connections = [], updateVideos = true} = {}) {+export function createServer ({repo, state = {}, connections = [], updateVideos = true} = {}) {if (updateVideos) {let lastAdded = Date.now()- runUpdateVideos()- setInterval(runUpdateVideos, 1000 * 60 * 10)- function runUpdateVideos() {+ runUpdateVideos(repo, connections)+ setInterval(runUpdateVideos, 1000 * 60 * 10, repo, connections)+ function runUpdateVideos(repo, connections) {console.log('update videos')updateAndPersistVideos(repo, ({name, videos}) => {const newVideos = videos.filter(v => v.addedAt > lastAdded)@@ -36,7 +36,6 @@ export function createServer ({repo, connections = [], updateVideos = true} = {}try {const url = new URL(req.url, `http://${req.headers.host}`)- /* initialize SSE */if (req.headers.accept && req.headers.accept.indexOf('text/event-stream') >= 0) return handleSSE(res, connections)/* api */