Database change event (#826)

* Add api.triggerDatabaseUpdated and yomichan.on('databaseUpdated')

* Update databaseUpdated event usage
This commit is contained in:
toasted-nutbread 2020-09-13 18:43:44 -04:00 committed by GitHub
parent 8b033a1650
commit 5ec5d0c91c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 29 additions and 22 deletions

View File

@ -115,7 +115,8 @@ class Backend {
['setAllSettings', {async: true, contentScript: false, handler: this._onApiSetAllSettings.bind(this)}], ['setAllSettings', {async: true, contentScript: false, handler: this._onApiSetAllSettings.bind(this)}],
['getOrCreateSearchPopup', {async: true, contentScript: true, handler: this._onApiGetOrCreateSearchPopup.bind(this)}], ['getOrCreateSearchPopup', {async: true, contentScript: true, handler: this._onApiGetOrCreateSearchPopup.bind(this)}],
['isTabSearchPopup', {async: true, contentScript: true, handler: this._onApiIsTabSearchPopup.bind(this)}], ['isTabSearchPopup', {async: true, contentScript: true, handler: this._onApiIsTabSearchPopup.bind(this)}],
['getDefinitionAudio', {async: true, contentScript: true, handler: this._onApiGetDefinitionAudio.bind(this)}] ['getDefinitionAudio', {async: true, contentScript: true, handler: this._onApiGetDefinitionAudio.bind(this)}],
['triggerDatabaseUpdated', {async: false, contentScript: true, handler: this._onApiTriggerDatabaseUpdated.bind(this)}]
]); ]);
this._messageHandlersWithProgress = new Map([ this._messageHandlersWithProgress = new Map([
['deleteDictionary', {async: true, contentScript: false, handler: this._onApiDeleteDictionary.bind(this)}] ['deleteDictionary', {async: true, contentScript: false, handler: this._onApiDeleteDictionary.bind(this)}]
@ -709,6 +710,7 @@ class Backend {
async _onApiPurgeDatabase() { async _onApiPurgeDatabase() {
this._translator.clearDatabaseCaches(); this._translator.clearDatabaseCaches();
await this._dictionaryDatabase.purge(); await this._dictionaryDatabase.purge();
this._triggerDatabaseUpdated('dictionary', 'purge');
} }
async _onApiGetMedia({targets}) { async _onApiGetMedia({targets}) {
@ -751,6 +753,7 @@ class Backend {
async _onApiDeleteDictionary({dictionaryName}, sender, onProgress) { async _onApiDeleteDictionary({dictionaryName}, sender, onProgress) {
this._translator.clearDatabaseCaches(); this._translator.clearDatabaseCaches();
await this._dictionaryDatabase.deleteDictionary(dictionaryName, {rate: 1000}, onProgress); await this._dictionaryDatabase.deleteDictionary(dictionaryName, {rate: 1000}, onProgress);
this._triggerDatabaseUpdated('dictionary', 'delete');
} }
async _onApiModifySettings({targets, source}) { async _onApiModifySettings({targets, source}) {
@ -807,6 +810,10 @@ class Backend {
return this._getDefinitionAudio(sources, expression, reading, details); return this._getDefinitionAudio(sources, expression, reading, details);
} }
_onApiTriggerDatabaseUpdated({type, cause}) {
this._triggerDatabaseUpdated(type, cause);
}
// Command handlers // Command handlers
async _onCommandSearch(params) { async _onCommandSearch(params) {
@ -1720,4 +1727,8 @@ class Backend {
default: throw new Error('Unknown image media type'); default: throw new Error('Unknown image media type');
} }
} }
_triggerDatabaseUpdated(type, cause) {
this._sendMessageAllTabs('databaseUpdated', {type, cause});
}
} }

View File

@ -307,8 +307,6 @@ class SettingsDictionaryEntryUI {
prevention.end(); prevention.end();
this.isDeleting = false; this.isDeleting = false;
progress.hidden = true; progress.hidden = true;
this.parent.trigger('databaseUpdated');
} }
} }
@ -397,13 +395,13 @@ class DictionaryController {
); );
this._dictionaryUI.save = () => this._settingsController.save(); this._dictionaryUI.save = () => this._settingsController.save();
this._dictionaryUI.preventPageExit = this._preventPageExit.bind(this); this._dictionaryUI.preventPageExit = this._preventPageExit.bind(this);
this._dictionaryUI.on('databaseUpdated', this._onDatabaseUpdated.bind(this));
document.querySelector('#dict-main').addEventListener('change', this._onDictionaryMainChanged.bind(this), false); document.querySelector('#dict-main').addEventListener('change', this._onDictionaryMainChanged.bind(this), false);
document.querySelector('#database-enable-prefix-wildcard-searches').addEventListener('change', this._onDatabaseEnablePrefixWildcardSearchesChanged.bind(this), false); document.querySelector('#database-enable-prefix-wildcard-searches').addEventListener('change', this._onDatabaseEnablePrefixWildcardSearchesChanged.bind(this), false);
this._settingsController.on('optionsChanged', this._onOptionsChanged.bind(this)); this._settingsController.on('optionsChanged', this._onOptionsChanged.bind(this));
this._settingsController.on('databaseUpdated', this._onDatabaseUpdated.bind(this));
yomichan.on('databaseUpdated', this._onDatabaseUpdated.bind(this));
await this._onOptionsChanged(); await this._onOptionsChanged();
await this._onDatabaseUpdated(); await this._onDatabaseUpdated();

View File

@ -110,8 +110,6 @@ class DictionaryImportController {
if (errors.length > 0) { if (errors.length > 0) {
this._showErrors(errors); this._showErrors(errors);
} }
this._triggerDatabaseUpdated('purge');
} catch (error) { } catch (error) {
this._showErrors([error]); this._showErrors([error]);
} finally { } finally {
@ -178,6 +176,7 @@ class DictionaryImportController {
const dictionaryImporter = new DictionaryImporter(); const dictionaryImporter = new DictionaryImporter();
const archiveContent = await this._readFile(file); const archiveContent = await this._readFile(file);
const {result, errors} = await dictionaryImporter.importDictionary(dictionaryDatabase, archiveContent, importDetails, onProgress); const {result, errors} = await dictionaryImporter.importDictionary(dictionaryDatabase, archiveContent, importDetails, onProgress);
api.triggerDatabaseUpdated('dictionary', 'import');
const errors2 = await this._addDictionarySettings(result.sequenced, result.title); const errors2 = await this._addDictionarySettings(result.sequenced, result.title);
if (errors.length > 0) { if (errors.length > 0) {
@ -185,8 +184,6 @@ class DictionaryImportController {
allErrors.push(new Error(`Dictionary may not have been imported properly: ${allErrors.length} error${allErrors.length === 1 ? '' : 's'} reported.`)); allErrors.push(new Error(`Dictionary may not have been imported properly: ${allErrors.length} error${allErrors.length === 1 ? '' : 's'} reported.`));
this._showErrors(allErrors); this._showErrors(allErrors);
} }
this._triggerDatabaseUpdated('import');
} finally { } finally {
dictionaryDatabase.close(); dictionaryDatabase.close();
} }
@ -271,10 +268,6 @@ class DictionaryImportController {
this._errorContainer.hidden = true; this._errorContainer.hidden = true;
} }
_triggerDatabaseUpdated(cause) {
this._settingsController.triggerDatabaseUpdated(cause);
}
_readFile(file) { _readFile(file) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const reader = new FileReader(); const reader = new FileReader();

View File

@ -125,10 +125,6 @@ class SettingsController extends EventDispatcher {
return obj; return obj;
} }
triggerDatabaseUpdated(cause) {
this.trigger('databaseUpdated', {cause});
}
// Private // Private
_setProfileIndex(value) { _setProfileIndex(value) {

View File

@ -197,6 +197,10 @@ const api = (() => {
return this._invoke('getDefinitionAudio', {sources, expression, reading, details}); return this._invoke('getDefinitionAudio', {sources, expression, reading, details});
} }
triggerDatabaseUpdated(type, cause) {
return this._invoke('triggerDatabaseUpdated', {type, cause});
}
// Invoke functions with progress // Invoke functions with progress
deleteDictionary(dictionaryName, onProgress) { deleteDictionary(dictionaryName, onProgress) {

View File

@ -56,11 +56,12 @@ const yomichan = (() => {
this._isBackendReadyPromiseResolve = resolve; this._isBackendReadyPromiseResolve = resolve;
this._messageHandlers = new Map([ this._messageHandlers = new Map([
['isReady', {async: false, handler: this._onMessageIsReady.bind(this)}], ['isReady', {async: false, handler: this._onMessageIsReady.bind(this)}],
['backendReady', {async: false, handler: this._onMessageBackendReady.bind(this)}], ['backendReady', {async: false, handler: this._onMessageBackendReady.bind(this)}],
['getUrl', {async: false, handler: this._onMessageGetUrl.bind(this)}], ['getUrl', {async: false, handler: this._onMessageGetUrl.bind(this)}],
['optionsUpdated', {async: false, handler: this._onMessageOptionsUpdated.bind(this)}], ['optionsUpdated', {async: false, handler: this._onMessageOptionsUpdated.bind(this)}],
['zoomChanged', {async: false, handler: this._onMessageZoomChanged.bind(this)}] ['databaseUpdated', {async: false, handler: this._onMessageDatabaseUpdated.bind(this)}],
['zoomChanged', {async: false, handler: this._onMessageZoomChanged.bind(this)}]
]); ]);
} }
@ -290,6 +291,10 @@ const yomichan = (() => {
this.trigger('optionsUpdated', {source}); this.trigger('optionsUpdated', {source});
} }
_onMessageDatabaseUpdated({type, cause}) {
this.trigger('databaseUpdated', {type, cause});
}
_onMessageZoomChanged({oldZoomFactor, newZoomFactor}) { _onMessageZoomChanged({oldZoomFactor, newZoomFactor}) {
this.trigger('zoomChanged', {oldZoomFactor, newZoomFactor}); this.trigger('zoomChanged', {oldZoomFactor, newZoomFactor});
} }