new: added live update for posts

This commit is contained in:
kapypara 2023-12-16 19:52:51 +03:00
parent 450512cf80
commit 2d4c60d3d1
1 changed files with 104 additions and 25 deletions

View File

@ -10,7 +10,7 @@ let currentItem = -1
// for newest listing
let newMode = params.get('order') == 'new'
let floorItem // for the later new items
let endItem // for the later new items
let maxItem
// for the api requests
@ -35,20 +35,13 @@ async function fetchStories() {
req.json().then(resolve => {
items = resolve
currentItem = 0
loadItems()
})
}
async function fetchMaxItem() {
const req = await fetch('https://hacker-news.firebaseio.com/v0/maxitem.json')
req.json().then(resolve => {
maxItem = resolve
floorItem = resolve
loadItems()
})
return req.json()
}
function loadItems() {
@ -358,25 +351,25 @@ async function loadCommnets(ids, target) {
}
}
if (!isQuery) {
newMode ? fetchMaxItem() : fetchStories()
async function loadPage() {
if (isQuery) {
return itemPage()
}
if (newMode) {
maxItem = await fetchMaxItem()
endItem = maxItem
} else {
await fetchStories()
}
loadItems()
document.addEventListener('scroll', loadItems)
} else {
itemPage()
}
// ================ new items section stuff ===================
let permission = 'denied'
let notificationBtn = document.getElementById('notification-btn')
notificationBtn.addEventListener("click", updatePermission)
function updatePermission() {
Notification.requestPermission().then(result => {
permission = result
notificationBtn.disabled = permission == 'denied'
})
}
updatePermission()
loadPage()
let switchBtn = document.getElementById('switch-btn')
if (newMode) {
@ -392,3 +385,89 @@ switchBtn.addEventListener("click", () => {
}
})
// ================ new items section stuff ===================
let permission = 'denied'
let notificationBtn = document.getElementById('notification-btn')
notificationBtn.addEventListener("click", updatePermission)
function updatePermission() {
if (!newMode) {
notificationBtn.style.visibility = 'collapse'
return
}
Notification.requestPermission().then(result => {
permission = result
notificationBtn.disabled = permission == 'denied'
})
}
updatePermission()
async function notify(count) {
if (count == 0) {
return
}
const n = new Notification("Clone Wars", { body: `${count} new Post`})
document.addEventListener("visibilitychange", () => {
if (document.visibilityState == "visible") {
n.close()
}
})
}
async function loadFresh() {
let newMax = endItem
try {
newMax = await fetchMaxItem()
} catch (e) {
console.log(`[loadFresh] got error ${e}`)
}
const newEnd = newMax
if (newMax == endItem) {
return
}
console.log(`[loadFresh]: new items ${newMax - endItem}`)
let count = 0
let intervalId = setInterval(async () => {
if (newMax == endItem) {
endItem = newEnd
notify(count)
return clearInterval(intervalId)
}
try {
let item = await getItem(newMax--)
if (item) {
const type = item.type
if (type == itemType.job || type == itemType.poll || type == itemType.story) {
makeItemListing(item)
count++
}
}
} catch (e) {
console.log(`[loadFresh] got error ${e}`)
}
}, timer)
}
if (newMode) {
setTimeout(setInterval, 5e3, loadFresh, 5e3)
}