mirror of
https://github.com/discourse/discourse.git
synced 2025-01-19 03:32:45 +08:00
DEV: Drop support for removing all appEvent listeners (#14936)
Also removes "appEventsCache". (and reduces the reported test memory usage by ~33%) There's no longer any need to remove appEvent listeners in application-instance initializers' `teardown`, as app instances are recreated before each test (in both legacy and ember cli envs)
This commit is contained in:
parent
f0d963faad
commit
fc3a6e57e3
|
@ -4,15 +4,11 @@ export default {
|
||||||
name: "edit-notification-clicks-tracker",
|
name: "edit-notification-clicks-tracker",
|
||||||
|
|
||||||
initialize(container) {
|
initialize(container) {
|
||||||
this.appEvents = container.lookup("service:app-events");
|
const appEvents = container.lookup("service:app-events");
|
||||||
this.appEvents.on("edit-notification:clicked", this, this.handleClick);
|
appEvents.on("edit-notification:clicked", this.handleClick);
|
||||||
},
|
},
|
||||||
|
|
||||||
handleClick({ topicId, postNumber, revisionNumber }) {
|
handleClick({ topicId, postNumber, revisionNumber }) {
|
||||||
setLastEditNotificationClick(topicId, postNumber, revisionNumber);
|
setLastEditNotificationClick(topicId, postNumber, revisionNumber);
|
||||||
},
|
},
|
||||||
|
|
||||||
teardown() {
|
|
||||||
this.appEvents.off("edit-notification:clicked", this, this.handleClick);
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -6,21 +6,17 @@ export default {
|
||||||
initialize(container) {
|
initialize(container) {
|
||||||
// workaround for Safari on iOS 14.3
|
// workaround for Safari on iOS 14.3
|
||||||
// seems it has started using opengraph tags when sharing
|
// seems it has started using opengraph tags when sharing
|
||||||
this.appEvents = container.lookup("service:app-events");
|
const ogTitle = document.querySelector("meta[property='og:title']");
|
||||||
this.ogTitle = document.querySelector("meta[property='og:title']");
|
const ogUrl = document.querySelector("meta[property='og:url']");
|
||||||
this.ogUrl = document.querySelector("meta[property='og:url']");
|
|
||||||
|
|
||||||
if (this.ogTitle && this.ogUrl) {
|
if (!ogTitle || !ogUrl) {
|
||||||
this.appEvents.on("page:changed", this, this.updateOgAttributes);
|
return;
|
||||||
}
|
}
|
||||||
},
|
|
||||||
|
|
||||||
updateOgAttributes(data) {
|
const appEvents = container.lookup("service:app-events");
|
||||||
this.ogTitle.setAttribute("content", data.title);
|
appEvents.on("page:changed", ({ title, url }) => {
|
||||||
this.ogUrl.setAttribute("content", getAbsoluteURL(data.url));
|
ogTitle.setAttribute("content", title);
|
||||||
},
|
ogUrl.setAttribute("content", getAbsoluteURL(url));
|
||||||
|
});
|
||||||
teardown() {
|
|
||||||
this.appEvents.off("page:changed", this, this.updateOgAttributes);
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
@ -14,7 +14,7 @@ import User from "discourse/models/user";
|
||||||
|
|
||||||
const ALL_TARGETS = ["controller", "component", "route", "model", "adapter"];
|
const ALL_TARGETS = ["controller", "component", "route", "model", "adapter"];
|
||||||
|
|
||||||
export function registerObjects(container, app) {
|
export function registerObjects(app) {
|
||||||
if (app.__registeredObjects__) {
|
if (app.__registeredObjects__) {
|
||||||
// don't run registrations twice.
|
// don't run registrations twice.
|
||||||
return;
|
return;
|
||||||
|
@ -36,7 +36,7 @@ export default {
|
||||||
after: "discourse-bootstrap",
|
after: "discourse-bootstrap",
|
||||||
|
|
||||||
initialize(container, app) {
|
initialize(container, app) {
|
||||||
registerObjects(container, app);
|
registerObjects(app);
|
||||||
|
|
||||||
let siteSettings = container.lookup("site-settings:main");
|
let siteSettings = container.lookup("site-settings:main");
|
||||||
|
|
||||||
|
|
|
@ -1,24 +1,5 @@
|
||||||
import Evented from "@ember/object/evented";
|
import Evented from "@ember/object/evented";
|
||||||
import Service from "@ember/service";
|
import Service from "@ember/service";
|
||||||
import deprecated from "discourse-common/lib/deprecated";
|
|
||||||
|
|
||||||
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, {
|
export default Service.extend(Evented, {
|
||||||
init() {
|
init() {
|
||||||
|
@ -29,49 +10,4 @@ export default Service.extend(Evented, {
|
||||||
this.currentUser.appEvents = this;
|
this.currentUser.appEvents = this;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
on() {
|
|
||||||
if (arguments.length === 2) {
|
|
||||||
let [name, fn] = arguments;
|
|
||||||
let target = {};
|
|
||||||
_events[name] = _events[name] || [];
|
|
||||||
_events[name].push({ target, fn });
|
|
||||||
|
|
||||||
this._super(name, target, fn);
|
|
||||||
} else if (arguments.length === 3) {
|
|
||||||
let [name, target, fn] = arguments;
|
|
||||||
_events[name] = _events[name] || [];
|
|
||||||
_events[name].push({ target, fn });
|
|
||||||
|
|
||||||
this._super(...arguments);
|
|
||||||
}
|
|
||||||
return this;
|
|
||||||
},
|
|
||||||
|
|
||||||
off() {
|
|
||||||
let name = arguments[0];
|
|
||||||
let fn = arguments[2];
|
|
||||||
|
|
||||||
if (_events[name]) {
|
|
||||||
if (arguments.length === 1) {
|
|
||||||
deprecated(
|
|
||||||
"Removing all event listeners at once is deprecated, please remove each listener individually."
|
|
||||||
);
|
|
||||||
|
|
||||||
_events[name].forEach((ref) => {
|
|
||||||
this._super(name, ref.target, ref.fn);
|
|
||||||
});
|
|
||||||
delete _events[name];
|
|
||||||
} else if (arguments.length === 3) {
|
|
||||||
this._super(...arguments);
|
|
||||||
|
|
||||||
_events[name] = _events[name].filter((e) => e.fn !== fn);
|
|
||||||
if (_events[name].length === 0) {
|
|
||||||
delete _events[name];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return this;
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
|
@ -302,15 +302,12 @@ export function acceptance(name, optionsOrCallback) {
|
||||||
resetLastEditNotificationClick();
|
resetLastEditNotificationClick();
|
||||||
clearAuthMethods();
|
clearAuthMethods();
|
||||||
|
|
||||||
app._runInitializer("instanceInitializers", (initName, initializer) => {
|
app._runInitializer("instanceInitializers", (_, initializer) => {
|
||||||
if (initializer && initializer.teardown) {
|
initializer.teardown?.();
|
||||||
initializer.teardown(this.container);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
app._runInitializer("initializers", (initName, initializer) => {
|
|
||||||
if (initializer && initializer.teardown) {
|
app._runInitializer("initializers", (_, initializer) => {
|
||||||
initializer.teardown(this.container);
|
initializer.teardown?.(this.container);
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (LEGACY_ENV) {
|
if (LEGACY_ENV) {
|
||||||
|
|
|
@ -12,7 +12,7 @@ import {
|
||||||
currentSettings,
|
currentSettings,
|
||||||
resetSettings,
|
resetSettings,
|
||||||
} from "discourse/tests/helpers/site-settings";
|
} from "discourse/tests/helpers/site-settings";
|
||||||
import { getOwner, setDefaultOwner } from "discourse-common/lib/get-owner";
|
import { setDefaultOwner } from "discourse-common/lib/get-owner";
|
||||||
import { setApplication, setResolver } from "@ember/test-helpers";
|
import { setApplication, setResolver } from "@ember/test-helpers";
|
||||||
import { setupS3CDN, setupURL } from "discourse-common/lib/get-url";
|
import { setupS3CDN, setupURL } from "discourse-common/lib/get-url";
|
||||||
import Application from "../app";
|
import Application from "../app";
|
||||||
|
@ -25,7 +25,6 @@ import Session from "discourse/models/session";
|
||||||
import User from "discourse/models/user";
|
import User from "discourse/models/user";
|
||||||
import bootbox from "bootbox";
|
import bootbox from "bootbox";
|
||||||
import { buildResolver } from "discourse-common/resolver";
|
import { buildResolver } from "discourse-common/resolver";
|
||||||
import { clearAppEventsCache } from "discourse/services/app-events";
|
|
||||||
import { createHelperContext } from "discourse-common/lib/helpers";
|
import { createHelperContext } from "discourse-common/lib/helpers";
|
||||||
import deprecated from "discourse-common/lib/deprecated";
|
import deprecated from "discourse-common/lib/deprecated";
|
||||||
import { flushMap } from "discourse/models/store";
|
import { flushMap } from "discourse/models/store";
|
||||||
|
@ -84,7 +83,7 @@ function createApplication(config, settings) {
|
||||||
}
|
}
|
||||||
|
|
||||||
app.SiteSettings = settings;
|
app.SiteSettings = settings;
|
||||||
registerObjects(container, app);
|
registerObjects(app);
|
||||||
return app;
|
return app;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -315,13 +314,6 @@ function setupTestsCommon(application, container, config) {
|
||||||
$(".modal-backdrop").remove();
|
$(".modal-backdrop").remove();
|
||||||
flushMap();
|
flushMap();
|
||||||
|
|
||||||
if (isLegacyEmber()) {
|
|
||||||
// ensures any event not removed is not leaking between tests
|
|
||||||
// most likely in initializers, other places (controller, component...)
|
|
||||||
// should be fixed in code
|
|
||||||
clearAppEventsCache(getOwner(this));
|
|
||||||
}
|
|
||||||
|
|
||||||
MessageBus.unsubscribe("*");
|
MessageBus.unsubscribe("*");
|
||||||
server = null;
|
server = null;
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue
Block a user