Merge pull request #2686 from SillyTavern/fix-extensions-toggle Wait for settings before reloading after toggling extensions

3167c85791b2ebf8708e45ebff6aa9b6fe4a8dfc

Wolfsblvt <wolfsblvt@gmail.com>

Signed
2 files changed, +24 -8Ignore whitespace
public/scripts/extensions.js+17 -1
@@ -21,6 +21,7 @@ const defaultUrl = 'http://localhost:5100';
2121let saveMetadataTimeout = null;
2222
2323let requiresReload = false;
24+let stateChanged = false;
2425
2526export function saveMetadataDebounced() {
2627 const context = getContext();
@@ -238,6 +239,7 @@ function onEnableExtensionClick() {
238239
239240async function enableExtension(name, reload = true) {
240241 extension_settings.disabledExtensions = extension_settings.disabledExtensions.filter(x => x !== name);
242+ stateChanged = true;
241243 await saveSettings();
242244 if (reload) {
243245 location.reload();
@@ -248,6 +250,7 @@ async function enableExtension(name, reload = true) {
248250
249251async function disableExtension(name, reload = true) {
250252 extension_settings.disabledExtensions.push(name);
253+ stateChanged = true;
251254 await saveSettings();
252255 if (reload) {
253256 location.reload();
@@ -657,7 +660,20 @@ async function showExtensionsDetails() {
657660 await oldPopup.complete(POPUP_RESULT.CANCELLED);
658661 }
659662
660663 const popup = new Popup(html, POPUP_TYPE.TEXT, '', { okButton: 'Close', wide: true, large: true, customButtons: [updateAllButton], allowVerticalScrolling: true });
664+ okButton: 'Close',
665+ wide: true,
666+ large: true,
667+ customButtons: [updateAllButton],
668+ allowVerticalScrolling: true,
669+ onClosing: async () => {
670+ if (stateChanged) {
671+ toastr.info('The page will be reloaded shortly...', 'Extensions state changed');
672+ await saveSettings();
673+ }
674+ return true;
675+ },
676+ });
661677 popupPromise = popup.show();
662678 } catch (error) {
663679 toastr.error('Error loading extensions. See browser console for details.');
public/scripts/popup.js+7 -7
@@ -40,8 +40,8 @@ export const POPUP_RESULT = {
4040 * @property {POPUP_RESULT|number?} [defaultResult=POPUP_RESULT.AFFIRMATIVE] - The default result of this popup when Enter is pressed. Can be changed from `POPUP_RESULT.AFFIRMATIVE`.
4141 * @property {CustomPopupButton[]|string[]?} [customButtons=null] - Custom buttons to add to the popup. If only strings are provided, the buttons will be added with default options, and their result will be in order from `2` onward.
4242 * @property {CustomPopupInput[]?} [customInputs=null] - Custom inputs to add to the popup. The display below the content and the input box, one by one.
4343 * @property {(popup: Popup) => Promise<boolean?>|boolean?} [onClosing=null] - Handler called before the popup closes, return `false` to cancel the close
4444 * @property {(popup: Popup) => Promise<void?>|void?} [onClose=null] - Handler called after the popup closes, but before the DOM is cleaned up
4545 * @property {number?} [cropAspect=null] - Aspect ratio for the crop popup
4646 * @property {string?} [cropImage=null] - Image URL to display in the crop popup
4747 */
@@ -138,8 +138,8 @@ export class Popup {
138138 /** @readonly @type {CustomPopupButton[]|string[]?} */ customButtons;
139139 /** @readonly @type {CustomPopupInput[]} */ customInputs;
140140
141141 /** @type {(popup: Popup) => Promise<boolean?>|boolean?} */ onClosing;
142142 /** @type {(popup: Popup) => Promise<void?>|void?} */ onClose;
143143
144144 /** @type {POPUP_RESULT|number} */ result;
145145 /** @type {any} */ value;
@@ -509,7 +509,7 @@ export class Popup {
509509 this.result = result;
510510
511511 if (this.onClosing) {
512512 const shouldClose = await this.onClosing(this);
513513 if (!shouldClose) {
514514 this.#isClosingPrevented = true;
515515 // Set values back if we cancel out of closing the popup
@@ -547,13 +547,13 @@ export class Popup {
547547 fixToastrForDialogs();
548548
549549 // After the dialog is actually completely closed, remove it from the DOM
550550 runAfterAnimation(this.dlg, async () => {
551551 // Call the close on the dialog
552552 this.dlg.close();
553553
554554 // Run a possible custom handler right before DOM removal
555555 if (this.onClose) {
556556 await this.onClose(this);
557557 }
558558
559559 // Remove it from the dom