REFACTOR: Move `app-events:main` to `service:app-events` (#8152)

REFACTOR: Move app-events:main to service:app-events (#8152)

AppEvents was always a service object in disguise, so we should move it to the correct place in the application. Doing this allows other service objects to inject it easily without container access.

In the future we should also deprecate this.appEvents without an explicit injection too.

diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js
index 180df64..033e043 100644
--- a/app/assets/javascripts/application.js
+++ b/app/assets/javascripts/application.js
@@ -19,7 +19,7 @@
 //= require ./discourse/lib/hash
 //= require ./discourse/lib/load-script
 //= require ./discourse/lib/notification-levels
-//= require ./discourse/lib/app-events
+//= require ./discourse/services/app-events
 //= require ./discourse/lib/offset-calculator
 //= require ./discourse/lib/lock-on
 //= require ./discourse/lib/url
diff --git a/app/assets/javascripts/discourse-common/resolver.js.es6 b/app/assets/javascripts/discourse-common/resolver.js.es6
index 1b9dab0..aeab2d4 100644
--- a/app/assets/javascripts/discourse-common/resolver.js.es6
+++ b/app/assets/javascripts/discourse-common/resolver.js.es6
@@ -1,4 +1,5 @@
 import { findHelper } from "discourse-common/lib/helpers";
+import deprecated from "discourse-common/lib/deprecated";
 
 /* global requirejs, require */
 var classify = Ember.String.classify;
@@ -45,6 +46,14 @@ export function buildResolver(baseName) {
     },
 
     normalize(fullName) {
+      if (fullName === "app-events:main") {
+        deprecated(
+          "`app-events:main` has been replaced with `service:app-events`",
+          { since: "2.4.0" }
+        );
+        return "service:app-events";
+      }
+
       const split = fullName.split(":");
       if (split.length > 1) {
         const appBase = `${baseName}/${split[0]}s/`;
diff --git a/app/assets/javascripts/discourse/initializers/avatar-select.js.es6 b/app/assets/javascripts/discourse/initializers/avatar-select.js.es6
index f35ee67..9eaac83 100644
--- a/app/assets/javascripts/discourse/initializers/avatar-select.js.es6
+++ b/app/assets/javascripts/discourse/initializers/avatar-select.js.es6
@@ -10,7 +10,7 @@ export default {
     ).selectable_avatars_enabled;
 
     container
-      .lookup("app-events:main")
+      .lookup("service:app-events")
       .on("show-avatar-select", this, "_showAvatarSelect");
   },
 
diff --git a/app/assets/javascripts/discourse/initializers/badging.js.es6 b/app/assets/javascripts/discourse/initializers/badging.js.es6
index b80416c..0047640 100644
--- a/app/assets/javascripts/discourse/initializers/badging.js.es6
+++ b/app/assets/javascripts/discourse/initializers/badging.js.es6
@@ -13,7 +13,7 @@ export default {
       user.unread_notifications + user.unread_private_messages;
 
     container
-      .lookup("app-events:main")
+      .lookup("service:app-events")
       .on("notifications:changed", this, "_updateBadge");
   },
 
diff --git a/app/assets/javascripts/discourse/initializers/page-tracking.js.es6 b/app/assets/javascripts/discourse/initializers/page-tracking.js.es6
index f11760e..71fa6f2 100644
--- a/app/assets/javascripts/discourse/initializers/page-tracking.js.es6
+++ b/app/assets/javascripts/discourse/initializers/page-tracking.js.es6
@@ -17,7 +17,7 @@ export default {
     router.on("routeWillChange", viewTrackingRequired);
     router.on("routeDidChange", cleanDOM);
 
-    let appEvents = container.lookup("app-events:main");
+    let appEvents = container.lookup("service:app-events");
 
     startPageTracking(router, appEvents);
 
diff --git a/app/assets/javascripts/discourse/initializers/subscribe-user-notifications.js.es6 b/app/assets/javascripts/discourse/initializers/subscribe-user-notifications.js.es6
index b71f37a..d3c251b 100644
--- a/app/assets/javascripts/discourse/initializers/subscribe-user-notifications.js.es6
+++ b/app/assets/javascripts/discourse/initializers/subscribe-user-notifications.js.es6
@@ -18,7 +18,7 @@ export default {
   initialize(container) {
     const user = container.lookup("current-user:main");
     const bus = container.lookup("message-bus:main");
-    const appEvents = container.lookup("app-events:main");
+    const appEvents = container.lookup("service:app-events");
 
     if (user) {
       bus.subscribe("/reviewable_counts", data => {
diff --git a/app/assets/javascripts/discourse/initializers/title-notifications.js.es6 b/app/assets/javascripts/discourse/initializers/title-notifications.js.es6
index ca83d7a..9801e1f 100644
--- a/app/assets/javascripts/discourse/initializers/title-notifications.js.es6
+++ b/app/assets/javascripts/discourse/initializers/title-notifications.js.es6
@@ -9,7 +9,7 @@ export default {
     this.container = container;
 
     container
-      .lookup("app-events:main")
+      .lookup("service:app-events")
       .on("notifications:changed", this, "_updateTitle");
   },
 
diff --git a/app/assets/javascripts/discourse/lib/app-events.js.es6 b/app/assets/javascripts/discourse/lib/app-events.js.es6
deleted file mode 100644
index aff71a2..0000000
--- a/app/assets/javascripts/discourse/lib/app-events.js.es6
+++ /dev/null
@@ -1,48 +0,0 @@
-import deprecated from "discourse-common/lib/deprecated";
-
-export default Ember.Object.extend(Ember.Evented, {
-  _events: {},
-
-  on() {
-    if (arguments.length === 2) {
-      let [name, fn] = arguments;
-      let target = {};
-      this._events[name] = this._events[name] || [];
-      this._events[name].push({ target, fn });
-
-      this._super(name, target, fn);
-    } else if (arguments.length === 3) {
-      let [name, target, fn] = arguments;
-      this._events[name] = this._events[name] || [];
-      this._events[name].push({ target, fn });
-
-      this._super(...arguments);
-    }
-    return this;
-  },
-
-  off() {
-    let name = arguments[0];
-    let fn = arguments[2];
-
-    if (this._events[name]) {
-      if (arguments.length === 1) {
-        deprecated(
-          "Removing all event listeners at once is deprecated, please remove each listener individually."
-        );
-
-        this._events[name].forEach(ref => {
-          this._super(name, ref.target, ref.fn);
-        });
-        delete this._events[name];
-      } else if (arguments.length === 3) {
-        this._super(...arguments);
-
-        this._events[name] = this._events[name].filter(e => e.fn !== fn);
-        if (this._events[name].length === 0) delete this._events[name];
-      }
-    }
-
-    return this;
-  }
-});
diff --git a/app/assets/javascripts/discourse/lib/clean-dom.js.es6 b/app/assets/javascripts/discourse/lib/clean-dom.js.es6
index 5915718..8d83f4d 100644
--- a/app/assets/javascripts/discourse/lib/clean-dom.js.es6
+++ b/app/assets/javascripts/discourse/lib/clean-dom.js.es6
@@ -30,7 +30,7 @@ function _clean() {
   }
 
   // TODO: Avoid container lookup here
-  const appEvents = Discourse.__container__.lookup("app-events:main");
+  const appEvents = Discourse.__container__.lookup("service:app-events");
   appEvents.trigger("dom:clean");
 }
 
diff --git a/app/assets/javascripts/discourse/lib/keyboard-shortcuts.js.es6 b/app/assets/javascripts/discourse/lib/keyboard-shortcuts.js.es6
index 664a1cb..e051f47 100644
--- a/app/assets/javascripts/discourse/lib/keyboard-shortcuts.js.es6
+++ b/app/assets/javascripts/discourse/lib/keyboard-shortcuts.js.es6
@@ -86,7 +86,7 @@ export default {
     this._stopCallback();
 
     this.searchService = this.container.lookup("search-service:main");
-    this.appEvents = this.container.lookup("app-events:main");
+    this.appEvents = this.container.lookup("service:app-events");
     this.currentUser = this.container.lookup("current-user:main");
     let siteSettings = this.container.lookup("site-settings:main");
 
diff --git a/app/assets/javascripts/discourse/lib/plugin-api.js.es6 b/app/assets/javascripts/discourse/lib/plugin-api.js.es6
index 950fdcd..4cf670b 100644
--- a/app/assets/javascripts/discourse/lib/plugin-api.js.es6
+++ b/app/assets/javascripts/discourse/lib/plugin-api.js.es6
@@ -449,7 +449,7 @@ class PluginApi {
     `‍``
   **/
   onAppEvent(name, fn) {
-    const appEvents = this._lookupContainer("app-events:main");
+    const appEvents = this._lookupContainer("service:app-events");
     appEvents && appEvents.on(name, fn);
   }
 

[... diff too long, it was truncated ...]

GitHub sha: f5d391a4

1 Like