Skip to content

A platform for community discussion. Free, open, simple.

FIX: Avoid leaking TopicTrackingState listeners due to sidebar (#18131)

Alan Guo Xiang Tanc245b7439822
diff --git a/app/assets/javascripts/discourse/app/components/sidebar/anonymous/community-section.js b/app/assets/javascripts/discourse/app/components/sidebar/anonymous/community-section.js
index faef94d5dc6..a37ec7795f7 100644
--- a/app/assets/javascripts/discourse/app/components/sidebar/anonymous/community-section.js
+++ b/app/assets/javascripts/discourse/app/components/sidebar/anonymous/community-section.js
@@ -7,18 +7,16 @@ import UsersSectionLink from "discourse/lib/sidebar/common/community-section/use
import BadgesSectionLink from "discourse/lib/sidebar/common/community-section/badges-section-link";
export default class SidebarAnonymousCommunitySection extends SidebarCommonCommunitySection {
- constructor() {
- super(...arguments);
-
- this.defaultMoreSectionLinks = [GroupsSectionLink, BadgesSectionLink];
-
- this.defaultMoreSecondarySectionLinks = [];
-
- this.defaultMainSectionLinks = [
+ get defaultMainSectionLinks() {
+ return [
EverythingSectionLink,
UsersSectionLink,
AboutSectionLink,
FAQSectionLink,
];
}
+
+ get defaultMoreSectionLinks() {
+ return [GroupsSectionLink, BadgesSectionLink];
+ }
}
diff --git a/app/assets/javascripts/discourse/app/components/sidebar/common/community-section.js b/app/assets/javascripts/discourse/app/components/sidebar/common/community-section.js
index 333a29f5d46..64a74edf18c 100644
--- a/app/assets/javascripts/discourse/app/components/sidebar/common/community-section.js
+++ b/app/assets/javascripts/discourse/app/components/sidebar/common/community-section.js
@@ -13,45 +13,68 @@ export default class SidebarCommunitySection extends Component {
@service appEvents;
@service siteSettings;
- // Override in child
- defaultMainSectionLinks = [];
- defaultAdminMainSectionLinks = [];
- defaultMoreSectionLinks = [];
- defaultMoreSecondarySectionLinks = [];
headerActionsIcon;
headerActions;
+ sectionLinks;
+ moreSectionLinks;
+ moreSecondarySectionLinks;
+ callbackId;
- get moreSectionLinks() {
- return [...this.defaultMoreSectionLinks, ...customSectionLinks].map(
- (sectionLinkClass) => {
- return this.#initializeSectionLink(sectionLinkClass);
- }
- );
- }
+ constructor() {
+ super(...arguments);
+
+ this.moreSectionLinks = [
+ ...this.defaultMoreSectionLinks,
+ ...customSectionLinks,
+ ].map((sectionLinkClass) => {
+ return this.#initializeSectionLink(sectionLinkClass);
+ });
- get moreSecondarySectionLinks() {
- return [
+ this.moreSecondarySectionLinks = [
...this.defaultMoreSecondarySectionLinks,
...secondaryCustomSectionLinks,
].map((sectionLinkClass) => {
return this.#initializeSectionLink(sectionLinkClass);
});
- }
- get mainSectionLinks() {
- return this.currentUser?.staff
+ const mainSectionLinks = this.currentUser?.staff
? [...this.defaultMainSectionLinks, ...this.defaultAdminMainSectionLinks]
: [...this.defaultMainSectionLinks];
- }
- get sectionLinks() {
- return this.mainSectionLinks.map((sectionLinkClass) => {
+ this.sectionLinks = mainSectionLinks.map((sectionLinkClass) => {
return this.#initializeSectionLink(sectionLinkClass);
});
+
+ this.callbackId = this.topicTrackingState.onStateChange(() => {
+ this.sectionLinks.forEach((sectionLink) => {
+ sectionLink.onTopicTrackingStateChange();
+ });
+ });
}
willDestroy() {
this.sectionLinks.forEach((sectionLink) => sectionLink.teardown());
+ this.topicTrackingState.offStateChange(this.callbackId);
+ }
+
+ // Override in child
+ get defaultMainSectionLinks() {
+ return [];
+ }
+
+ // Override in child
+ get defaultAdminMainSectionLinks() {
+ return [];
+ }
+
+ // Override in child
+ get defaultMoreSectionLinks() {
+ return [];
+ }
+
+ // Override in child
+ get defaultMoreSecondarySectionLinks() {
+ return [];
}
#initializeSectionLink(sectionLinkClass) {
diff --git a/app/assets/javascripts/discourse/app/components/sidebar/user/community-section.js b/app/assets/javascripts/discourse/app/components/sidebar/user/community-section.js
index 2c46db8a403..1ba644a618c 100644
--- a/app/assets/javascripts/discourse/app/components/sidebar/user/community-section.js
+++ b/app/assets/javascripts/discourse/app/components/sidebar/user/community-section.js
@@ -21,22 +21,6 @@ export default class SidebarUserCommunitySection extends SidebarCommonCommunityS
constructor() {
super(...arguments);
- this.defaultMoreSectionLinks = [
- GroupsSectionLink,
- UsersSectionLink,
- BadgesSectionLink,
- ];
-
- this.defaultMoreSecondarySectionLinks = [AboutSectionLink, FAQSectionLink];
-
- this.defaultMainSectionLinks = [
- EverythingSectionLink,
- TrackedSectionLink,
- MyPostsSectionLink,
- ];
-
- this.defaultAdminMainSectionLinks = [AdminSectionLink];
-
this.headerActionsIcon = "plus";
this.headerActions = [
@@ -47,6 +31,22 @@ export default class SidebarUserCommunitySection extends SidebarCommonCommunityS
];
}
+ get defaultMainSectionLinks() {
+ return [EverythingSectionLink, TrackedSectionLink, MyPostsSectionLink];
+ }
+
+ get defaultAdminMainSectionLinks() {
+ return [AdminSectionLink];
+ }
+
+ get defaultMoreSectionLinks() {
+ return [GroupsSectionLink, UsersSectionLink, BadgesSectionLink];
+ }
+
+ get defaultMoreSecondarySectionLinks() {
+ return [AboutSectionLink, FAQSectionLink];
+ }
+
@action
composeTopic() {
const composerArgs = {
diff --git a/app/assets/javascripts/discourse/app/lib/sidebar/base-community-section-link.js b/app/assets/javascripts/discourse/app/lib/sidebar/base-community-section-link.js
index 65af90de12e..e41a3e91fb1 100644
--- a/app/assets/javascripts/discourse/app/lib/sidebar/base-community-section-link.js
+++ b/app/assets/javascripts/discourse/app/lib/sidebar/base-community-section-link.js
@@ -16,6 +16,11 @@ export default class BaseCommunitySectionLink {
this.siteSettings = siteSettings;
}
+ /**
+ * Called when state has changed in the TopicTrackingState service
+ */
+ onTopicTrackingStateChange() {}
+
/**
* Called when community-section component is torn down.
*/
diff --git a/app/assets/javascripts/discourse/app/lib/sidebar/common/community-section/everything-section-link.js b/app/assets/javascripts/discourse/app/lib/sidebar/common/community-section/everything-section-link.js
index 8071140d03a..8cba16fe4f0 100644
--- a/app/assets/javascripts/discourse/app/lib/sidebar/common/community-section/everything-section-link.js
+++ b/app/assets/javascripts/discourse/app/lib/sidebar/common/community-section/everything-section-link.js
@@ -1,34 +1,26 @@
import I18n from "I18n";
import { tracked } from "@glimmer/tracking";
-import { bind } from "discourse-common/utils/decorators";
import BaseSectionLink from "discourse/lib/sidebar/base-community-section-link";
export default class EverythingSectionLink extends BaseSectionLink {
@tracked totalUnread = 0;
@tracked totalNew = 0;
- callbackId = null;
constructor() {
super(...arguments);
+ this.#refreshCounts();
+ }
- if (this.currentUser) {
- this.callbackId = this.topicTrackingState.onStateChange(
- this._refreshCounts
- );
-
- this._refreshCounts();
- }
+ onTopicTrackingStateChange() {
+ this.#refreshCounts();
}
- teardown() {
- if (this.callbackId) {
- this.topicTrackingState.offStateChange(this.callbackId);
+ #refreshCounts() {
+ if (!this.currentUser) {
+ return;
}
- }
- @bind
- _refreshCounts() {
this.totalUnread = this.topicTrackingState.countUnread();
if (this.totalUnread === 0) {
diff --git a/app/assets/javascripts/discourse/app/lib/sidebar/user/community-section/tracked-section-link.js b/app/assets/javascripts/discourse/app/lib/sidebar/user/community-section/tracked-section-link.js
index 6b89cf81299..1898b6f6802 100644
--- a/app/assets/javascripts/discourse/app/lib/sidebar/user/community-section/tracked-section-link.js
+++ b/app/assets/javascripts/discourse/app/lib/sidebar/user/community-section/tracked-section-link.js
@@ -1,30 +1,23 @@
import I18n from "I18n";
import { tracked } from "@glimmer/tracking";
-import { bind } from "discourse-common/utils/decorators";
import BaseSectionLink from "discourse/lib/sidebar/base-community-section-link";
import { isTrackedTopic } from "discourse/lib/topic-list-tracked-filter";
export default class TrackedSectionLink extends BaseSectionLink {
@tracked totalUnread = 0;
@tracked totalNew = 0;
- callbackId = null;
constructor() {
super(...arguments);
-
- this.callbackId = this.topicTrackingState.onStateChange(
- this._refreshCounts
- );
- this._refreshCounts();
+ this.#refreshCounts();
}
- teardown() {
- this.topicTrackingState.offStateChange(this.callbackId);
+ onTopicTrackingStateChange() {
+ this.#refreshCounts();
}
- @bind
- _refreshCounts() {
+ #refreshCounts() {
this.totalUnread = this.topicTrackingState.countUnread({
customFilterFn: isTrackedTopic,
});