Implement issues for v1.1.0 #20

Merged
phlaym merged 24 commits from v1.1.0 into main 2023-04-07 15:07:41 +00:00
Showing only changes of commit 2409fa2b8d - Show all commits

View File

@ -8,17 +8,17 @@ import LoadMoreComponent from '$lib/components/LoadMoreComponent.svelte';
export let data: PageData; export let data: PageData;
const refreshInterval = parseInt(PUBLIC_REFRESH_INTERVAL);
let interval: NodeJS.Timer | null = null; let interval: NodeJS.Timer | null = null;
let moreOlderPostsAvailable = true; let moreOlderPostsAvailable = true;
let loadingOlderPosts = false; let loadingOlderPosts = false;
onMount(async () => { function refresh() {
interval = setInterval(async () => {
const params = new URLSearchParams(); const params = new URLSearchParams();
if (data.posts.length > 0) { if (data.posts.length > 0) {
params.set('since', data.posts[0].created_at); params.set('since', data.posts[0].created_at);
} }
await fetch(`/api/posts?${params}`) fetch(`/api/posts?${params}`)
.then(r => r.json()) .then(r => r.json())
.then((resp: Post[]) => { .then((resp: Post[]) => {
if (resp.length > 0) { if (resp.length > 0) {
@ -35,7 +35,21 @@ onMount(async () => {
// TODO: Show error in UI // TODO: Show error in UI
console.error('Error loading newest posts', e); console.error('Error loading newest posts', e);
}); });
}, parseInt(PUBLIC_REFRESH_INTERVAL)); }
onMount(async () => {
interval = setInterval(refresh, refreshInterval);
// - If the page is hidden, slow down refresh rate
// - If the page is shown, bump up refresh rate
document.addEventListener('visibilitychange', () => {
const delay = document.hidden ? refreshInterval * 10 : refreshInterval;
if (interval) {
clearInterval(interval);
}
interval = setInterval(refresh, delay);
});
return () => { return () => {
if (interval !== null) { if (interval !== null) {
clearInterval(interval) clearInterval(interval)