mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
add aborting command execution
This commit is contained in:
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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();
|
||||
|
Reference in New Issue
Block a user