mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
separate abort logic for commands
This commit is contained in:
27
public/scripts/slash-commands/SlashCommandAbortController.js
Normal file
27
public/scripts/slash-commands/SlashCommandAbortController.js
Normal file
@ -0,0 +1,27 @@
|
||||
export class SlashCommandAbortController {
|
||||
/**@type {SlashCommandAbortSignal}*/ signal;
|
||||
|
||||
|
||||
constructor() {
|
||||
this.signal = new SlashCommandAbortSignal();
|
||||
}
|
||||
abort(reason = 'No reason.') {
|
||||
this.signal.aborted = true;
|
||||
this.signal.reason = reason;
|
||||
}
|
||||
pause(reason = 'No reason.') {
|
||||
this.signal.paused = true;
|
||||
this.signal.reason = reason;
|
||||
}
|
||||
continue(reason = 'No reason.') {
|
||||
this.signal.paused = false;
|
||||
this.signal.reason = reason;
|
||||
}
|
||||
}
|
||||
|
||||
export class SlashCommandAbortSignal {
|
||||
/**@type {boolean}*/ paused = false;
|
||||
/**@type {boolean}*/ aborted = false;
|
||||
/**@type {string}*/ reason = null;
|
||||
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
import { substituteParams } from '../../script.js';
|
||||
import { escapeRegex } from '../utils.js';
|
||||
import { delay, escapeRegex } from '../utils.js';
|
||||
import { SlashCommandAbortController } from './SlashCommandAbortController.js';
|
||||
import { SlashCommandClosureExecutor } from './SlashCommandClosureExecutor.js';
|
||||
import { SlashCommandClosureResult } from './SlashCommandClosureResult.js';
|
||||
import { SlashCommandExecutor } from './SlashCommandExecutor.js';
|
||||
@ -14,7 +15,7 @@ export class SlashCommandClosure {
|
||||
/**@type {Object.<string,string|SlashCommandClosure>}*/ providedArguments = {};
|
||||
/**@type {SlashCommandExecutor[]}*/ executorList = [];
|
||||
/**@type {string}*/ keptText;
|
||||
/**@type {AbortController}*/ abortController;
|
||||
/**@type {SlashCommandAbortController}*/ abortController;
|
||||
/**@type {(done:number, total:number)=>void}*/ onProgress;
|
||||
|
||||
constructor(parent) {
|
||||
@ -232,15 +233,15 @@ export class SlashCommandClosure {
|
||||
;
|
||||
}
|
||||
|
||||
let abortResult = this.testAbortController();
|
||||
let abortResult = await this.testAbortController();
|
||||
if (abortResult) {
|
||||
return abortResult;
|
||||
}
|
||||
this.scope.pipe = await executor.command.callback(args, value ?? '');
|
||||
done += 0.5;
|
||||
this.onProgress?.(done, this.executorList.length);
|
||||
// eslint-disable-next-line no-cond-assign
|
||||
if (abortResult = this.testAbortController()) {
|
||||
abortResult = await this.testAbortController();
|
||||
if (abortResult) {
|
||||
return abortResult;
|
||||
}
|
||||
}
|
||||
@ -250,7 +251,13 @@ export class SlashCommandClosure {
|
||||
return result;
|
||||
}
|
||||
|
||||
testAbortController() {
|
||||
async testPaused() {
|
||||
while (!this.abortController?.signal?.aborted && this.abortController?.signal?.paused) {
|
||||
await delay(200);
|
||||
}
|
||||
}
|
||||
async testAbortController() {
|
||||
await this.testPaused();
|
||||
if (this.abortController?.signal?.aborted) {
|
||||
const result = new SlashCommandClosureResult();
|
||||
result.isAborted = true;
|
||||
|
@ -4,4 +4,6 @@ export class SlashCommandClosureResult {
|
||||
/**@type {string}*/ pipe;
|
||||
/**@type {boolean}*/ isAborted = false;
|
||||
/**@type {string}*/ abortReason;
|
||||
/**@type {boolean}*/ isError = false;
|
||||
/**@type {string}*/ errorMessage;
|
||||
}
|
||||
|
@ -30,12 +30,15 @@ export class SlashCommandParserError extends Error {
|
||||
let hint = [];
|
||||
let lines = this.text.slice(start + 1, end - 1).split('\n');
|
||||
let lineNum = this.line - lines.length + 1;
|
||||
let tabOffset = 0;
|
||||
for (const line of lines) {
|
||||
const num = `${' '.repeat(lineOffset - lineNum.toString().length)}${lineNum}`;
|
||||
lineNum++;
|
||||
hint.push(`${num}: ${line}`);
|
||||
const untabbedLine = line.replace(/\t/g, ' '.repeat(4));
|
||||
tabOffset = untabbedLine.length - line.length;
|
||||
hint.push(`${num}: ${untabbedLine}`);
|
||||
}
|
||||
hint.push(`${' '.repeat(this.index - lineStart + lineOffset + 1)}^^^^^`);
|
||||
hint.push(`${' '.repeat(this.index - 2 - lineStart + lineOffset + 1 + tabOffset)}^^^^^`);
|
||||
return hint.join('\n');
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user