A clean and minimal youtube frontend, without all the ads and whistles
JavaScript
refactor: introduce state factory
Christian Fei944256760e25
diff --git a/server/http.js b/server/http.jsindex aaad40d..3eba5d3 100644--- a/server/http.js+++ b/server/http.js@@ -5,28 +5,12 @@ import { updateAndPersistVideos } from '../lib/update-videos.js'import apiHandler from './router/api.js'import appHandler from './router/app.js'import Repository from '../lib/repository.js'+import stateFactory from './state-factory.js'export function createServer (repo = new Repository()) {- const stateChangeHandler = {- get (target, key) {- if (typeof target[key] === 'object' && target[key] !== null) {- return new Proxy(target[key], stateChangeHandler)- }- return target[key]- },- set (target, prop, value) {- if (target[prop] !== value) broadcastSSE(JSON.stringify({ type: 'state', state: target }), connections)- target[prop] = value- return true- }- }-- const state = new Proxy({- downloading: {},- summarizing: {}- }, stateChangeHandler)-const connections = []+ const state = stateFactory((updatedState) =>+ broadcastSSE(JSON.stringify({ type: 'state', state: updatedState }), connections))let lastAdded = Date.now()runUpdateVideos(repo, connections)diff --git a/server/state-factory.js b/server/state-factory.jsnew file mode 100644index 0000000..d768329--- /dev/null+++ b/server/state-factory.js@@ -0,0 +1,20 @@+export default function stateFactory (changeCallback = () => {}) {+ const stateChangeHandler = {+ get (target, key) {+ if (typeof target[key] === 'object' && target[key] !== null) {+ return new Proxy(target[key], stateChangeHandler)+ }+ return target[key]+ },+ set (target, prop, value) {+ target[prop] = value+ if (target[prop] !== value) changeCallback(target)+ return true+ }+ }++ return new Proxy({+ downloading: {},+ summarizing: {}+ }, stateChangeHandler)+}