From ff68956371fb0ba0b3798258e360b50945656e8a Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Thu, 18 Jul 2024 22:47:57 +0300 Subject: [PATCH] Add events to SlashCommandAbortController --- .../slash-commands/AbstractEventTarget.js | 36 +++++++++++++++++++ .../SlashCommandAbortController.js | 8 ++++- 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 public/scripts/slash-commands/AbstractEventTarget.js diff --git a/public/scripts/slash-commands/AbstractEventTarget.js b/public/scripts/slash-commands/AbstractEventTarget.js new file mode 100644 index 000000000..717d1c515 --- /dev/null +++ b/public/scripts/slash-commands/AbstractEventTarget.js @@ -0,0 +1,36 @@ +/** + * @abstract + * @implements {EventTarget} + */ +export class AbstractEventTarget { + constructor() { + this.listeners = {}; + } + + addEventListener(type, callback, _options) { + if (!this.listeners[type]) { + this.listeners[type] = []; + } + this.listeners[type].push(callback); + } + + dispatchEvent(event) { + if (!this.listeners[event.type] || this.listeners[event.type].length === 0) { + return true; + } + this.listeners[event.type].forEach(listener => { + listener(event); + }); + return true; + } + + removeEventListener(type, callback, _options) { + if (!this.listeners[type]) { + return; + } + const index = this.listeners[type].indexOf(callback); + if (index !== -1) { + this.listeners[type].splice(index, 1); + } + } +} diff --git a/public/scripts/slash-commands/SlashCommandAbortController.js b/public/scripts/slash-commands/SlashCommandAbortController.js index 7ed919f8c..b55e77ae6 100644 --- a/public/scripts/slash-commands/SlashCommandAbortController.js +++ b/public/scripts/slash-commands/SlashCommandAbortController.js @@ -1,22 +1,28 @@ -export class SlashCommandAbortController { +import { AbstractEventTarget } from './AbstractEventTarget.js'; + +export class SlashCommandAbortController extends AbstractEventTarget { /**@type {SlashCommandAbortSignal}*/ signal; constructor() { + super(); this.signal = new SlashCommandAbortSignal(); } abort(reason = 'No reason.', isQuiet = false) { this.signal.isQuiet = isQuiet; this.signal.aborted = true; this.signal.reason = reason; + this.dispatchEvent(new Event('abort')); } pause(reason = 'No reason.') { this.signal.paused = true; this.signal.reason = reason; + this.dispatchEvent(new Event('pause')); } continue(reason = 'No reason.') { this.signal.paused = false; this.signal.reason = reason; + this.dispatchEvent(new Event('continue')); } }