Skip to content

Commit

Permalink
Use dict_status to display state
Browse files Browse the repository at this point in the history
  • Loading branch information
haukex committed Sep 22, 2024
1 parent b03ca10 commit 70597ba
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
24 changes: 18 additions & 6 deletions src/js/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ window.addEventListener('DOMContentLoaded', async () => {

// variables to keep state
let dictLinesLen = 0
let dictWasUpdated = false
let timerId :number
const searchCache = new LRUCache<string, [string, string[]]>(10)

Expand All @@ -99,6 +100,17 @@ window.addEventListener('DOMContentLoaded', async () => {
// note the click handler must check the state too and ignore clicks when not Ready
rand_entry_link.classList.add('busy-link')
}
if ( newState === MainState.Ready )
dict_status.innerText = `Dictionary holds ${dictLinesLen} entries` + ( dictWasUpdated ? '(updated in background).' : '.' )
else if ( newState === MainState.AwaitingDict )
dict_status.innerText = 'The dictionary is loading, please wait...'
else if ( newState === MainState.Searching )
dict_status.innerText = `Searching ${dictLinesLen} entries, please wait...`
else if ( newState === MainState.Error )
dict_status.innerText = state === MainState.AwaitingDict ? 'Dictionary load failure! See error message above.'
: state === MainState.Searching ? 'Search timed out! See error message above.' : 'See error message above.'
else if ( newState === MainState.Init )
dict_status.innerText = 'Initializing, please wait...'
// if transitioning to error state, make sure corresponding messages are shown
// (though the code below should already be doing this, just play it safe)
if ( newState === MainState.Error ) {
Expand Down Expand Up @@ -267,7 +279,7 @@ window.addEventListener('DOMContentLoaded', async () => {
const cached = searchCache.get(what)
if ( cached !== undefined ) {
const [cachedWhatPat, cachedMatches] = cached
console.debug(`Search for /${cachedWhatPat}/gi had ${cachedMatches.length} results in cache.`)
console.log(`Search for /${cachedWhatPat}/gi had ${cachedMatches.length} results in cache.`)
gotSearchResults(what, cachedWhatPat, cachedMatches)
return
}
Expand Down Expand Up @@ -308,7 +320,6 @@ window.addEventListener('DOMContentLoaded', async () => {
const dictLoadFail = (message :string) => {
error_log.innerText = navigator.userAgent + '\n' + GIT_ID + '\n' + message
dict_load_fail.classList.remove('d-none')
dict_status.innerText = 'Dictionary load failure! See error message above.'
updateState(MainState.Error)
}

Expand All @@ -327,15 +338,14 @@ window.addEventListener('DOMContentLoaded', async () => {
// ---------------[ LoadingDict ]---------------
if (event.data.state === WorkerState.LoadingDict) {
if ( state === MainState.AwaitingDict ) {
dict_status.innerText = 'The dictionary is loading, please wait...'
updateState(state) // force update of the dictionary status message
// We now know the dictionary is loading, and we don't know how often progress will be reported,
// so we can't set a new timeout here.
} else console.warn(`Unexpected worker state ${WorkerState[event.data.state]} in state ${MainState[state]}`)
}
// ---------------[ Ready ]---------------
else if (event.data.state === WorkerState.Ready) {
if ( state === MainState.AwaitingDict ) {
dict_status.innerText = `Dictionary holds ${dictLinesLen} entries.`
updateState(MainState.Ready)
// Install event listener for browser navigation updating the URL hash
window.addEventListener('hashchange', searchFromUrl)
Expand Down Expand Up @@ -384,8 +394,10 @@ window.addEventListener('DOMContentLoaded', async () => {
dict_upd_status.innerText = '(Updating in background...)'
else {
dict_upd_status.innerText = ''
if (event.data.status === 'done')
dict_status.innerText = `Dictionary holds ${dictLinesLen} entries (updated in background).`
if (event.data.status === 'done') {
dictWasUpdated = true
updateState(state)
}
}
}
// -------------------------{ results }-------------------------
Expand Down
2 changes: 1 addition & 1 deletion src/workers/dict-search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export function searchDict(dictLines :string[], what :string): [string, string[]
// now that we've sorted, we can strip the scores out of the returned values
const matches :string[] = scoredMatches.map(([line, _score]) => line)

console.debug(`Search for ${whatRe} found ${matches.length} matches in ${new Date().getTime()-searchStartMs}ms.`)
console.log(`Search for ${whatRe} found ${matches.length} matches in ${new Date().getTime()-searchStartMs}ms.`)
// if we sent a progress report <100% previously, make sure to send a 100% one
if (sentLt100Report) {
const m :WorkerMessageType = { type: 'search-prog', percent: 100 }
Expand Down

0 comments on commit 70597ba

Please sign in to comment.