Update AudioController to not use mutable options references (#585)

This commit is contained in:
toasted-nutbread 2020-05-30 21:54:38 -04:00 committed by GitHub
parent db209c9116
commit cfd3a1ec3a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -23,7 +23,6 @@ class AudioController {
constructor(settingsController) { constructor(settingsController) {
this._settingsController = settingsController; this._settingsController = settingsController;
this._audioSystem = null; this._audioSystem = null;
this._settingsAudioSources = null;
this._audioSourceContainer = null; this._audioSourceContainer = null;
this._audioSourceAddButton = null; this._audioSourceAddButton = null;
this._audioSourceEntries = []; this._audioSourceEntries = [];
@ -45,28 +44,22 @@ class AudioController {
this._settingsController.on('optionsChanged', this._onOptionsChanged.bind(this)); this._settingsController.on('optionsChanged', this._onOptionsChanged.bind(this));
this._onOptionsChanged(); const options = await this._settingsController.getOptions();
this._onOptionsChanged({options});
} }
// Private // Private
async _onOptionsChanged() { _onOptionsChanged({options}) {
const options = await this._settingsController.getOptionsMutable(); for (let i = this._audioSourceEntries.length - 1; i >= 0; --i) {
this._cleanupAudioSourceEntry(i);
for (const entry of [...this._audioSourceEntries]) {
this._removeAudioSourceEntry(entry);
} }
this._settingsAudioSources = options.audio.sources; for (const audioSource of options.audio.sources) {
for (const audioSource of toIterable(this._settingsAudioSources)) {
this._createAudioSourceEntry(audioSource); this._createAudioSourceEntry(audioSource);
} }
} }
async _save() {
await this._settingsController.save();
}
_prepareTextToSpeech() { _prepareTextToSpeech() {
if (typeof speechSynthesis === 'undefined') { return; } if (typeof speechSynthesis === 'undefined') { return; }
@ -157,7 +150,7 @@ class AudioController {
'custom' 'custom'
]; ];
for (const source of audioSourcesAvailable) { for (const source of audioSourcesAvailable) {
if (this._settingsAudioSources.indexOf(source) < 0) { if (!this._audioSourceEntries.some((metadata) => metadata.value === source)) {
return source; return source;
} }
} }
@ -174,7 +167,8 @@ class AudioController {
const entry = { const entry = {
container, container,
eventListeners eventListeners,
value
}; };
eventListeners.addEventListener(select, 'change', this._onAudioSourceSelectChange.bind(this, entry), false); eventListeners.addEventListener(select, 'change', this._onAudioSourceSelectChange.bind(this, entry), false);
@ -184,46 +178,56 @@ class AudioController {
this._audioSourceEntries.push(entry); this._audioSourceEntries.push(entry);
} }
_removeAudioSourceEntry(entry) { async _removeAudioSourceEntry(entry) {
const index = this._audioSourceEntries.indexOf(entry); const index = this._audioSourceEntries.indexOf(entry);
if (index < 0) { return; } if (index < 0) { return; }
const {container, eventListeners} = entry; this._cleanupAudioSourceEntry(index);
await this._settingsController.modifyProfileSettings([{
action: 'splice',
path: 'audio.sources',
start: index,
deleteCount: 1,
items: []
}]);
}
_cleanupAudioSourceEntry(index) {
const {container, eventListeners} = this._audioSourceEntries[index];
if (container.parentNode !== null) { if (container.parentNode !== null) {
container.parentNode.removeChild(container); container.parentNode.removeChild(container);
} }
eventListeners.removeAllEventListeners(); eventListeners.removeAllEventListeners();
this._audioSourceEntries.splice(index, 1); this._audioSourceEntries.splice(index, 1);
this._settingsAudioSources.splice(index, 1);
for (let i = index, ii = this._audioSourceEntries.length; i < ii; ++i) {
this._audioSourceEntries[i].index = i;
}
} }
_onTextToSpeechVoiceChange(e) { _onTextToSpeechVoiceChange(e) {
e.currentTarget.dataset.value = e.currentTarget.value; e.currentTarget.dataset.value = e.currentTarget.value;
} }
_onAddAudioSource() { async _onAddAudioSource() {
const audioSource = this._getUnusedAudioSource(); const audioSource = this._getUnusedAudioSource();
this._settingsAudioSources.push(audioSource); const index = this._audioSourceEntries.length;
this._createAudioSourceEntry(audioSource); this._createAudioSourceEntry(audioSource);
this._save(); await this._settingsController.modifyProfileSettings([{
action: 'splice',
path: 'audio.sources',
start: index,
deleteCount: 0,
items: [audioSource]
}]);
} }
_onAudioSourceSelectChange(entry, event) { async _onAudioSourceSelectChange(entry, event) {
const index = this._audioSourceEntries.indexOf(entry); const index = this._audioSourceEntries.indexOf(entry);
if (index < 0) { return; } if (index < 0) { return; }
const value = event.currentTarget.value; const value = event.currentTarget.value;
this._settingsAudioSources[index] = value; entry.value = value;
this._save(); await this._settingsController.setProfileSetting(`audio.sources[${index}]`, value);
} }
_onAudioSourceRemoveClicked(entry) { async _onAudioSourceRemoveClicked(entry) {
this._removeAudioSourceEntry(entry); await this._removeAudioSourceEntry(entry);
this._save();
} }
} }