Sanity-check decaffeinate class Events

This commit is contained in:
Simon Legner 2024-01-06 15:56:40 +01:00
parent 04d167c9e0
commit 3276b2637f

View file

@ -1,29 +1,13 @@
// TODO: This file was created by bulk-decaffeinate.
// Sanity-check the conversion and remove this comment.
/*
* decaffeinate suggestions:
* DS101: Remove unnecessary use of Array.from
* DS102: Remove unnecessary code created because of implicit returns
* DS104: Avoid inline assignments
* DS207: Consider shorter variations of null checks
* DS208: Avoid top-level this
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md
*/
class Events {
on(event, callback) {
if (event.indexOf(" ") >= 0) {
for (var name of Array.from(event.split(" "))) {
for (var name of event.split(" ")) {
this.on(name, callback);
}
} else {
let base;
((base =
this._callbacks != null ? this._callbacks : (this._callbacks = {}))[
event
] != null
? base[event]
: (base[event] = [])
).push(callback);
this._callbacks ||= {};
this._callbacks[event] ||= [];
this._callbacks[event].push(callback);
}
return this;
}
@ -31,12 +15,11 @@ class Events {
off(event, callback) {
let callbacks, index;
if (event.indexOf(" ") >= 0) {
for (var name of Array.from(event.split(" "))) {
for (var name of event.split(" ")) {
this.off(name, callback);
}
} else if (
(callbacks =
this._callbacks != null ? this._callbacks[event] : undefined) &&
(callbacks = this._callbacks?.[event]) &&
(index = callbacks.indexOf(callback)) >= 0
) {
callbacks.splice(index, 1);
@ -51,7 +34,7 @@ class Events {
this.eventInProgress = { name: event, args };
const callbacks = this._callbacks?.[event];
if (callbacks) {
for (let callback of Array.from(callbacks.slice(0))) {
for (let callback of callbacks.slice(0)) {
if (typeof callback === "function") {
callback(...args);
}
@ -59,14 +42,14 @@ class Events {
}
this.eventInProgress = null;
if (event !== "all") {
this.trigger("all", event, ...Array.from(args));
this.trigger("all", event, ...args);
}
return this;
}
removeEvent(event) {
if (this._callbacks != null) {
for (var name of Array.from(event.split(" "))) {
for (var name of event.split(" ")) {
delete this._callbacks[name];
}
}