History

Sanity-check decaffeinate class Events

Simon Legner committed 2 years ago

Commit 3276b261 parent 04d167c1 file · +9 −26Browse files

Unified · Split

assets/javascripts/lib/events.js+9−26
@@ -1,29 +1,13 @@
1−// TODO: This file was created by bulk-decaffeinate.
2−// Sanity-check the conversion and remove this comment.
3−/*
4− * decaffeinate suggestions:
5− * DS101: Remove unnecessary use of Array.from
6− * DS102: Remove unnecessary code created because of implicit returns
7− * DS104: Avoid inline assignments
8− * DS207: Consider shorter variations of null checks
9− * DS208: Avoid top-level this
10− * Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md
11− */
121 class Events {
132 on(event, callback) {
143 if (event.indexOf(" ") >= 0) {
15− for (var name of Array.from(event.split(" "))) {
4+ for (var name of event.split(" ")) {
165 this.on(name, callback);
176 }
187 } else {
19− let base;
20− ((base =
21− this._callbacks != null ? this._callbacks : (this._callbacks = {}))[
22− event
23− ] != null
24− ? base[event]
25− : (base[event] = [])
26− ).push(callback);
8+ this._callbacks ||= {};
9+ this._callbacks[event] ||= [];
10+ this._callbacks[event].push(callback);
2711 }
2812 return this;
2913 }
@@ -31,12 +15,11 @@ class Events { · 1 unmodified line
3115 off(event, callback) {
3216 let callbacks, index;
3317 if (event.indexOf(" ") >= 0) {
34− for (var name of Array.from(event.split(" "))) {
18+ for (var name of event.split(" ")) {
3519 this.off(name, callback);
3620 }
3721 } else if (
38− (callbacks =
39− this._callbacks != null ? this._callbacks[event] : undefined) &&
22+ (callbacks = this._callbacks?.[event]) &&
4023 (index = callbacks.indexOf(callback)) >= 0
4124 ) {
4225 callbacks.splice(index, 1);
@@ -51,7 +34,7 @@ class Events { · 8 unmodified lines
5134 this.eventInProgress = { name: event, args };
5235 const callbacks = this._callbacks?.[event];
5336 if (callbacks) {
54− for (let callback of Array.from(callbacks.slice(0))) {
37+ for (let callback of callbacks.slice(0)) {
5538 if (typeof callback === "function") {
5639 callback(...args);
5740 }
@@ -59,14 +42,14 @@ class Events { · 1 unmodified line
5942 }
6043 this.eventInProgress = null;
6144 if (event !== "all") {
62− this.trigger("all", event, ...Array.from(args));
45+ this.trigger("all", event, ...args);
6346 }
6447 return this;
6548 }
6649
6750 removeEvent(event) {
6851 if (this._callbacks != null) {
69− for (var name of Array.from(event.split(" "))) {
52+ for (var name of event.split(" ")) {
7053 delete this._callbacks[name];
7154 }
7255 }