Move message bus subscription into controller

Move message bus subscription into controller

This ensures we always correctly unsubscribe from the channel

From 312357988f2f49fd32c8c0f245278c0ede8fc6e8 Mon Sep 17 00:00:00 2001
From: Sam <sam.saffron@gmail.com>
Date: Fri, 23 Nov 2018 09:44:59 +1100
Subject: [PATCH] Move message bus subscription into controller

This ensures we always correctly unsubscribe from the channel

diff --git a/assets/javascripts/discourse-assign/initializers/extend-for-assigns.js.es6 b/assets/javascripts/discourse-assign/initializers/extend-for-assigns.js.es6
index 4a4633e..486ac0e 100644
--- a/assets/javascripts/discourse-assign/initializers/extend-for-assigns.js.es6
+++ b/assets/javascripts/discourse-assign/initializers/extend-for-assigns.js.es6
@@ -153,24 +153,34 @@ function initialize(api, container) {
     }
   });
 
+  api.modifyClass("controller:topic", {
+    subscribe() {
+      this._super();
+      this.messageBus.subscribe("/staff/topic-assignment", data => {
+        const topic = this.get("model");
+        const topicId = topic.get("id");
+
+        if (data.topic_id === topicId) {
+          topic.set(
+            "assigned_to_user_id",
+            data.type === "assigned" ? data.assigned_to.id : null
+          );
+          topic.set("assigned_to_user", data.assigned_to);
+        }
+        this.appEvents.trigger("header:update-topic", topic);
+      });
+    },
+    unsubscribe() {
+      this._super();
+      if (!this.get("content.id")) return;
+      this.messageBus.unsubscribe("/staff/topic-assignment");
+    }
+  });
+
   api.decorateWidget("post-contents:after-cooked", dec => {
     if (dec.attrs.post_number === 1) {
       const postModel = dec.getModel();
       if (postModel) {
-        // Redraw widget when a message arrives.
-        const messageBus = container.lookup("message-bus:main");
-        messageBus.unsubscribe("/staff/topic-assignment");
-        messageBus.subscribe("/staff/topic-assignment", data => {
-          if (data.topic_id === postModel.get("topic.id")) {
-            postModel.set(
-              "topic.assigned_to_user_id",
-              data.type === "assigned" ? data.assigned_to.id : null
-            );
-            postModel.set("topic.assigned_to_user", data.assigned_to);
-          }
-          dec.widget.scheduleRerender();
-        });
-
         const assignedToUser = postModel.get("topic.assigned_to_user");
         if (assignedToUser) {
           return dec.widget.attach("assigned-to", {

GitHub

1 Like