subcommand and /abort fixes

- use AbortController in /abort instead of execption
- allow quiet abort
- allow loud abort
- allow abort reason
- abort when aborted in subcommand
- break out of loops when aborted inside
- fix parsing of subcommands with multiple commands
This commit is contained in:
LenAnderson
2024-05-18 14:48:31 -04:00
parent 909ec4191d
commit 87cc28ae28
5 changed files with 100 additions and 25 deletions

View File

@ -702,6 +702,19 @@ SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'addswipe',
}));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'abort',
callback: abortCallback,
namedArgumentList: [
SlashCommandNamedArgument.fromProps({ name: 'quiet',
description: 'Whether to suppress the toast message notifying about the /abort call.',
typeList: [ARGUMENT_TYPE.BOOLEAN],
defaultValue: 'true',
enumList: ['true', 'false'],
}),
],
unnamedArgumentList: [
SlashCommandArgument.fromProps({ description: 'The reason for aborting command execution. Shown when quiet=false',
typeList: [ARGUMENT_TYPE.STRING],
}),
],
helpString: 'Aborts the slash command batch execution.',
}));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'fuzzy',
@ -1419,9 +1432,15 @@ async function runCallback(args, name) {
}
}
function abortCallback() {
$('#send_textarea').val('')[0].dispatchEvent(new Event('input', { bubbles:true }));
throw new Error('/abort command executed');
/**
*
* @param {object} param0
* @param {SlashCommandAbortController} param0._abortController
* @param {string} [param0.quiet]
* @param {string} [reason]
*/
function abortCallback({ _abortController, quiet }, reason) {
_abortController.abort((reason ?? '').toString().length == 0 ? '/abort command executed' : reason, !isFalseBoolean(quiet ?? 'true'));
}
async function delayCallback(_, amount) {
@ -2782,7 +2801,7 @@ async function executeSlashCommandsWithOptions(text, options = {}) {
let closure;
try {
closure = parser.parse(text, true, options.parserFlags, options.abortController);
closure = parser.parse(text, true, options.parserFlags, options.abortController ?? new SlashCommandAbortController());
closure.scope.parent = options.scope;
closure.onProgress = options.onProgress;
} catch (e) {
@ -2809,7 +2828,7 @@ async function executeSlashCommandsWithOptions(text, options = {}) {
try {
const result = await closure.execute();
if (result.isAborted) {
if (result.isAborted && !result.isQuietlyAborted) {
toastr.warning(result.abortReason, 'Command execution aborted');
}
return result;