REFACTOR: Migrate `screenTrack` to a service

REFACTOR: Migrate screenTrack to a service

diff --git a/app/assets/javascripts/discourse/app/components/scrolling-post-stream.js b/app/assets/javascripts/discourse/app/components/scrolling-post-stream.js
index 399e2a7..aeb0148 100644
--- a/app/assets/javascripts/discourse/app/components/scrolling-post-stream.js
+++ b/app/assets/javascripts/discourse/app/components/scrolling-post-stream.js
@@ -4,6 +4,7 @@ import MountWidget from "discourse/components/mount-widget";
 import { cloak, uncloak } from "discourse/widgets/post-stream";
 import { isWorkaroundActive } from "discourse/lib/safari-hacks";
 import offsetCalculator from "discourse/lib/offset-calculator";
+import { inject as service } from "@ember/service";
 
 function findTopView($posts, viewportTop, postsWrapperTop, min, max) {
   if (max < min) {
@@ -26,6 +27,7 @@ function findTopView($posts, viewportTop, postsWrapperTop, min, max) {
 }
 
 export default MountWidget.extend({
+  screenTrack: service(),
   widget: "post-stream",
   _topVisible: null,
   _bottomVisible: null,
diff --git a/app/assets/javascripts/discourse/app/controllers/topic.js b/app/assets/javascripts/discourse/app/controllers/topic.js
index 93077ca..c6277af 100644
--- a/app/assets/javascripts/discourse/app/controllers/topic.js
+++ b/app/assets/javascripts/discourse/app/controllers/topic.js
@@ -43,6 +43,9 @@ export function registerCustomPostMessageCallback(type, callback) {
 export default Controller.extend(bufferedProperty("model"), {
   composer: controller(),
   application: controller(),
+  documentTitle: service(),
+  screenTrack: service(),
+
   multiSelect: false,
   selectedPostIds: null,
   editingTopic: false,
@@ -60,7 +63,6 @@ export default Controller.extend(bufferedProperty("model"), {
   username_filters: null,
   filter: null,
   quoteState: null,
-  documentTitle: service(),
 
   canRemoveTopicFeaturedLink: and(
     "canEditTopicFeaturedLink",
@@ -454,9 +456,7 @@ export default Controller.extend(bufferedProperty("model"), {
     },
 
     deferTopic() {
-      const screenTrack = Discourse.__container__.lookup("screen-track:main");
-      const currentUser = this.currentUser;
-      const topic = this.model;
+      const { screenTrack, currentUser, topic } = this;
 
       screenTrack.reset();
       screenTrack.stop();
diff --git a/app/assets/javascripts/discourse/app/initializers/signup-cta.js b/app/assets/javascripts/discourse/app/initializers/signup-cta.js
index db674cc..0a63d3c 100644
--- a/app/assets/javascripts/discourse/app/initializers/signup-cta.js
+++ b/app/assets/javascripts/discourse/app/initializers/signup-cta.js
@@ -9,7 +9,7 @@ export default {
   name: "signup-cta",
 
   initialize(container) {
-    const screenTrack = container.lookup("screen-track:main");
+    const screenTrack = container.lookup("service:screen-track");
     const session = Session.current();
     const siteSettings = container.lookup("site-settings:main");
     const keyValueStore = container.lookup("key-value-store:main");
diff --git a/app/assets/javascripts/discourse/app/pre-initializers/inject-discourse-objects.js b/app/assets/javascripts/discourse/app/pre-initializers/inject-discourse-objects.js
index 98d59a8..b1efdca 100644
--- a/app/assets/javascripts/discourse/app/pre-initializers/inject-discourse-objects.js
+++ b/app/assets/javascripts/discourse/app/pre-initializers/inject-discourse-objects.js
@@ -71,17 +71,14 @@ export default {
     ALL_TARGETS.forEach(t => app.inject(t, "session", "session:main"));
     app.inject("service", "session", "session:main");
 
+    // TODO: Automatically register this service
     const screenTrack = new ScreenTrack(
       topicTrackingState,
       siteSettings,
       session,
       currentUser
     );
-
-    app.register("screen-track:main", screenTrack, { instantiate: false });
-    ["component", "route"].forEach(t =>
-      app.inject(t, "screenTrack", "screen-track:main")
-    );
+    app.register("service:screen-track", screenTrack, { instantiate: false });
 
     if (currentUser) {
       ["component", "route", "controller", "service"].forEach(t => {
diff --git a/app/assets/javascripts/discourse/app/routes/build-topic-route.js b/app/assets/javascripts/discourse/app/routes/build-topic-route.js
index e05fdcc..a652b39 100644
--- a/app/assets/javascripts/discourse/app/routes/build-topic-route.js
+++ b/app/assets/javascripts/discourse/app/routes/build-topic-route.js
@@ -10,6 +10,7 @@ import { defaultHomepage } from "discourse/lib/utilities";
 import Session from "discourse/models/session";
 import { Promise } from "rsvp";
 import Site from "discourse/models/site";
+import { inject as service } from "@ember/service";
 
 // A helper to build a topic route for a filter
 function filterQueryParams(params, defaultParams) {
@@ -87,6 +88,7 @@ export default function(filter, extras) {
   extras = extras || {};
   return DiscourseRoute.extend(
     {
+      screenTrack: service(),
       queryParams,
 
       beforeModel() {
diff --git a/app/assets/javascripts/discourse/app/routes/topic.js b/app/assets/javascripts/discourse/app/routes/topic.js
index 55c8a74..ebaa384 100644
--- a/app/assets/javascripts/discourse/app/routes/topic.js
+++ b/app/assets/javascripts/discourse/app/routes/topic.js
@@ -5,12 +5,15 @@ import DiscourseRoute from "discourse/routes/discourse";
 import DiscourseURL from "discourse/lib/url";
 import { ID_CONSTRAINT } from "discourse/models/topic";
 import { setTopicId } from "discourse/lib/topic-list-tracker";
+import { inject as service } from "@ember/service";
 
 const SCROLL_DELAY = 500;
 
 import showModal from "discourse/lib/show-modal";
 
 const TopicRoute = DiscourseRoute.extend({
+  screenTrack: service(),
+
   init() {
     this._super(...arguments);
 
diff --git a/test/javascripts/controllers/topic-test.js b/test/javascripts/controllers/topic-test.js
index 08796ef..cf6ed4e 100644
--- a/test/javascripts/controllers/topic-test.js
+++ b/test/javascripts/controllers/topic-test.js
@@ -14,6 +14,7 @@ moduleFor("controller:topic", "controller:topic", {
     "service:document-title"
   ],
   beforeEach() {
+    this.registry.register("service:screen-track", {}, { instantiate: false });
     this.registry.injection("controller", "appEvents", "service:app-events");
   }
 });

GitHub sha: f52927cd