REFACTOR: Remove container from DiscourseURL

This commit is contained in:
Robin Ward 2020-08-19 12:08:04 -04:00
parent dae8b9bcfb
commit 98e5e1cd53
4 changed files with 21 additions and 19 deletions

View File

@ -7,6 +7,7 @@ import { defaultHomepage } from "discourse/lib/utilities";
import User from "discourse/models/user";
import { default as getURL, withoutPrefix } from "discourse-common/lib/get-url";
import Session from "discourse/models/session";
import { setOwner } from "@ember/application";
const rewrites = [];
const TOPIC_REGEXP = /\/t\/([^\/]+)\/(\d+)\/?(\d+)?/;
@ -189,7 +190,7 @@ const DiscourseURL = EmberObject.extend({
// while URLs are loading. For example, while a topic loads it sets `currentPost`
// which triggers a replaceState even though the topic hasn't fully loaded yet!
next(() => {
const location = DiscourseURL.get("router.location");
const location = this.get("router.location");
if (location && location.replaceURL) {
location.replaceURL(path);
}
@ -231,7 +232,7 @@ const DiscourseURL = EmberObject.extend({
const pathname = path.replace(/(https?\:)?\/\/[^\/]+/, "");
if (!DiscourseURL.isInternal(path)) {
if (!this.isInternal(path)) {
return redirectTo(path);
}
@ -361,7 +362,7 @@ const DiscourseURL = EmberObject.extend({
// If the topic_id is the same
if (oldTopicId === newTopicId) {
DiscourseURL.replaceState(path);
this.replaceState(path);
const container = Discourse.__container__;
const topicController = container.lookup("controller:topic");
@ -436,17 +437,6 @@ const DiscourseURL = EmberObject.extend({
return window.location.origin + (prefix === "/" ? "" : prefix);
},
// TODO: These container calls can be replaced eventually if we migrate this to a service
// object.
/**
@private
Get a handle on the application's router. Note that currently it uses `__container__` which is not
advised but there is no other way to access the router.
@property router
**/
get router() {
return Discourse.__container__.lookup("router:main");
},
@ -455,8 +445,6 @@ const DiscourseURL = EmberObject.extend({
return Discourse.__container__.lookup("service:app-events");
},
// Get a controller. Note that currently it uses `__container__` which is not
// advised but there is no other way to access the router.
controllerFor(name) {
return Discourse.__container__.lookup("controller:" + name);
},
@ -498,6 +486,12 @@ const DiscourseURL = EmberObject.extend({
const promise = transition.promise || transition;
promise.then(() => jumpToElement(elementId));
}
}).create();
});
let _urlInstance = DiscourseURL.create();
export default DiscourseURL;
export function setURLContainer(container) {
_urlInstance.container = container;
setOwner(_urlInstance, container);
}
export default _urlInstance;

View File

@ -12,6 +12,7 @@ import { setupURL, setupS3CDN } from "discourse-common/lib/get-url";
import deprecated from "discourse-common/lib/deprecated";
import { setIconList } from "discourse-common/lib/icon-library";
import { setPluginContainer } from "discourse/lib/plugin-api";
import { setURLContainer } from "discourse/lib/url";
export default {
name: "discourse-bootstrap",
@ -19,6 +20,7 @@ export default {
// The very first initializer to run
initialize(container, app) {
setPluginContainer(container);
setURLContainer(container);
// Our test environment has its own bootstrap code
if (isTesting()) {

View File

@ -28,6 +28,7 @@ import { mapRoutes } from "discourse/mapping-router";
import { currentSettings, mergeSettings } from "helpers/site-settings";
import { getOwner } from "discourse-common/lib/get-owner";
import { setTopicList } from "discourse/lib/topic-list-tracker";
import { setURLContainer } from "discourse/lib/url";
export function currentUser() {
return User.create(sessionFixtures["/session/current.json"].current_user);
@ -174,6 +175,7 @@ export function acceptance(name, options) {
Discourse.reset();
this.container = getOwner(this);
setPluginContainer(this.container);
setURLContainer(this.container);
if (options.beforeEach) {
options.beforeEach.call(this);
}
@ -202,6 +204,7 @@ export function acceptance(name, options) {
resetCustomPostMessageCallbacks();
setTopicList(null);
_clearSnapshots();
setURLContainer(null);
Discourse._runInitializer(
"instanceInitializers",
(initName, initializer) => {

View File

@ -165,7 +165,10 @@ QUnit.testStart(function(ctx) {
Session.resetCurrent();
User.resetCurrent();
resetSite(settings);
createHelperContext({ siteSettings: settings });
createHelperContext({
siteSettings: settings,
capabilities: {}
});
_DiscourseURL.redirectedTo = null;
_DiscourseURL.redirectTo = function(url) {