A platform for community discussion. Free, open, simple.
FIX: Ensure closing sidebar tears down all callbacks. (#18434)
Alan Guo Xiang Tan4c5e575c1529
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.jsindex 9a2e5174d85..b40754fcbc2 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@@ -35,7 +35,14 @@ export default class SidebarCommunitySection extends Component {}willDestroy() {- this.sectionLinks.forEach((sectionLink) => sectionLink.teardown());+ [+ ...this.sectionLinks,+ ...this.moreSectionLinks,+ ...this.moreSecondarySectionLinks,+ ].forEach((sectionLink) => {+ sectionLink.teardown?.();+ });+this.topicTrackingState.offStateChange(this.callbackId);}diff --git a/app/assets/javascripts/discourse/app/components/sidebar/user/categories-section.js b/app/assets/javascripts/discourse/app/components/sidebar/user/categories-section.jsindex aee1619f04f..4e0d3c3e7cb 100644--- a/app/assets/javascripts/discourse/app/components/sidebar/user/categories-section.js+++ b/app/assets/javascripts/discourse/app/components/sidebar/user/categories-section.js@@ -21,6 +21,8 @@ export default class SidebarUserCategoriesSection extends SidebarCommonCategorie}willDestroy() {+ super.willDestroy(...arguments);+this.topicTrackingState.offStateChange(this.callbackId);}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.jsindex c3b0fe911ab..8f2ffef367c 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@@ -39,6 +39,8 @@ export default class SidebarUserCommunitySection extends SidebarCommonCommunityS}willDestroy() {+ super.willDestroy(...arguments);+this.appEvents.off("user-reviewable-count:changed",this._refreshSectionLinksdiff --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.jsindex f034d089f40..e56736f273d 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@@ -984,6 +984,8 @@ acceptance("Sidebar - Logged on user - Community Section", function (needs) {});test("adding section link via plugin API with callback function", async function (assert) {+ let teardownCalled = false;+withPluginApi("1.2.0", (api) => {api.addCommunitySectionLink((baseSectionLink) => {return class CustomSectionLink extends baseSectionLink {@@ -1006,6 +1008,10 @@ acceptance("Sidebar - Logged on user - Community Section", function (needs) {get text() {return "my summary";}++ get teardown() {+ teardownCalled = true;+ }};});});@@ -1035,24 +1041,9 @@ acceptance("Sidebar - Logged on user - Community Section", function (needs) {"eviltrout summary","displays the right title for the link");- });-- test("clean up topic tracking state state changed callbacks when section is destroyed", async function (assert) {- await visit("/");-- const topicTrackingState = this.container.lookup(- "service:topic-tracking-state"- );-- const initialCallbackCount = Object.keys(- topicTrackingState.stateChangeCallbacks- ).length;await click(".btn-sidebar-toggle");- assert.ok(- Object.keys(topicTrackingState.stateChangeCallbacks).length <- initialCallbackCount- );+ assert.ok(teardownCalled, "section link teardown callback was called");});});diff --git a/app/assets/javascripts/discourse/tests/acceptance/sidebar-user-test.js b/app/assets/javascripts/discourse/tests/acceptance/sidebar-user-test.jsindex 5f5aa93fca4..10ed04ae0f1 100644--- a/app/assets/javascripts/discourse/tests/acceptance/sidebar-user-test.js+++ b/app/assets/javascripts/discourse/tests/acceptance/sidebar-user-test.js@@ -169,5 +169,27 @@ acceptance("displays the mobile icon for the button");});++ test("clean up topic tracking state state changed callbacks when sidebar is destroyed", async function (assert) {+ this.siteSettings.tagging_enabled = true;++ await visit("/");++ const topicTrackingState = this.container.lookup(+ "service:topic-tracking-state"+ );++ const initialCallbackCount = Object.keys(+ topicTrackingState.stateChangeCallbacks+ ).length;++ await click(".btn-sidebar-toggle");++ assert.strictEqual(+ Object.keys(topicTrackingState.stateChangeCallbacks).length,+ initialCallbackCount - 3,+ "the 3 topic tracking state change callbacks are removed"+ );+ });});