DEV: encapsulates _events and clearing it at app-events module level (#9489)

DEV: encapsulates _events and clearing it at app-events module level (#9489)

diff --git a/app/assets/javascripts/discourse/services/app-events.js b/app/assets/javascripts/discourse/services/app-events.js
index 8a3e770..8d8761f 100644
--- a/app/assets/javascripts/discourse/services/app-events.js
+++ b/app/assets/javascripts/discourse/services/app-events.js
@@ -2,21 +2,37 @@ import deprecated from "discourse-common/lib/deprecated";
 import Evented from "@ember/object/evented";
 import Service from "@ember/service";
 
-export default Service.extend(Evented, {
-  _events: {},
+let _events = {};
+
+export function clearAppEventsCache(container) {
+  if (container) {
+    const appEvents = container.lookup("service:app-events");
+    Object.keys(_events).forEach(eventKey => {
+      const event = _events[eventKey];
+      event.forEach(listener => {
+        if (appEvents.has(eventKey)) {
+          appEvents.off(eventKey, listener.target, listener.fn);
+        }
+      });
+    });
+  }
 
+  _events = {};
+}
+
+export default Service.extend(Evented, {
   on() {
     if (arguments.length === 2) {
       let [name, fn] = arguments;
       let target = {};
-      this._events[name] = this._events[name] || [];
-      this._events[name].push({ target, fn });
+      _events[name] = _events[name] || [];
+      _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 });
+      _events[name] = _events[name] || [];
+      _events[name].push({ target, fn });
 
       this._super(...arguments);
     }
@@ -27,21 +43,21 @@ export default Service.extend(Evented, {
     let name = arguments[0];
     let fn = arguments[2];
 
-    if (this._events[name]) {
+    if (_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 => {
+        _events[name].forEach(ref => {
           this._super(name, ref.target, ref.fn);
         });
-        delete this._events[name];
+        delete _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];
+        _events[name] = _events[name].filter(e => e.fn !== fn);
+        if (_events[name].length === 0) delete _events[name];
       }
     }
 
diff --git a/test/javascripts/test_helper.js b/test/javascripts/test_helper.js
index 38afc19..d15cd7c 100644
--- a/test/javascripts/test_helper.js
+++ b/test/javascripts/test_helper.js
@@ -189,16 +189,9 @@ QUnit.testDone(function() {
   // ensures any event not removed is not leaking between tests
   // most likely in intialisers, other places (controller, component...)
   // should be fixed in code
-  var appEvents = window.Discourse.__container__.lookup("service:app-events");
-  var events = appEvents.__proto__._events;
-  Object.keys(events).forEach(function(eventKey) {
-    var event = events[eventKey];
-    event.forEach(function(listener) {
-      if (appEvents.has(eventKey)) {
-        appEvents.off(eventKey, listener.target, listener.fn);
-      }
-    });
-  });
+  require("discourse/services/app-events").clearAppEventsCache(
+    window.Discourse.__container__
+  );
 
   window.MessageBus.unsubscribe("*");
   delete window.server;

GitHub sha: 1eb9bc4c

This commit appears in #9489 which was approved by eviltrout. It was merged by jjaffeux.