Improve asynchronous handling and status rendering in proof function

The changes ensure that all recursive calls are properly awaited, leading to more predictable and reliable behavior.
Additionally, the status rendering has been also improved with these changes.
This commit is contained in:
Aljaž Grilc 2024-04-08 11:56:02 +02:00
parent 5b13bd3d28
commit cf8bc8afbd

View File

@ -40,10 +40,6 @@ class BesService {
static register(hostElement, textAreaService) { static register(hostElement, textAreaService) {
let service = new BesService(hostElement) let service = new BesService(hostElement)
service.proof(hostElement) service.proof(hostElement)
if (service.statusIcon.classList.contains('bes-status-loading')) {
service.updateStatusIcon('bes-status-success')
service.statusDiv.title = 'BesService je registriran.'
}
if (textAreaService) service.textAreaService = textAreaService if (textAreaService) service.textAreaService = textAreaService
return service return service
} }
@ -75,7 +71,7 @@ class BesService {
* @param {Node} node DOM root node to proof * @param {Node} node DOM root node to proof
* @returns {Array} Markup of text to proof using BesStr * @returns {Array} Markup of text to proof using BesStr
*/ */
async proof(node) { async proof(node, isInitialCall = true) {
this.updateStatusIcon('bes-status-loading') this.updateStatusIcon('bes-status-loading')
this.statusDiv.title = 'BesService je v procesu preverjanja pravopisa.' this.statusDiv.title = 'BesService je v procesu preverjanja pravopisa.'
switch (node.nodeType) { switch (node.nodeType) {
@ -91,9 +87,10 @@ class BesService {
] ]
} }
this.clearMistakeMarkup(node) this.clearMistakeMarkup(node)
let data = [] let dataPromises = Array.from(node.childNodes).map(child =>
for (const el2 of node.childNodes) this.proof(child, false)
data = data.concat(await this.proof(el2)) )
let data = (await Promise.all(dataPromises)).flat()
if (data.some(x => !x.markup && !/^\s*$/.test(x.text))) { if (data.some(x => !x.markup && !/^\s*$/.test(x.text))) {
const requestData = { const requestData = {
format: 'plain', format: 'plain',
@ -111,7 +108,7 @@ class BesService {
body: new URLSearchParams(requestData) body: new URLSearchParams(requestData)
}) })
const signal = this.abortController.signal const signal = this.abortController.signal
fetch(request, { signal }) await fetch(request, { signal })
.then(response => { .then(response => {
if (!response.ok) { if (!response.ok) {
this.updateStatusIcon('bes-status-error') this.updateStatusIcon('bes-status-error')
@ -181,14 +178,27 @@ class BesService {
) )
}) })
} }
if (isInitialCall) {
// TODO: Check count in 'makrofinančno' case.
const count = this.children.reduce(
(total, child) => total + child.matches.length,
0
)
if (count > 0) {
this.updateStatusIcon('bes-status-mistakes')
this.statusDiv.title = 'Število napak: ' + count
} else {
this.updateStatusIcon('bes-status-success') this.updateStatusIcon('bes-status-success')
this.statusDiv.title = 'BesService je registriran.' this.statusDiv.title = 'V besedilu ni napak.'
}
}
return [{ text: '<' + node.tagName + '/>', node: node, markup: true }] return [{ text: '<' + node.tagName + '/>', node: node, markup: true }]
} else { } else {
// Inline elements require no markup. Keep plain text only. // Inline elements require no markup. Keep plain text only.
let data = [] let dataPromises = Array.from(node.childNodes).map(child =>
for (const el2 of node.childNodes) this.proof(child, false)
data = data.concat(await this.proof(el2)) )
let data = (await Promise.all(dataPromises)).flat()
return data return data
} }
@ -288,19 +298,6 @@ class BesService {
element: el, element: el,
matches: matches matches: matches
}) })
// TODO: This also shows the count of mistakes that are not visible, meaning that they are hidden behind the shown ones.
const count = this.children.reduce(
(total, child) => total + child.matches.length,
0
)
if (count > 0) {
this.updateStatusIcon('bes-status-mistakes')
this.statusDiv.title = 'Število napak: ' + count
} else {
this.updateStatusIcon('bes-status-success')
this.statusDiv.title = 'V besedilu ni napak.'
}
} }
/** /**