Add events to SlashCommandAbortController

ff68956371fb0ba0b3798258e360b50945656e8a

Cohee <18619528+Cohee1207@users.noreply.github.com>

2 files changed, +43 -1Ignore whitespace
public/scripts/slash-commands/AbstractEventTarget.js+36 -0
@@ -0,0 +1,36 @@
1+/**
2+ * @abstract
3+ * @implements {EventTarget}
4+ */
5+export class AbstractEventTarget {
6+ constructor() {
7+ this.listeners = {};
8+ }
9+
10+ addEventListener(type, callback, _options) {
11+ if (!this.listeners[type]) {
12+ this.listeners[type] = [];
13+ }
14+ this.listeners[type].push(callback);
15+ }
16+
17+ dispatchEvent(event) {
18+ if (!this.listeners[event.type] || this.listeners[event.type].length === 0) {
19+ return true;
20+ }
21+ this.listeners[event.type].forEach(listener => {
22+ listener(event);
23+ });
24+ return true;
25+ }
26+
27+ removeEventListener(type, callback, _options) {
28+ if (!this.listeners[type]) {
29+ return;
30+ }
31+ const index = this.listeners[type].indexOf(callback);
32+ if (index !== -1) {
33+ this.listeners[type].splice(index, 1);
34+ }
35+ }
36+}
public/scripts/slash-commands/SlashCommandAbortController.js+7 -1
@@ -1,22 +1,28 @@
1-export class SlashCommandAbortController {
1+import { AbstractEventTarget } from './AbstractEventTarget.js';
2+
3+export class SlashCommandAbortController extends AbstractEventTarget {
24 /**@type {SlashCommandAbortSignal}*/ signal;
35
46
57 constructor() {
8+ super();
69 this.signal = new SlashCommandAbortSignal();
710 }
811 abort(reason = 'No reason.', isQuiet = false) {
912 this.signal.isQuiet = isQuiet;
1013 this.signal.aborted = true;
1114 this.signal.reason = reason;
15+ this.dispatchEvent(new Event('abort'));
1216 }
1317 pause(reason = 'No reason.') {
1418 this.signal.paused = true;
1519 this.signal.reason = reason;
20+ this.dispatchEvent(new Event('pause'));
1621 }
1722 continue(reason = 'No reason.') {
1823 this.signal.paused = false;
1924 this.signal.reason = reason;
25+ this.dispatchEvent(new Event('continue'));
2026 }
2127}
2228