Add resolver, server, PDF rendering and web app
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
6a7fcdc4b8
commit
bdfdf581f3
59 files changed
+4088
No files matched your search
@@ -0,0 +1,42 @@
|
||||
import { useEffect, useState } from 'preact/hooks';
|
||||
import type { Book } from '../shared/types.ts';
|
||||
import { api } from './api.ts';
|
||||
import { emptyState, reduce, shelfOrder } from './store.ts';
|
||||
import type { BookAction, BookState } from './store.ts';
|
||||
|
||||
let state: BookState = emptyState;
|
||||
const listeners = new Set<() => void>();
|
||||
|
||||
export function dispatch(action: BookAction): void {
|
||||
state = reduce(state, action);
|
||||
listeners.forEach(l => l());
|
||||
}
|
||||
export const upsertBook = (book: Book) => dispatch({ type: 'upsert', book });
|
||||
|
||||
export function useBooks(): Book[] {
|
||||
const [, force] = useState(0);
|
||||
useEffect(() => {
|
||||
const l = () => force(n => n + 1);
|
||||
listeners.add(l);
|
||||
return () => { listeners.delete(l); };
|
||||
}, []);
|
||||
return shelfOrder(state);
|
||||
}
|
||||
|
||||
export function getBook(id: number): Book | undefined { return state.byId[id]; }
|
||||
|
||||
async function refetch(): Promise<void> {
|
||||
try { dispatch({ type: 'replace', books: await api.listBooks('all') }); } catch { /* retried on next open */ }
|
||||
}
|
||||
|
||||
/** One EventSource for the whole app. Every (re)connect refetches the full list. */
|
||||
export function startLive(): void {
|
||||
const es = new EventSource('/api/events');
|
||||
es.onopen = () => { void refetch(); };
|
||||
es.addEventListener('book', e => {
|
||||
try { upsertBook(JSON.parse((e as MessageEvent).data) as Book); } catch { /* ignore */ }
|
||||
});
|
||||
es.addEventListener('deleted', e => {
|
||||
try { dispatch({ type: 'delete', id: (JSON.parse((e as MessageEvent).data) as { id: number }).id }); } catch { /* ignore */ }
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user