Improve primary audio assignment when audio isn't available (#1407)

This commit is contained in:
toasted-nutbread 2021-02-15 21:46:55 -05:00 committed by GitHub
parent 55f5182ca9
commit 3a86601c88
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -30,6 +30,7 @@ class DisplayAudio {
this._eventListeners = new EventListenerCollection(); this._eventListeners = new EventListenerCollection();
this._cache = new Map(); this._cache = new Map();
this._menuContainer = document.querySelector('#popup-menus'); this._menuContainer = document.querySelector('#popup-menus');
this._entriesToken = {};
} }
get autoPlayAudioDelay() { get autoPlayAudioDelay() {
@ -50,6 +51,7 @@ class DisplayAudio {
} }
cleanupEntries() { cleanupEntries() {
this._entriesToken = {};
this._cache.clear(); this._cache.clear();
this.clearAutoPlayTimer(); this.clearAutoPlayTimer();
this._eventListeners.removeAllEventListeners(); this._eventListeners.removeAllEventListeners();
@ -105,7 +107,9 @@ class DisplayAudio {
this.clearAutoPlayTimer(); this.clearAutoPlayTimer();
const expressionReading = this._getExpressionAndReading(definitionIndex, expressionIndex); const expressionReading = this._getExpressionAndReading(definitionIndex, expressionIndex);
if (expressionReading === null) { return; } if (expressionReading === null) {
return {audio: null, source: null, valid: false};
}
const buttons = this._getAudioPlayButtons(definitionIndex, expressionIndex); const buttons = this._getAudioPlayButtons(definitionIndex, expressionIndex);
@ -125,9 +129,10 @@ class DisplayAudio {
// Create audio // Create audio
let audio; let audio;
let title; let title;
let source = null;
const info = await this._createExpressionAudio(sources, sourceDetailsMap, expression, reading, {textToSpeechVoice, customSourceUrl, customSourceType}); const info = await this._createExpressionAudio(sources, sourceDetailsMap, expression, reading, {textToSpeechVoice, customSourceUrl, customSourceType});
if (info !== null) { const valid = (info !== null);
let source; if (valid) {
({audio, source} = info); ({audio, source} = info);
const sourceIndex = sources.indexOf(source); const sourceIndex = sources.indexOf(source);
title = `From source ${1 + sourceIndex}: ${source}`; title = `From source ${1 + sourceIndex}: ${source}`;
@ -161,6 +166,8 @@ class DisplayAudio {
// NOP // NOP
} }
} }
return {audio, source, valid};
} finally { } finally {
progressIndicatorVisible.clearOverride(overrideToken); progressIndicatorVisible.clearOverride(overrideToken);
} }
@ -194,7 +201,7 @@ class DisplayAudio {
const {detail: {action, item, menu}} = e; const {detail: {action, item, menu}} = e;
switch (action) { switch (action) {
case 'playAudioFromSource': case 'playAudioFromSource':
this._playAudioFromSource(definitionIndex, expressionIndex, item, menu); this._playAudioFromSource(definitionIndex, expressionIndex, item);
break; break;
case 'setPrimaryAudio': case 'setPrimaryAudio':
e.preventDefault(); e.preventDefault();
@ -231,16 +238,22 @@ class DisplayAudio {
return {source, index, hasIndex}; return {source, index, hasIndex};
} }
_playAudioFromSource(definitionIndex, expressionIndex, item, menu) { async _playAudioFromSource(definitionIndex, expressionIndex, item) {
const sourceInfo = this._getMenuItemSourceInfo(item); const sourceInfo = this._getMenuItemSourceInfo(item);
if (sourceInfo === null) { return; } if (sourceInfo === null) { return; }
const {source, index, hasIndex} = sourceInfo; const {source, index, hasIndex} = sourceInfo;
const sourceDetailsMap = hasIndex ? new Map([[source, {start: index, end: index + 1}]]) : null; const sourceDetailsMap = hasIndex ? new Map([[source, {start: index, end: index + 1}]]) : null;
this._setPrimaryAudio(definitionIndex, expressionIndex, item, menu, false); try {
const token = this._entriesToken;
this.playAudio(definitionIndex, expressionIndex, [source], sourceDetailsMap); const {valid} = await this.playAudio(definitionIndex, expressionIndex, [source], sourceDetailsMap);
if (valid && token === this._entriesToken) {
this._setPrimaryAudio(definitionIndex, expressionIndex, item, null, false);
}
} catch (e) {
// NOP
}
} }
_setPrimaryAudio(definitionIndex, expressionIndex, item, menu, canToggleOff) { _setPrimaryAudio(definitionIndex, expressionIndex, item, menu, canToggleOff) {
@ -260,8 +273,10 @@ class DisplayAudio {
primaryCardAudio = (!canToggleOff || primaryCardAudio === null || primaryCardAudio.source !== source || primaryCardAudio.index !== index) ? {source, index} : null; primaryCardAudio = (!canToggleOff || primaryCardAudio === null || primaryCardAudio.source !== source || primaryCardAudio.index !== index) ? {source, index} : null;
cacheEntry.primaryCardAudio = primaryCardAudio; cacheEntry.primaryCardAudio = primaryCardAudio;
if (menu !== null) {
this._updateMenuPrimaryCardAudio(menu.bodyNode, expression, reading); this._updateMenuPrimaryCardAudio(menu.bodyNode, expression, reading);
} }
}
_getAudioPlayButtonExpressionIndex(button) { _getAudioPlayButtonExpressionIndex(button) {
const expressionNode = button.closest('.term-expression'); const expressionNode = button.closest('.term-expression');