Update peer paste

This commit is contained in:
Raven Scott
2026-05-28 01:55:01 -04:00
parent 95559fc552
commit 25f3195e57
2 changed files with 62 additions and 0 deletions
+36
View File
@@ -140,6 +140,8 @@ function wrapPasteViewDocument({ pasteId, title, chips, toolbar, banner, bodyHtm
}
let cleanupTimer = null;
let recentWatchCb = null;
let recentWatchDebounce = null;
const metrics = {
queryTimeoutFallbacks: 0,
wsMessagesHandled: 0,
@@ -1402,6 +1404,26 @@ async function broadcastRecentPastesUpdate() {
}
}
function setupRecentPastesWatcher() {
if (recentWatchCb) return;
recentWatchCb = () => {
if (recentWatchDebounce) clearTimeout(recentWatchDebounce);
recentWatchDebounce = setTimeout(() => {
recentWatchDebounce = null;
withPeerPasteContext(async () => {
await broadcastRecentPastesUpdate();
}).catch((err) => {
sdk.log.debug('peer.paste', `recent watcher broadcast failed: ${err.message}`);
});
}, 250);
};
try {
sdk.db.watch(recentWatchCb);
} catch (err) {
sdk.log.warn('peer.paste', `Failed to watch DB for recent updates: ${err.message}`);
}
}
function setupWebSocketHandlers() {
if (!sdk.websocket.initialize()) {
sdk.log.warn('peer.paste', 'Failed to initialize WebSocket server');
@@ -1636,6 +1658,8 @@ async function onInit() {
await cleanupExpiredPastes();
cleanupTimer = setInterval(cleanupExpiredPastes, CLEANUP_INTERVAL_MS);
setupWebSocketHandlers();
setupRecentPastesWatcher();
await broadcastRecentPastesUpdate();
sdk.admin.registerSetting('defaultExpiryHours', {
type: 'number',
@@ -1654,6 +1678,18 @@ async function onShutdown() {
clearInterval(cleanupTimer);
cleanupTimer = null;
}
if (recentWatchDebounce) {
clearTimeout(recentWatchDebounce);
recentWatchDebounce = null;
}
if (recentWatchCb) {
try {
sdk.db.unwatch(recentWatchCb);
} catch (err) {
sdk.log.debug('peer.paste', `Failed to unwatch DB: ${err.message}`);
}
recentWatchCb = null;
}
sdk.websocket.close();
sdk.log.info('peer.paste', 'peer.paste shutdown complete');
}
+26
View File
@@ -111,6 +111,7 @@ let hasMoreServer = true;
let isLoadingPage = false;
let wsReconnectAttempts = 0;
let editorMode = 'edit';
let didReceiveInitialSidebarData = false;
function updateCreateButtonLabel() {
const publicPaste = document.getElementById('publicPaste').checked;
@@ -249,9 +250,21 @@ function handleRealtimePage(data, reset = false) {
visibleCount = Math.min(visibleCount + PAGE_SIZE, allRecent.length);
}
hasMoreServer = !!data.hasMore;
didReceiveInitialSidebarData = true;
renderRecentList();
}
async function fetchRecentViaHttp() {
try {
const res = await fetch(`/api/pastes?offset=0&limit=${PAGE_SIZE}`, { credentials: 'same-origin' });
if (!res.ok) return;
const data = await res.json();
handleRealtimePage(data, true);
} catch (_) {
// best-effort fallback only
}
}
function sendWs(payload) {
return new Promise((resolve, reject) => {
if (!ws || ws.readyState !== WebSocket.OPEN) {
@@ -442,6 +455,19 @@ function connectWebSocket() {
ws.addEventListener('open', () => {
wsReconnectAttempts = 0;
document.getElementById('live-dot').textContent = 'LIVE';
didReceiveInitialSidebarData = false;
sendWs({ type: 'request-pastes', offset: 0, limit: PAGE_SIZE })
.then((msg) => {
if (msg && msg.type === 'pastes-page') {
handleRealtimePage(msg.data, true);
}
})
.catch(() => {});
setTimeout(() => {
if (!didReceiveInitialSidebarData) {
fetchRecentViaHttp();
}
}, 1500);
});
ws.addEventListener('close', () => {