Sanity-check decaffeinate class Events

pull/1441/head
Simon Legner 1 year ago
parent 04d167c9e0
commit 3276b2637f

@ -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 { class Events {
on(event, callback) { on(event, callback) {
if (event.indexOf(" ") >= 0) { if (event.indexOf(" ") >= 0) {
for (var name of Array.from(event.split(" "))) { for (var name of event.split(" ")) {
this.on(name, callback); this.on(name, callback);
} }
} else { } else {
let base; this._callbacks ||= {};
((base = this._callbacks[event] ||= [];
this._callbacks != null ? this._callbacks : (this._callbacks = {}))[ this._callbacks[event].push(callback);
event
] != null
? base[event]
: (base[event] = [])
).push(callback);
} }
return this; return this;
} }
@ -31,12 +15,11 @@ class Events {
off(event, callback) { off(event, callback) {
let callbacks, index; let callbacks, index;
if (event.indexOf(" ") >= 0) { if (event.indexOf(" ") >= 0) {
for (var name of Array.from(event.split(" "))) { for (var name of event.split(" ")) {
this.off(name, callback); this.off(name, callback);
} }
} else if ( } else if (
(callbacks = (callbacks = this._callbacks?.[event]) &&
this._callbacks != null ? this._callbacks[event] : undefined) &&
(index = callbacks.indexOf(callback)) >= 0 (index = callbacks.indexOf(callback)) >= 0
) { ) {
callbacks.splice(index, 1); callbacks.splice(index, 1);
@ -51,7 +34,7 @@ class Events {
this.eventInProgress = { name: event, args }; this.eventInProgress = { name: event, args };
const callbacks = this._callbacks?.[event]; const callbacks = this._callbacks?.[event];
if (callbacks) { if (callbacks) {
for (let callback of Array.from(callbacks.slice(0))) { for (let callback of callbacks.slice(0)) {
if (typeof callback === "function") { if (typeof callback === "function") {
callback(...args); callback(...args);
} }
@ -59,14 +42,14 @@ class Events {
} }
this.eventInProgress = null; this.eventInProgress = null;
if (event !== "all") { if (event !== "all") {
this.trigger("all", event, ...Array.from(args)); this.trigger("all", event, ...args);
} }
return this; return this;
} }
removeEvent(event) { removeEvent(event) {
if (this._callbacks != null) { if (this._callbacks != null) {
for (var name of Array.from(event.split(" "))) { for (var name of event.split(" ")) {
delete this._callbacks[name]; delete this._callbacks[name];
} }
} }

Loading…
Cancel
Save