Skip to content

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

FIX: Sidebar custom sections rendering perf degrades over time (#21552)

Alan Guo Xiang Tan1106e4ad09eb
diff --git a/app/assets/javascripts/discourse/app/components/sidebar/user/custom-sections.js b/app/assets/javascripts/discourse/app/components/sidebar/user/custom-sections.js
index 8e8cc0478e1..48355a1addf 100644
--- a/app/assets/javascripts/discourse/app/components/sidebar/user/custom-sections.js
+++ b/app/assets/javascripts/discourse/app/components/sidebar/user/custom-sections.js
@@ -2,9 +2,12 @@ import Component from "@glimmer/component";
import { inject as service } from "@ember/service";
import { ajax } from "discourse/lib/ajax";
import { bind } from "discourse-common/utils/decorators";
-import { cached } from "@glimmer/tracking";
import Section from "discourse/lib/sidebar/section";
import CommunitySection from "discourse/lib/sidebar/community-section";
+import { tracked } from "@glimmer/tracking";
+
+export const REFRESH_CUSTOM_SIDEBAR_SECTIONS_APP_EVENT_NAME =
+ "sidebar:refresh-custom-sections";
export default class SidebarUserCustomSections extends Component {
@service currentUser;
@@ -15,21 +18,45 @@ export default class SidebarUserCustomSections extends Component {
@service site;
@service siteSettings;
+ @tracked sections = [];
+
constructor() {
super(...arguments);
this.messageBus.subscribe("/refresh-sidebar-sections", this._refresh);
+
+ this.appEvents.on(
+ REFRESH_CUSTOM_SIDEBAR_SECTIONS_APP_EVENT_NAME,
+ this,
+ this._refreshSections
+ );
+
+ this.#initSections();
}
willDestroy() {
+ this.appEvents.off(
+ REFRESH_CUSTOM_SIDEBAR_SECTIONS_APP_EVENT_NAME,
+ this,
+ this._refreshSections
+ );
+
this.messageBus.unsubscribe("/refresh-sidebar-sections");
+ this.#teardown();
+ }
+
+ #teardown() {
return this.sections.forEach((section) => {
section.teardown?.();
});
}
- @cached
- get sections() {
- return this.currentUser.sidebarSections.map((section) => {
+ _refreshSections() {
+ this.#teardown();
+ this.#initSections();
+ }
+
+ #initSections() {
+ this.sections = this.currentUser.sidebarSections.map((section) => {
switch (section.section_type) {
case "community":
const systemSection = new CommunitySection({
@@ -62,7 +89,7 @@ export default class SidebarUserCustomSections extends Component {
@bind
_refresh() {
return ajax("/sidebar_sections.json", {}).then((json) => {
- this.currentUser.set("sidebar_sections", json.sidebar_sections);
+ this.currentUser.updateSidebarSections(json.sidebar_sections);
});
}
}
diff --git a/app/assets/javascripts/discourse/app/controllers/sidebar-section-form.js b/app/assets/javascripts/discourse/app/controllers/sidebar-section-form.js
index 71467d82c89..bc83fef10f4 100644
--- a/app/assets/javascripts/discourse/app/controllers/sidebar-section-form.js
+++ b/app/assets/javascripts/discourse/app/controllers/sidebar-section-form.js
@@ -268,8 +268,7 @@ export default Controller.extend(ModalFunctionality, {
}),
})
.then((data) => {
- this.currentUser.set(
- "sidebar_sections",
+ this.currentUser.updateSidebarSections(
this.currentUser.sidebar_sections.concat(data.sidebar_section)
);
this.send("closeModal");
@@ -310,7 +309,7 @@ export default Controller.extend(ModalFunctionality, {
return section;
}
);
- this.currentUser.set("sidebar_sections", newSidebarSections);
+ this.currentUser.updateSidebarSections(newSidebarSections);
this.send("closeModal");
})
.catch((e) =>
@@ -360,7 +359,7 @@ export default Controller.extend(ModalFunctionality, {
this.currentUser.sidebar_sections.filter((section) => {
return section.id !== data["sidebar_section"].id;
});
- this.currentUser.set("sidebar_sections", newSidebarSections);
+ this.currentUser.updateSidebarSections(newSidebarSections);
this.send("closeModal");
})
.catch((e) =>
diff --git a/app/assets/javascripts/discourse/app/lib/sidebar/community-section.js b/app/assets/javascripts/discourse/app/lib/sidebar/community-section.js
index 0a6d705a74a..2927e9be503 100644
--- a/app/assets/javascripts/discourse/app/lib/sidebar/community-section.js
+++ b/app/assets/javascripts/discourse/app/lib/sidebar/community-section.js
@@ -95,6 +95,7 @@ export default class CommunitySection {
if (this.callbackId) {
this.topicTrackingState.offStateChange(this.callbackId);
}
+
[...this.links, ...this.moreLinks].forEach((sectionLink) => {
sectionLink.teardown?.();
});
diff --git a/app/assets/javascripts/discourse/app/lib/sidebar/user/community-section/review-section-link.js b/app/assets/javascripts/discourse/app/lib/sidebar/user/community-section/review-section-link.js
index d2f8f3e1725..1ac7d1dabff 100644
--- a/app/assets/javascripts/discourse/app/lib/sidebar/user/community-section/review-section-link.js
+++ b/app/assets/javascripts/discourse/app/lib/sidebar/user/community-section/review-section-link.js
@@ -12,21 +12,18 @@ export default class ReviewSectionLink extends BaseSectionLink {
super(...arguments);
this._refreshCanDisplay();
- if (this.shouldDisplay) {
- this.appEvents.on(
- "user-reviewable-count:changed",
- this._refreshCanDisplay
- );
- }
+
+ this.appEvents?.on(
+ "user-reviewable-count:changed",
+ this._refreshCanDisplay
+ );
}
teardown() {
- if (this.shouldDisplay) {
- this.appEvents.off(
- "user-reviewable-count:changed",
- this._refreshCanDisplay
- );
- }
+ this.appEvents?.off(
+ "user-reviewable-count:changed",
+ this._refreshCanDisplay
+ );
}
@bind
diff --git a/app/assets/javascripts/discourse/app/models/user.js b/app/assets/javascripts/discourse/app/models/user.js
index d225239173e..11a00cbee68 100644
--- a/app/assets/javascripts/discourse/app/models/user.js
+++ b/app/assets/javascripts/discourse/app/models/user.js
@@ -51,6 +51,7 @@ import {
showUserTip,
} from "discourse/lib/user-tips";
import { dependentKeyCompat } from "@ember/object/compat";
+import { REFRESH_CUSTOM_SIDEBAR_SECTIONS_APP_EVENT_NAME } from "discourse/components/sidebar/user/custom-sections";
export const SECOND_FACTOR_METHODS = {
TOTP: 1,
@@ -1161,6 +1162,11 @@ const User = RestModel.extend({
this.appEvents.trigger("user-reviewable-count:changed", count);
},
+ updateSidebarSections(sections) {
+ this.set("sidebar_sections", sections);
+ this.appEvents.trigger(REFRESH_CUSTOM_SIDEBAR_SECTIONS_APP_EVENT_NAME);
+ },
+
isInDoNotDisturb() {
return (
this.do_not_disturb_until &&
diff --git a/app/assets/javascripts/discourse/tests/acceptance/sidebar-user-community-section-test.js b/app/assets/javascripts/discourse/tests/acceptance/sidebar-user-community-section-test.js
index 90c2c5d7a11..7a3558fd67f 100644
--- a/app/assets/javascripts/discourse/tests/acceptance/sidebar-user-community-section-test.js
+++ b/app/assets/javascripts/discourse/tests/acceptance/sidebar-user-community-section-test.js
@@ -791,6 +791,14 @@ acceptance("Sidebar - Logged on user - Community Section", function (needs) {
"shows suffix indicator for unread posts on everything link"
);
+ const topicTrackingState = this.container.lookup(
+ "service:topic-tracking-state"
+ );
+
+ const initialCallbackCount = Object.keys(
+ topicTrackingState.stateChangeCallbacks
+ ).length;
+
// simulate reading topic 2
await publishToMessageBus("/unread", {
topic_id: 2,
@@ -809,6 +817,12 @@ acceptance("Sidebar - Logged on user - Community Section", function (needs) {
"shows suffix indicator for new topics on categories link"
);
+ assert.equal(
+ Object.keys(topicTrackingState.stateChangeCallbacks).length,
+ initialCallbackCount,
+ "it does not add a new topic tracking state callback when the topic is read"
+ );
+
// simulate reading topic 1
await publishToMessageBus("/unread", {
topic_id: 1,