service2.js: Move abortController management upstream

Generally, any gramar checking will be cancellable and will need this.
This commit is contained in:
Simon Rozman 2024-05-13 13:21:30 +02:00
parent ff54607e7e
commit 5e339566f3

View File

@ -41,6 +41,7 @@ class BesService {
* Unregisters grammar checking service. * Unregisters grammar checking service.
*/ */
unregister() { unregister() {
if (this.abortController) this.abortController.abort()
besServices = besServices.filter(item => item !== this) besServices = besServices.filter(item => item !== this)
this.hostElement.removeEventListener('scroll', this.onScroll) this.hostElement.removeEventListener('scroll', this.onScroll)
this.hostElement.spellcheck = this.originalSpellcheck this.hostElement.spellcheck = this.originalSpellcheck
@ -55,6 +56,7 @@ class BesService {
this.proofingError = null // The first non-fatal error in grammar-checking run this.proofingError = null // The first non-fatal error in grammar-checking run
this.proofingMatches = 0 // Number of grammar mistakes detected in entire grammar-checking run this.proofingMatches = 0 // Number of grammar mistakes detected in entire grammar-checking run
this.updateStatusIcon('bes-status-loading', 'Besana preverja pravopis.') this.updateStatusIcon('bes-status-loading', 'Besana preverja pravopis.')
this.abortController = new AbortController()
} }
/** /**
@ -72,6 +74,7 @@ class BesService {
* @param {Response} response HTTP response * @param {Response} response HTTP response
*/ */
onFailedProofing(response) { onFailedProofing(response) {
delete this.abortController
this.updateStatusIcon( this.updateStatusIcon(
'bes-status-error', 'bes-status-error',
`Pri preverjanju pravopisa je prišlo do napake ${response.status} ${response.statusText}.` `Pri preverjanju pravopisa je prišlo do napake ${response.status} ${response.statusText}.`
@ -102,6 +105,7 @@ class BesService {
* Called when grammar-checking run is ended * Called when grammar-checking run is ended
*/ */
onEndProofing() { onEndProofing() {
delete this.abortController
if (this.proofingError) { if (this.proofingError) {
this.updateStatusIcon( this.updateStatusIcon(
'bes-status-error', 'bes-status-error',
@ -264,38 +268,9 @@ class BesDOMService extends BesService {
this.hostElement.removeEventListener('input', this.onInput) this.hostElement.removeEventListener('input', this.onInput)
this.hostElement.removeEventListener('beforeinput', this.onBeforeInput) this.hostElement.removeEventListener('beforeinput', this.onBeforeInput)
if (this.timer) clearTimeout(this.timer) if (this.timer) clearTimeout(this.timer)
if (this.abortController) this.abortController.abort()
super.unregister() super.unregister()
} }
/**
* Called initially when grammar-checking run is started
*/
onStartProofing() {
super.onStartProofing()
this.abortController = new AbortController()
}
/**
* Called when grammar-checking failed (as 500 Internal server error, timeout, etc.)
*
* This error is fatal and proofing will not continue.
*
* @param {Response} response HTTP response
*/
onFailedProofing(response) {
delete this.abortController
super.onFailedProofing(response)
}
/**
* Called when grammar-checking run is ended
*/
onEndProofing() {
delete this.abortController
super.onEndProofing()
}
/** /**
* Called to report scrolling * Called to report scrolling
*/ */