Improved version checker

This commit is contained in:
Eduard Urbach 2018-04-08 13:51:12 +02:00
parent d6ca29b5c8
commit 12b0cb00b2
2 changed files with 24 additions and 8 deletions

View File

@ -11,7 +11,7 @@ import InfiniteScroller from "./InfiniteScroller"
import ServiceWorkerManager from "./ServiceWorkerManager" import ServiceWorkerManager from "./ServiceWorkerManager"
import { displayAiringDate, displayDate, displayTime } from "./DateView" import { displayAiringDate, displayDate, displayTime } from "./DateView"
import { findAll, canUseWebP, requestIdleCallback, swapElements, delay } from "./Utils" import { findAll, canUseWebP, requestIdleCallback, swapElements, delay } from "./Utils"
import { checkNewVersion } from "./NewVersionCheck" import { checkNewVersionDelayed } from "./NewVersionCheck"
import * as actions from "./Actions" import * as actions from "./Actions"
export default class AnimeNotifier { export default class AnimeNotifier {
@ -197,8 +197,8 @@ export default class AnimeNotifier {
window.addEventListener("beforeunload", this.onBeforeUnload.bind(this)) window.addEventListener("beforeunload", this.onBeforeUnload.bind(this))
// Periodically check etags of scripts and styles to let the user know about page updates // Periodically check etags of scripts and styles to let the user know about page updates
checkNewVersion("/scripts", this.statusMessage) checkNewVersionDelayed("/scripts", this.statusMessage)
checkNewVersion("/styles", this.statusMessage) checkNewVersionDelayed("/styles", this.statusMessage)
// // Download popular anime titles for the search // // Download popular anime titles for the search
// let response = await fetch("/api/popular/anime/titles/500") // let response = await fetch("/api/popular/anime/titles/500")

View File

@ -5,16 +5,29 @@ let etags = new Map<string, string>()
let hasNewVersion = false let hasNewVersion = false
let newVersionCheckDelay = 60000 let newVersionCheckDelay = 60000
export async function checkNewVersion(url: string, statusMessage: StatusMessage) { async function checkNewVersion(url: string, statusMessage: StatusMessage) {
if(hasNewVersion) { if(hasNewVersion) {
return return
} }
try { try {
let response = await fetch(url) let headers = {}
if(etags.has(url)) {
headers["If-None-Match"] = etags.get(url)
}
let response = await fetch(url, {
headers
})
// Not modified response
if(response.status === 304) {
return
}
if(!response.ok) { if(!response.ok) {
console.warn("Error fetching", url) console.warn("Error fetching", url, response.status)
return return
} }
@ -34,8 +47,11 @@ export async function checkNewVersion(url: string, statusMessage: StatusMessage)
} }
} catch(err) { } catch(err) {
console.warn("Error fetching", url + "\n", err) console.warn("Error fetching", url + "\n", err)
} finally {
checkNewVersionDelayed(url, statusMessage)
} }
delay(newVersionCheckDelay).then(() => checkNewVersion(url, statusMessage))
} }
export async function checkNewVersionDelayed(url: string, statusMessage: StatusMessage) {
delay(newVersionCheckDelay).then(() => checkNewVersion(url, statusMessage))
}