Add events to SlashCommandAbortController
This commit is contained in:
parent
e6995e40cc
commit
ff68956371
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,22 +1,28 @@
|
||||||
export class SlashCommandAbortController {
|
import { AbstractEventTarget } from './AbstractEventTarget.js';
|
||||||
|
|
||||||
|
export class SlashCommandAbortController extends AbstractEventTarget {
|
||||||
/**@type {SlashCommandAbortSignal}*/ signal;
|
/**@type {SlashCommandAbortSignal}*/ signal;
|
||||||
|
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
|
super();
|
||||||
this.signal = new SlashCommandAbortSignal();
|
this.signal = new SlashCommandAbortSignal();
|
||||||
}
|
}
|
||||||
abort(reason = 'No reason.', isQuiet = false) {
|
abort(reason = 'No reason.', isQuiet = false) {
|
||||||
this.signal.isQuiet = isQuiet;
|
this.signal.isQuiet = isQuiet;
|
||||||
this.signal.aborted = true;
|
this.signal.aborted = true;
|
||||||
this.signal.reason = reason;
|
this.signal.reason = reason;
|
||||||
|
this.dispatchEvent(new Event('abort'));
|
||||||
}
|
}
|
||||||
pause(reason = 'No reason.') {
|
pause(reason = 'No reason.') {
|
||||||
this.signal.paused = true;
|
this.signal.paused = true;
|
||||||
this.signal.reason = reason;
|
this.signal.reason = reason;
|
||||||
|
this.dispatchEvent(new Event('pause'));
|
||||||
}
|
}
|
||||||
continue(reason = 'No reason.') {
|
continue(reason = 'No reason.') {
|
||||||
this.signal.paused = false;
|
this.signal.paused = false;
|
||||||
this.signal.reason = reason;
|
this.signal.reason = reason;
|
||||||
|
this.dispatchEvent(new Event('continue'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue