Blame Raw
Cohee · e3f41666 · · 78 lines (2.4 KB)
1 contributor
1import { SlashCommandClosure } from './SlashCommandClosure.js';
2import { SlashCommandExecutor } from './SlashCommandExecutor.js';
3
4export class SlashCommandDebugController {
5 /** @type {SlashCommandClosure[]} */ stack = [];
6 /** @type {SlashCommandExecutor[]} */ cmdStack = [];
7 /** @type {boolean[]} */ stepStack = [];
8 /** @type {boolean} */ isStepping = false;
9 /** @type {boolean} */ isSteppingInto = false;
10 /** @type {boolean} */ isSteppingOut = false;
11
12 /** @type {object} */ namedArguments;
13 /** @type {string|SlashCommandClosure|(string|SlashCommandClosure)[]} */ unnamedArguments;
14
15 /** @type {Promise<boolean>} */ continuePromise;
16 /** @type {(boolean)=>void} */ continueResolver;
17
18 /** @type {(closure:SlashCommandClosure, executor:SlashCommandExecutor)=>Promise<boolean>} */ onBreakPoint;
19
20
21 testStepping(closure) {
22 return this.stepStack[this.stack.indexOf(closure)];
23 }
24
25
26 down(closure) {
27 this.stack.push(closure);
28 if (this.stepStack.length < this.stack.length) {
29 this.stepStack.push(this.isSteppingInto);
30 }
31 }
32 up() {
33 this.stack.pop();
34 while (this.cmdStack.length > this.stack.length) this.cmdStack.pop();
35 this.stepStack.pop();
36 }
37
38 setExecutor(executor) {
39 this.cmdStack[this.stack.length - 1] = executor;
40 }
41
42
43 resume() {
44 this.continueResolver?.(false);
45 this.continuePromise = null;
46 this.stepStack.forEach((_, idx) => this.stepStack[idx] = false);
47 }
48 step() {
49 this.stepStack.forEach((_, idx) => this.stepStack[idx] = true);
50 this.continueResolver?.(true);
51 this.continuePromise = null;
52 }
53 stepInto() {
54 this.isSteppingInto = true;
55 this.stepStack.forEach((_, idx) => this.stepStack[idx] = true);
56 this.continueResolver?.(true);
57 this.continuePromise = null;
58 }
59 stepOut() {
60 this.isSteppingOut = true;
61 this.stepStack[this.stepStack.length - 1] = false;
62 this.continueResolver?.(false);
63 this.continuePromise = null;
64 }
65
66 async awaitContinue() {
67 this.continuePromise ??= new Promise(resolve => {
68 this.continueResolver = resolve;
69 });
70 this.isStepping = await this.continuePromise;
71 return this.isStepping;
72 }
73
74 async awaitBreakPoint(closure, executor) {
75 this.isStepping = await this.onBreakPoint(closure, executor);
76 return this.isStepping;
77 }
78}