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.
This commit is contained in:
Robin Ward 2019-10-04 10:06:08 -04:00 committed by GitHub
parent 5e88baebb6
commit f5d391a48a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 41 additions and 35 deletions

View File

@ -19,7 +19,7 @@
//= require ./discourse/lib/hash //= require ./discourse/lib/hash
//= require ./discourse/lib/load-script //= require ./discourse/lib/load-script
//= require ./discourse/lib/notification-levels //= require ./discourse/lib/notification-levels
//= require ./discourse/lib/app-events //= require ./discourse/services/app-events
//= require ./discourse/lib/offset-calculator //= require ./discourse/lib/offset-calculator
//= require ./discourse/lib/lock-on //= require ./discourse/lib/lock-on
//= require ./discourse/lib/url //= require ./discourse/lib/url

View File

@ -1,4 +1,5 @@
import { findHelper } from "discourse-common/lib/helpers"; import { findHelper } from "discourse-common/lib/helpers";
import deprecated from "discourse-common/lib/deprecated";
/* global requirejs, require */ /* global requirejs, require */
var classify = Ember.String.classify; var classify = Ember.String.classify;
@ -45,6 +46,14 @@ export function buildResolver(baseName) {
}, },
normalize(fullName) { 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(":"); const split = fullName.split(":");
if (split.length > 1) { if (split.length > 1) {
const appBase = `${baseName}/${split[0]}s/`; const appBase = `${baseName}/${split[0]}s/`;

View File

@ -10,7 +10,7 @@ export default {
).selectable_avatars_enabled; ).selectable_avatars_enabled;
container container
.lookup("app-events:main") .lookup("service:app-events")
.on("show-avatar-select", this, "_showAvatarSelect"); .on("show-avatar-select", this, "_showAvatarSelect");
}, },

View File

@ -13,7 +13,7 @@ export default {
user.unread_notifications + user.unread_private_messages; user.unread_notifications + user.unread_private_messages;
container container
.lookup("app-events:main") .lookup("service:app-events")
.on("notifications:changed", this, "_updateBadge"); .on("notifications:changed", this, "_updateBadge");
}, },

View File

@ -17,7 +17,7 @@ export default {
router.on("routeWillChange", viewTrackingRequired); router.on("routeWillChange", viewTrackingRequired);
router.on("routeDidChange", cleanDOM); router.on("routeDidChange", cleanDOM);
let appEvents = container.lookup("app-events:main"); let appEvents = container.lookup("service:app-events");
startPageTracking(router, appEvents); startPageTracking(router, appEvents);

View File

@ -18,7 +18,7 @@ export default {
initialize(container) { initialize(container) {
const user = container.lookup("current-user:main"); const user = container.lookup("current-user:main");
const bus = container.lookup("message-bus:main"); const bus = container.lookup("message-bus:main");
const appEvents = container.lookup("app-events:main"); const appEvents = container.lookup("service:app-events");
if (user) { if (user) {
bus.subscribe("/reviewable_counts", data => { bus.subscribe("/reviewable_counts", data => {

View File

@ -9,7 +9,7 @@ export default {
this.container = container; this.container = container;
container container
.lookup("app-events:main") .lookup("service:app-events")
.on("notifications:changed", this, "_updateTitle"); .on("notifications:changed", this, "_updateTitle");
}, },

View File

@ -30,7 +30,7 @@ function _clean() {
} }
// TODO: Avoid container lookup here // 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"); appEvents.trigger("dom:clean");
} }

View File

@ -86,7 +86,7 @@ export default {
this._stopCallback(); this._stopCallback();
this.searchService = this.container.lookup("search-service:main"); 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"); this.currentUser = this.container.lookup("current-user:main");
let siteSettings = this.container.lookup("site-settings:main"); let siteSettings = this.container.lookup("site-settings:main");

View File

@ -449,7 +449,7 @@ class PluginApi {
``` ```
**/ **/
onAppEvent(name, fn) { onAppEvent(name, fn) {
const appEvents = this._lookupContainer("app-events:main"); const appEvents = this._lookupContainer("service:app-events");
appEvents && appEvents.on(name, fn); appEvents && appEvents.on(name, fn);
} }

View File

@ -398,6 +398,9 @@ const DiscourseURL = Ember.Object.extend({
); );
}, },
// TODO: These container calls can be replaced eventually if we migrate this to a service
// object.
/** /**
@private @private
@ -410,6 +413,10 @@ const DiscourseURL = Ember.Object.extend({
return Discourse.__container__.lookup("router:main"); return Discourse.__container__.lookup("router:main");
}, },
get appEvents() {
return Discourse.__container__.lookup("service:app-events");
},
// Get a controller. Note that currently it uses `__container__` which is not // Get a controller. Note that currently it uses `__container__` which is not
// advised but there is no other way to access the router. // advised but there is no other way to access the router.
controllerFor(name) { controllerFor(name) {

View File

@ -1,8 +1,6 @@
import Session from "discourse/models/session"; import Session from "discourse/models/session";
import KeyValueStore from "discourse/lib/key-value-store"; import KeyValueStore from "discourse/lib/key-value-store";
import AppEvents from "discourse/lib/app-events";
import Store from "discourse/models/store"; import Store from "discourse/models/store";
import DiscourseURL from "discourse/lib/url";
import DiscourseLocation from "discourse/lib/discourse-location"; import DiscourseLocation from "discourse/lib/discourse-location";
import SearchService from "discourse/services/search"; import SearchService from "discourse/services/search";
import { import {
@ -17,10 +15,7 @@ export default {
name: "inject-discourse-objects", name: "inject-discourse-objects",
initialize(container, app) { initialize(container, app) {
const appEvents = AppEvents.create(); ALL_TARGETS.forEach(t => app.inject(t, "appEvents", "service:app-events"));
app.register("app-events:main", appEvents, { instantiate: false });
ALL_TARGETS.forEach(t => app.inject(t, "appEvents", "app-events:main"));
DiscourseURL.appEvents = appEvents;
// backwards compatibility: remove when plugins have updated // backwards compatibility: remove when plugins have updated
app.register("store:main", Store); app.register("store:main", Store);

View File

@ -1,6 +1,6 @@
import deprecated from "discourse-common/lib/deprecated"; import deprecated from "discourse-common/lib/deprecated";
export default Ember.Object.extend(Ember.Evented, { export default Ember.Service.extend(Ember.Evented, {
_events: {}, _events: {},
on() { on() {

View File

@ -111,7 +111,7 @@ export default class Widget {
this.currentUser = register.lookup("current-user:main"); this.currentUser = register.lookup("current-user:main");
this.capabilities = register.lookup("capabilities:main"); this.capabilities = register.lookup("capabilities:main");
this.store = register.lookup("service:store"); this.store = register.lookup("service:store");
this.appEvents = register.lookup("app-events:main"); this.appEvents = register.lookup("service:app-events");
this.keyValueStore = register.lookup("key-value-store:main"); this.keyValueStore = register.lookup("key-value-store:main");
// Helps debug widgets // Helps debug widgets

View File

@ -3,7 +3,7 @@ import { withPluginApi } from "discourse/lib/plugin-api";
function initialize(api) { function initialize(api) {
const messageBus = api.container.lookup("message-bus:main"); const messageBus = api.container.lookup("message-bus:main");
const currentUser = api.getCurrentUser(); const currentUser = api.getCurrentUser();
const appEvents = api.container.lookup("app-events:main"); const appEvents = api.container.lookup("service:app-events");
api.modifyClass("component:site-header", { api.modifyClass("component:site-header", {
didInsertElement() { didInsertElement() {

View File

@ -704,7 +704,7 @@ testCase("replace-text event by default", async function(assert) {
this.set("value", "red green blue"); this.set("value", "red green blue");
await this.container await this.container
.lookup("app-events:main") .lookup("service:app-events")
.trigger("composer:replace-text", "green", "yellow"); .trigger("composer:replace-text", "green", "yellow");
assert.equal(this.value, "red green blue"); assert.equal(this.value, "red green blue");
@ -714,7 +714,7 @@ composerTestCase("replace-text event for composer", async function(assert) {
this.set("value", "red green blue"); this.set("value", "red green blue");
await this.container await this.container
.lookup("app-events:main") .lookup("service:app-events")
.trigger("composer:replace-text", "green", "yellow"); .trigger("composer:replace-text", "green", "yellow");
assert.equal(this.value, "red yellow blue"); assert.equal(this.value, "red yellow blue");
@ -800,7 +800,7 @@ composerTestCase("replace-text event for composer", async function(assert) {
setTextareaSelection(textarea, start, start + len); setTextareaSelection(textarea, start, start + len);
this.container this.container
.lookup("app-events:main") .lookup("service:app-events")
.trigger("composer:replace-text", "green", "yellow", { forceFocus: true }); .trigger("composer:replace-text", "green", "yellow", { forceFocus: true });
Ember.run.next(() => { Ember.run.next(() => {

View File

@ -1,15 +1,15 @@
import AppEvents from "discourse/lib/app-events";
import Topic from "discourse/models/topic"; import Topic from "discourse/models/topic";
import PostStream from "discourse/models/post-stream"; import PostStream from "discourse/models/post-stream";
import { Placeholder } from "discourse/lib/posts-with-placeholders"; import { Placeholder } from "discourse/lib/posts-with-placeholders";
moduleFor("controller:topic", "controller:topic", { moduleFor("controller:topic", "controller:topic", {
needs: ["controller:composer", "controller:application"], needs: [
"controller:composer",
"controller:application",
"service:app-events"
],
beforeEach() { beforeEach() {
this.registry.register("app-events:main", AppEvents.create(), { this.registry.injection("controller", "appEvents", "service:app-events");
instantiate: false
});
this.registry.injection("controller", "appEvents", "app-events:main");
} }
}); });

View File

@ -1,4 +1,3 @@
import AppEvents from "discourse/lib/app-events";
import createStore from "helpers/create-store"; import createStore from "helpers/create-store";
import { autoLoadModules } from "discourse/initializers/auto-load-modules"; import { autoLoadModules } from "discourse/initializers/auto-load-modules";
import TopicTrackingState from "discourse/models/topic-tracking-state"; import TopicTrackingState from "discourse/models/topic-tracking-state";
@ -11,19 +10,15 @@ export default function(name, opts) {
} }
test(name, function(assert) { test(name, function(assert) {
const appEvents = AppEvents.create();
this.site = Discourse.Site.current(); this.site = Discourse.Site.current();
this.registry.register("site-settings:main", Discourse.SiteSettings, { this.registry.register("site-settings:main", Discourse.SiteSettings, {
instantiate: false instantiate: false
}); });
this.registry.register("app-events:main", appEvents, {
instantiate: false
});
this.registry.register("capabilities:main", Ember.Object); this.registry.register("capabilities:main", Ember.Object);
this.registry.register("site:main", this.site, { instantiate: false }); this.registry.register("site:main", this.site, { instantiate: false });
this.registry.injection("component", "siteSettings", "site-settings:main"); this.registry.injection("component", "siteSettings", "site-settings:main");
this.registry.injection("component", "appEvents", "app-events:main"); this.registry.injection("component", "appEvents", "service:app-events");
this.registry.injection("component", "capabilities", "capabilities:main"); this.registry.injection("component", "capabilities", "capabilities:main");
this.registry.injection("component", "site", "site:main"); this.registry.injection("component", "site", "site:main");

View File

@ -1,5 +1,5 @@
import { currentUser } from "helpers/qunit-helpers"; import { currentUser } from "helpers/qunit-helpers";
import AppEvents from "discourse/lib/app-events"; import AppEvents from "discourse/services/app-events";
import Composer from "discourse/models/composer"; import Composer from "discourse/models/composer";
import createStore from "helpers/create-store"; import createStore from "helpers/create-store";

View File

@ -159,7 +159,7 @@ QUnit.testDone(function() {
// ensures any event not removed is not leaking between tests // ensures any event not removed is not leaking between tests
// most likely in intialisers, other places (controller, component...) // most likely in intialisers, other places (controller, component...)
// should be fixed in code // should be fixed in code
var appEvents = window.Discourse.__container__.lookup("app-events:main"); var appEvents = window.Discourse.__container__.lookup("service:app-events");
var events = appEvents.__proto__._events; var events = appEvents.__proto__._events;
Object.keys(events).forEach(function(eventKey) { Object.keys(events).forEach(function(eventKey) {
var event = events[eventKey]; var event = events[eventKey];