Profile index fixes (#2207)

* Update settings controller to consistently initialize

* Allow profile index to be reset if an error occurs

* Update message handler to be async

* Fix error when deleting the current profile
This commit is contained in:
toasted-nutbread 2022-08-20 11:31:50 -04:00 committed by GitHub
parent 9436928e3d
commit 5c267f4bb7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 47 additions and 34 deletions

View File

@ -178,7 +178,7 @@ class Display extends EventDispatcher {
['previousEntryDifferentDictionary', () => { this._focusEntryWithDifferentDictionary(-1, true); }] ['previousEntryDifferentDictionary', () => { this._focusEntryWithDifferentDictionary(-1, true); }]
]); ]);
this.registerDirectMessageHandlers([ this.registerDirectMessageHandlers([
['Display.setOptionsContext', {async: false, handler: this._onMessageSetOptionsContext.bind(this)}], ['Display.setOptionsContext', {async: true, handler: this._onMessageSetOptionsContext.bind(this)}],
['Display.setContent', {async: false, handler: this._onMessageSetContent.bind(this)}], ['Display.setContent', {async: false, handler: this._onMessageSetContent.bind(this)}],
['Display.setCustomCss', {async: false, handler: this._onMessageSetCustomCss.bind(this)}], ['Display.setCustomCss', {async: false, handler: this._onMessageSetCustomCss.bind(this)}],
['Display.setContentScale', {async: false, handler: this._onMessageSetContentScale.bind(this)}], ['Display.setContentScale', {async: false, handler: this._onMessageSetContentScale.bind(this)}],
@ -459,21 +459,25 @@ class Display extends EventDispatcher {
this._documentFocusController.blurElement(element); this._documentFocusController.blurElement(element);
} }
searchLast() { searchLast(updateOptionsContext) {
const type = this._contentType; const type = this._contentType;
if (type === 'clear') { return; } if (type === 'clear') { return; }
const query = this._query; const query = this._query;
const hasState = this._historyHasState();
const state = ( const state = (
this._historyHasState() ? hasState ?
clone(this._history.state) : clone(this._history.state) :
{ {
focusEntry: 0, focusEntry: 0,
optionsContext: this._optionsContext, optionsContext: null,
url: window.location.href, url: window.location.href,
sentence: {text: query, offset: 0}, sentence: {text: query, offset: 0},
documentTitle: document.title documentTitle: document.title
} }
); );
if (!hasState || updateOptionsContext) {
state.optionsContext = clone(this._optionsContext);
}
const details = { const details = {
focus: false, focus: false,
historyMode: 'clear', historyMode: 'clear',
@ -551,9 +555,9 @@ class Display extends EventDispatcher {
invokeMessageHandler(messageHandler, params, callback); invokeMessageHandler(messageHandler, params, callback);
} }
_onMessageSetOptionsContext({optionsContext}) { async _onMessageSetOptionsContext({optionsContext}) {
this.setOptionsContext(optionsContext); await this.setOptionsContext(optionsContext);
this.searchLast(); this.searchLast(true);
} }
_onMessageSetContent({details}) { _onMessageSetContent({details}) {

View File

@ -135,7 +135,7 @@ class SearchDisplayController {
await this._display.updateOptions(); await this._display.updateOptions();
const query = this._queryInput.value; const query = this._queryInput.value;
if (query) { if (query) {
this._display.searchLast(); this._display.searchLast(false);
} }
} }

View File

@ -111,7 +111,7 @@ function getOperatingSystemDisplayName(os) {
})(); })();
const settingsController = new SettingsController(); const settingsController = new SettingsController();
settingsController.prepare(); await settingsController.prepare();
const backupController = new BackupController(settingsController, null); const backupController = new BackupController(settingsController, null);
await backupController.prepare(); await backupController.prepare();

View File

@ -93,8 +93,8 @@ function setupPermissionsToggles() {
const modalController = new ModalController(); const modalController = new ModalController();
modalController.prepare(); modalController.prepare();
const settingsController = new SettingsController(0); const settingsController = new SettingsController();
settingsController.prepare(); await settingsController.prepare();
const permissionsToggleController = new PermissionsToggleController(settingsController); const permissionsToggleController = new PermissionsToggleController(settingsController);
permissionsToggleController.prepare(); permissionsToggleController.prepare();

View File

@ -215,13 +215,13 @@ class ProfileController {
this._updateProfileSelectOptions(); this._updateProfileSelectOptions();
// Modify settings
await this._settingsController.modifyGlobalSettings(modifications);
// Update profile index // Update profile index
if (settingsProfileIndex === profileIndex) { if (settingsProfileIndex === profileIndex) {
this._settingsController.profileIndex = profileCurrentNew; this._settingsController.profileIndex = profileCurrentNew;
} }
// Modify settings
await this._settingsController.modifyGlobalSettings(modifications);
} }
async swapProfiles(index1, index2) { async swapProfiles(index1, index2) {

View File

@ -22,9 +22,9 @@
*/ */
class SettingsController extends EventDispatcher { class SettingsController extends EventDispatcher {
constructor(profileIndex=0) { constructor() {
super(); super();
this._profileIndex = profileIndex; this._profileIndex = 0;
this._source = generateId(16); this._source = generateId(16);
this._pageExitPreventions = new Set(); this._pageExitPreventions = new Set();
this._pageExitPreventionEventListeners = new EventListenerCollection(); this._pageExitPreventionEventListeners = new EventListenerCollection();
@ -42,23 +42,28 @@ class SettingsController extends EventDispatcher {
set profileIndex(value) { set profileIndex(value) {
if (this._profileIndex === value) { return; } if (this._profileIndex === value) { return; }
this._setProfileIndex(value); this._setProfileIndex(value, true);
} }
get permissionsUtil() { get permissionsUtil() {
return this._permissionsUtil; return this._permissionsUtil;
} }
prepare() { async prepare() {
yomichan.on('optionsUpdated', this._onOptionsUpdated.bind(this)); yomichan.on('optionsUpdated', this._onOptionsUpdated.bind(this));
if (this._canObservePermissionsChanges()) { if (this._canObservePermissionsChanges()) {
chrome.permissions.onAdded.addListener(this._onPermissionsChanged.bind(this)); chrome.permissions.onAdded.addListener(this._onPermissionsChanged.bind(this));
chrome.permissions.onRemoved.addListener(this._onPermissionsChanged.bind(this)); chrome.permissions.onRemoved.addListener(this._onPermissionsChanged.bind(this));
} }
const optionsFull = await this.getOptionsFull();
const {profiles, profileCurrent} = optionsFull;
if (profileCurrent >= 0 && profileCurrent < profiles.length) {
this._profileIndex = profileCurrent;
}
} }
async refresh() { async refresh() {
await this._onOptionsUpdatedInternal(); await this._onOptionsUpdatedInternal(true);
} }
async getOptions() { async getOptions() {
@ -73,7 +78,7 @@ class SettingsController extends EventDispatcher {
async setAllSettings(value) { async setAllSettings(value) {
const profileIndex = value.profileCurrent; const profileIndex = value.profileCurrent;
await yomichan.api.setAllSettings(value, this._source); await yomichan.api.setAllSettings(value, this._source);
this._setProfileIndex(profileIndex); this._setProfileIndex(profileIndex, true);
} }
async getSettings(targets) { async getSettings(targets) {
@ -143,21 +148,29 @@ class SettingsController extends EventDispatcher {
// Private // Private
_setProfileIndex(value) { _setProfileIndex(value, canUpdateProfileIndex) {
this._profileIndex = value; this._profileIndex = value;
this.trigger('optionsContextChanged'); this.trigger('optionsContextChanged');
this._onOptionsUpdatedInternal(); this._onOptionsUpdatedInternal(canUpdateProfileIndex);
} }
_onOptionsUpdated({source}) { _onOptionsUpdated({source}) {
if (source === this._source) { return; } if (source === this._source) { return; }
this._onOptionsUpdatedInternal(); this._onOptionsUpdatedInternal(true);
} }
async _onOptionsUpdatedInternal() { async _onOptionsUpdatedInternal(canUpdateProfileIndex) {
const optionsContext = this.getOptionsContext(); const optionsContext = this.getOptionsContext();
try {
const options = await this.getOptions(); const options = await this.getOptions();
this.trigger('optionsChanged', {options, optionsContext}); this.trigger('optionsChanged', {options, optionsContext});
} catch (e) {
if (canUpdateProfileIndex) {
this._setProfileIndex(0, false);
return;
}
throw e;
}
} }
_setupTargets(targets, extraFields) { _setupTargets(targets, extraFields) {

View File

@ -77,15 +77,13 @@ async function setupGenericSettingsController(genericSettingController) {
} }
delete document.documentElement.dataset.loadingStalled; delete document.documentElement.dataset.loadingStalled;
const optionsFull = await yomichan.api.optionsGetFull();
const preparePromises = []; const preparePromises = [];
const modalController = new ModalController(); const modalController = new ModalController();
modalController.prepare(); modalController.prepare();
const settingsController = new SettingsController(optionsFull.profileCurrent); const settingsController = new SettingsController();
settingsController.prepare(); await settingsController.prepare();
const persistentStorageController = new PersistentStorageController(); const persistentStorageController = new PersistentStorageController();
persistentStorageController.prepare(); persistentStorageController.prepare();

View File

@ -56,15 +56,13 @@ async function setupGenericSettingsController(genericSettingController) {
setupEnvironmentInfo(); setupEnvironmentInfo();
const optionsFull = await yomichan.api.optionsGetFull();
const preparePromises = []; const preparePromises = [];
const modalController = new ModalController(); const modalController = new ModalController();
modalController.prepare(); modalController.prepare();
const settingsController = new SettingsController(optionsFull.profileCurrent); const settingsController = new SettingsController();
settingsController.prepare(); await settingsController.prepare();
const dictionaryController = new DictionaryController(settingsController, modalController, statusFooter); const dictionaryController = new DictionaryController(settingsController, modalController, statusFooter);
dictionaryController.prepare(); dictionaryController.prepare();