Display try catch refactor (#704)

* Remove try-catch from _termLookup

Already handled by _onTermLookup

* Move try-catch out of _onTermLookup and into _onGlossaryMouseUp
This commit is contained in:
toasted-nutbread 2020-08-01 16:33:21 -04:00 committed by GitHub
parent f271c83d77
commit a562a11498
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -521,78 +521,74 @@ class Display extends EventDispatcher {
_onGlossaryMouseUp(e) { _onGlossaryMouseUp(e) {
if (!this._clickScanPrevent && DOM.isMouseButtonPressed(e, 'primary')) { if (!this._clickScanPrevent && DOM.isMouseButtonPressed(e, 'primary')) {
this._onTermLookup(e); try {
this._onTermLookup(e);
} catch (error) {
this.onError(error);
}
} }
} }
async _onTermLookup(e) { async _onTermLookup(e) {
try { if (!this._historyHasState()) { return; }
if (!this._historyHasState()) { return; }
const termLookupResults = await this._termLookup(e); const termLookupResults = await this._termLookup(e);
if (!termLookupResults || !this._historyHasState()) { return; } if (!termLookupResults || !this._historyHasState()) { return; }
const {state} = this._history; const {state} = this._history;
const {textSource, definitions} = termLookupResults; const {textSource, definitions} = termLookupResults;
const scannedElement = e.target; const scannedElement = e.target;
const sentenceExtent = this._options.anki.sentenceExt; const sentenceExtent = this._options.anki.sentenceExt;
const layoutAwareScan = this._options.scanning.layoutAwareScan; const layoutAwareScan = this._options.scanning.layoutAwareScan;
const sentence = docSentenceExtract(textSource, sentenceExtent, layoutAwareScan); const sentence = docSentenceExtract(textSource, sentenceExtent, layoutAwareScan);
state.focusEntry = this._entryIndexFind(scannedElement); state.focusEntry = this._entryIndexFind(scannedElement);
state.scrollX = this._windowScroll.x; state.scrollX = this._windowScroll.x;
state.scrollY = this._windowScroll.y; state.scrollY = this._windowScroll.y;
this._historyStateUpdate(state); this._historyStateUpdate(state);
const query = textSource.text(); const query = textSource.text();
const details = { const details = {
focus: false, focus: false,
history: true, history: true,
params: this._createSearchParams('terms', query, false), params: this._createSearchParams('terms', query, false),
state: { state: {
focusEntry: 0, focusEntry: 0,
sentence, sentence,
url: state.url url: state.url
}, },
content: { content: {
definitions definitions
} }
}; };
this.setContent(details); this.setContent(details);
} catch (error) {
this.onError(error);
}
} }
async _termLookup(e) { async _termLookup(e) {
try { e.preventDefault();
e.preventDefault();
const {length: scanLength, deepDomScan: deepScan, layoutAwareScan} = this._options.scanning; const {length: scanLength, deepDomScan: deepScan, layoutAwareScan} = this._options.scanning;
const textSource = docRangeFromPoint(e.clientX, e.clientY, deepScan); const textSource = docRangeFromPoint(e.clientX, e.clientY, deepScan);
if (textSource === null) { if (textSource === null) {
return false;
}
let definitions, length;
try {
textSource.setEndOffset(scanLength, layoutAwareScan);
({definitions, length} = await api.termsFind(textSource.text(), {}, this.getOptionsContext()));
if (definitions.length === 0) {
return false; return false;
} }
let definitions, length; textSource.setEndOffset(length, layoutAwareScan);
try { } finally {
textSource.setEndOffset(scanLength, layoutAwareScan); textSource.cleanup();
({definitions, length} = await api.termsFind(textSource.text(), {}, this.getOptionsContext()));
if (definitions.length === 0) {
return false;
}
textSource.setEndOffset(length, layoutAwareScan);
} finally {
textSource.cleanup();
}
return {textSource, definitions};
} catch (error) {
this.onError(error);
} }
return {textSource, definitions};
} }
_onAudioPlay(e) { _onAudioPlay(e) {