DEV: Clean up all message bus subscriptions (#18675)

1. "What Goes Up Must Come Down" – if you subscribe to message bus, make sure you also unsubscribe
2. When you unsubscribe - remove only your subscription, not **all** subscriptions on given channel
This commit is contained in:
Jarek Radosz
2022-11-30 16:49:51 +01:00
committed by GitHub
parent 321b14d40c
commit b0839ccf27
21 changed files with 624 additions and 496 deletions

View File

@@ -1,4 +1,4 @@
import { debounce } from "discourse-common/utils/decorators";
import { bind, debounce } from "discourse-common/utils/decorators";
import { ajax } from "discourse/lib/ajax";
import { headerOffset } from "discourse/lib/offset-calculator";
import isElementInViewport from "discourse/lib/is-element-in-viewport";
@@ -43,35 +43,32 @@ function initialize(api) {
return this._super(bookmark, post);
},
subscribe() {
@bind
onMessage(data) {
this._super(...arguments);
this.messageBus.subscribe(`/topic/${this.model.id}`, (data) => {
const topic = this.model;
const topic = this.model;
// scroll only for discobot (-2 is discobot id)
if (
topic.isPrivateMessage &&
this.currentUser &&
this.currentUser.id !== data.user_id &&
data.user_id === -2 &&
data.type === "created"
) {
const postNumber = data.post_number;
const notInPostStream = topic.get("highest_post_number") <= postNumber;
const postNumberDifference = postNumber - topic.currentPost;
// scroll only for discobot (-2 is discobot id)
if (
topic.isPrivateMessage &&
this.currentUser &&
this.currentUser.id !== data.user_id &&
data.user_id === -2 &&
data.type === "created"
notInPostStream &&
postNumberDifference > 0 &&
postNumberDifference < 7
) {
const postNumber = data.post_number;
const notInPostStream =
topic.get("highest_post_number") <= postNumber;
const postNumberDifference = postNumber - topic.currentPost;
if (
notInPostStream &&
postNumberDifference > 0 &&
postNumberDifference < 7
) {
this._scrollToDiscobotPost(data.post_number);
}
this._scrollToDiscobotPost(data.post_number);
}
});
// No need to unsubscribe, core unsubscribes /topic/* routes
}
},
@debounce(500)

View File

@@ -1,7 +1,7 @@
import EmberObject from "@ember/object";
import WidgetGlue from "discourse/widgets/glue";
import { getRegister } from "discourse-common/lib/get-owner";
import { observes } from "discourse-common/utils/decorators";
import { bind, observes } from "discourse-common/utils/decorators";
import { withPluginApi } from "discourse/lib/plugin-api";
const PLUGIN_ID = "discourse-poll";
@@ -34,16 +34,19 @@ function initializePolls(api) {
subscribe() {
this._super(...arguments);
this.messageBus.subscribe(`/polls/${this.model.id}`, (msg) => {
const post = this.get("model.postStream").findLoadedPost(msg.post_id);
post?.set("polls", msg.polls);
});
this.messageBus.subscribe(`/polls/${this.model.id}`, this._onPollMessage);
},
unsubscribe() {
this.messageBus.unsubscribe("/polls/*");
this.messageBus.unsubscribe("/polls/*", this._onPollMessage);
this._super(...arguments);
},
@bind
_onPollMessage(msg) {
const post = this.get("model.postStream").findLoadedPost(msg.post_id);
post?.set("polls", msg.polls);
},
});
api.modifyClass("model:post", {