DEV: Use Ember.Service over manually registering a factory.

DEV: Use Ember.Service over manually registering a factory.

Follow up to 9e827eb420c7aefd166fba1e7724eced81aaffe7

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 b412997..8140c5f 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
@@ -28,7 +28,10 @@ export default {
 
     const messageBus = window.MessageBus;
     app.register("message-bus:main", messageBus, { instantiate: false });
-    ALL_TARGETS.forEach(t => app.inject(t, "messageBus", "message-bus:main"));
+
+    ALL_TARGETS.concat("service").forEach(t =>
+      app.inject(t, "messageBus", "message-bus:main")
+    );
 
     const currentUser = User.current();
     app.register("current-user:main", currentUser, { instantiate: false });
@@ -47,7 +50,7 @@ export default {
 
     const siteSettings = app.SiteSettings;
     app.register("site-settings:main", siteSettings, { instantiate: false });
-    ALL_TARGETS.forEach(t =>
+    ALL_TARGETS.concat("service").forEach(t =>
       app.inject(t, "siteSettings", "site-settings:main")
     );
 
@@ -77,7 +80,7 @@ export default {
     );
 
     if (currentUser) {
-      ["component", "route", "controller"].forEach(t => {
+      ["component", "route", "controller", "service"].forEach(t => {
         app.inject(t, "currentUser", "current-user:main");
       });
     }
diff --git a/plugins/discourse-presence/assets/javascripts/discourse/components/composer-presence-display.js.es6 b/plugins/discourse-presence/assets/javascripts/discourse/components/composer-presence-display.js.es6
index a371e5f..6f94bad 100644
--- a/plugins/discourse-presence/assets/javascripts/discourse/components/composer-presence-display.js.es6
+++ b/plugins/discourse-presence/assets/javascripts/discourse/components/composer-presence-display.js.es6
@@ -1,17 +1,12 @@
 import Component from "@ember/component";
-import { getOwner } from "@ember/application";
 import { cancel } from "@ember/runloop";
 import { equal, gt } from "@ember/object/computed";
+import { inject as service } from "@ember/service";
 import discourseComputed, {
   observes,
   on
 } from "discourse-common/utils/decorators";
-import {
-  REPLYING,
-  CLOSED,
-  EDITING,
-  COMPOSER_TYPE
-} from "../lib/presence-manager";
+import { REPLYING, CLOSED, EDITING, COMPOSER_TYPE } from "../lib/presence";
 import { REPLY, EDIT } from "discourse/models/composer";
 
 export default Component.extend({
@@ -22,15 +17,7 @@ export default Component.extend({
   reply: null,
   title: null,
   isWhispering: null,
-  presenceManager: null,
-
-  init() {
-    this._super(...arguments);
-
-    this.setProperties({
-      presenceManager: getOwner(this).lookup("presence-manager:main")
-    });
-  },
+  presenceManager: service(),
 
   @discourseComputed("topic.id")
   users(topicId) {
diff --git a/plugins/discourse-presence/assets/javascripts/discourse/components/topic-presence-display.js.es6 b/plugins/discourse-presence/assets/javascripts/discourse/components/topic-presence-display.js.es6
index 2f683ca..c44742c 100644
--- a/plugins/discourse-presence/assets/javascripts/discourse/components/topic-presence-display.js.es6
+++ b/plugins/discourse-presence/assets/javascripts/discourse/components/topic-presence-display.js.es6
@@ -1,17 +1,12 @@
 import Component from "@ember/component";
-import { getOwner } from "@ember/application";
 import { gt } from "@ember/object/computed";
+import { inject as service } from "@ember/service";
 import discourseComputed, { on } from "discourse-common/utils/decorators";
-import { TOPIC_TYPE } from "../lib/presence-manager";
+import { TOPIC_TYPE } from "../lib/presence";
 
 export default Component.extend({
   topic: null,
-  presenceManager: null,
-
-  init() {
-    this._super(...arguments);
-    this.set("presenceManager", getOwner(this).lookup("presence-manager:main"));
-  },
+  presenceManager: service(),
 
   @discourseComputed("topic.id")
   users(topicId) {
diff --git a/plugins/discourse-presence/assets/javascripts/discourse/initializers/discourse-presence.js.es6 b/plugins/discourse-presence/assets/javascripts/discourse/initializers/discourse-presence.js.es6
deleted file mode 100644
index 1009fc1..0000000
--- a/plugins/discourse-presence/assets/javascripts/discourse/initializers/discourse-presence.js.es6
+++ /dev/null
@@ -1,32 +0,0 @@
-import { withPluginApi } from "discourse/lib/plugin-api";
-import PresenceManager from "../lib/presence-manager";
-import ENV from "discourse-common/config/environment";
-
-function initializeDiscoursePresence(api, { app }) {
-  const currentUser = api.getCurrentUser();
-
-  if (currentUser) {
-    app.register(
-      "presence-manager:main",
-      PresenceManager.create({
-        currentUser,
-        messageBus: api.container.lookup("message-bus:main"),
-        siteSettings: api.container.lookup("site-settings:main")
-      }),
-      { instantiate: false }
-    );
-  }
-}
-
-export default {
-  name: "discourse-presence",
-  after: "message-bus",
-
-  initialize(container, app) {
-    const siteSettings = container.lookup("site-settings:main");
-
-    if (siteSettings.presence_enabled && ENV.environment !== "test") {
-      withPluginApi("0.8.40", initializeDiscoursePresence, { app });
-    }
-  }
-};
diff --git a/plugins/discourse-presence/assets/javascripts/discourse/lib/presence-manager.js.es6 b/plugins/discourse-presence/assets/javascripts/discourse/lib/presence-manager.js.es6
deleted file mode 100644
index 1be3564..0000000
--- a/plugins/discourse-presence/assets/javascripts/discourse/lib/presence-manager.js.es6
+++ /dev/null
@@ -1,286 +0,0 @@
-import EmberObject from "@ember/object";
-import { cancel, later, throttle } from "@ember/runloop";
-import { ajax } from "discourse/lib/ajax";
-import discourseComputed from "discourse-common/utils/decorators";
-
-// The durations chosen here determines the accuracy of the presence feature and
-// is tied closely with the server side implementation. Decreasing the duration
-// to increase the accuracy will come at the expense of having to more network
-// calls to publish the client's state.
-//
-// Logic walk through of our heuristic implementation:
-// - When client A is typing, a message is published every KEEP_ALIVE_DURATION_SECONDS.
-// - Client B receives the message and stores each user in an array and marks
-//   the user with a client-side timestamp of when the user was seen.
-// - If client A continues to type, client B will continue to receive messages to
-//   update the client-side timestamp of when client A was last seen.
-// - If client A disconnects or becomes inactive, the state of client A will be
-//   cleaned up on client B by a scheduler that runs every TIMER_INTERVAL_MILLISECONDS
-const KEEP_ALIVE_DURATION_SECONDS = 10;
-const BUFFER_DURATION_SECONDS = KEEP_ALIVE_DURATION_SECONDS + 2;
-
-const MESSAGE_BUS_LAST_ID = 0;
-const TIMER_INTERVAL_MILLISECONDS = 2000;
-
-export const REPLYING = "replying";
-export const EDITING = "editing";
-export const CLOSED = "closed";
-
-export const TOPIC_TYPE = "topic";
-export const COMPOSER_TYPE = "composer";
-
-const Presence = EmberObject.extend({
-  users: null,
-  editingUsers: null,
-  subscribers: null,
-  topicId: null,
-  currentUser: null,
-  messageBus: null,
-  siteSettings: null,
-
-  init() {
-    this._super(...arguments);
-
-    this.setProperties({
-      users: [],
-      editingUsers: [],
-      subscribers: new Set()
-    });
-  },
-
-  subscribe(type) {
-    if (this.subscribers.size === 0) {
-      this.messageBus.subscribe(
-        this.channel,
-        message => {
-          const { user, state } = message;
-          if (this.get("currentUser.id") === user.id) return;
-
-          switch (state) {
-            case REPLYING:
-              this._appendUser(this.users, user);
-              break;
-            case EDITING:
-              this._appendUser(this.editingUsers, user, {

[... diff too long, it was truncated ...]

GitHub sha: b299f5f4

This commit appears in #9634 which was approved by davidtaylorhq and eviltrout. It was merged by tgxworld.