Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Periodically check for newer data in background #48

Merged
merged 1 commit into from
Dec 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions src/renderer/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ const POEPALETTE_MINISEARCH = new MiniSearch({
storeFields: ['display_text', 'wiki_url', 'poedb_url', 'ninja_url', 'trade_url', 'tft_url', 'tool_url'],
})

const init_minisearch = async (league) => {
let currentLeague
let dataTimestamp

const initMinisearch = async (league) => {
currentLeague = league

const url = `https://d2zmd9bpiyaqin.cloudfront.net/data-${league}.json`
console.log(`loading search data from ${url}`)

Expand All @@ -27,6 +32,31 @@ const init_minisearch = async (league) => {
POEPALETTE_MINISEARCH.addAll(content.data)
console.timeEnd('search index building')
console.log(`search index size: ${POEPALETTE_MINISEARCH.termCount}`)

dataTimestamp = Date.parse(response.headers.get('last-modified'))
}

window.electronAPI.onLeagueChanged((event, league) => initMinisearch(league))

const checkForDataUpdates = async () => {
const url = `https://d2zmd9bpiyaqin.cloudfront.net/data-${currentLeague}.json`

const response = await fetch(url, { method: 'HEAD' })
if (response.status !== 200) {
window.electronAPI.panic(
`Failed to check for data updates at ${url} (HTTP status ${response.status})`,
)
return
}

const lastModified = Date.parse(response.headers.get('last-modified'))

if (lastModified > dataTimestamp) {
console.log(`New data available at ${url}, reinitializing search index`)
initMinisearch(currentLeague)
} else {
console.log(`No new data available at ${url}`)
}
}

window.electronAPI.onLeagueChanged((event, league) => init_minisearch(league))
setInterval(checkForDataUpdates, 15 * 60 * 1000) // check every 15 minutes