add aborting command execution

This commit is contained in:
LenAnderson
2024-04-27 09:11:54 -04:00
parent c8880a32f9
commit eee0504ff4
5 changed files with 77 additions and 31 deletions

View File

@ -7,13 +7,14 @@ import { SlashCommandScope } from './SlashCommandScope.js';
export class SlashCommandClosure {
/**@type {SlashCommandScope}*/ scope;
/**@type {Boolean}*/ executeNow = false;
/**@type {boolean}*/ executeNow = false;
// @ts-ignore
/**@type {Object.<string,string|SlashCommandClosure>}*/ arguments = {};
// @ts-ignore
/**@type {Object.<string,string|SlashCommandClosure>}*/ providedArguments = {};
/**@type {SlashCommandExecutor[]}*/ executorList = [];
/**@type {String}*/ keptText;
/**@type {string}*/ keptText;
/**@type {AbortController}*/ abortController;
constructor(parent) {
this.scope = new SlashCommandScope(parent);
@ -77,6 +78,7 @@ export class SlashCommandClosure {
closure.providedArguments = this.providedArguments;
closure.executorList = this.executorList;
closure.keptText = this.keptText;
closure.abortController = this.abortController;
return closure;
}
@ -225,11 +227,29 @@ export class SlashCommandClosure {
;
}
let abortResult;
// eslint-disable-next-line no-cond-assign
if (abortResult = this.testAbortController()) {
return abortResult;
}
this.scope.pipe = await executor.command.callback(args, value ?? '');
// eslint-disable-next-line no-cond-assign
if (abortResult = this.testAbortController()) {
return abortResult;
}
}
}
/**@type {SlashCommandClosureResult} */
const result = Object.assign(new SlashCommandClosureResult(), { interrupt, newText: this.keptText, pipe: this.scope.pipe });
return result;
}
testAbortController() {
if (this.abortController?.signal?.aborted) {
const result = new SlashCommandClosureResult();
result.isAborted = true;
result.abortReason = this.abortController.signal.reason.toString();
return result;
}
}
}

View File

@ -1,5 +1,7 @@
export class SlashCommandClosureResult {
/**@type {Boolean}*/ interrupt = false;
/**@type {String}*/ newText = '';
/**@type {String}*/ pipe;
/**@type {boolean}*/ interrupt = false;
/**@type {string}*/ newText = '';
/**@type {string}*/ pipe;
/**@type {boolean}*/ isAborted = false;
/**@type {string}*/ abortReason;
}

View File

@ -87,6 +87,7 @@ export class SlashCommandParser {
/**@type {string}*/ text;
/**@type {string}*/ keptText;
/**@type {number}*/ index;
/**@type {AbortController}*/ abortController;
/**@type {SlashCommandScope}*/ scope;
/**@type {SlashCommandClosure}*/ closure;
@ -539,11 +540,12 @@ export class SlashCommandParser {
}
parse(text, verifyCommandNames = true, flags = null) {
parse(text, verifyCommandNames = true, flags = null, abortController = null) {
this.verifyCommandNames = verifyCommandNames;
for (const key of Object.keys(PARSER_FLAG)) {
this.flags[PARSER_FLAG[key]] = flags?.[PARSER_FLAG[key]] ?? power_user.stscript.parser.flags[PARSER_FLAG[key]] ?? false;
}
this.abortController = abortController;
this.text = `{:${text}:}`;
this.keptText = '';
this.index = 0;
@ -569,6 +571,7 @@ export class SlashCommandParser {
let injectPipe = true;
this.take(2); // discard opening {:
let closure = new SlashCommandClosure(this.scope);
closure.abortController = this.abortController;
this.scope = closure.scope;
this.closure = closure;
this.discardWhitespace();