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)