mirror of
https://github.com/discourse/discourse.git
synced 2024-11-22 17:06:31 -06:00
DEV: encapsulates _events and clearing it at app-events module level (#9489)
This commit is contained in:
parent
c9cdd12e97
commit
1eb9bc4cec
@ -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];
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user