From a947b7b839cfdd7436d533752d4df3eee1ce8bec Mon Sep 17 00:00:00 2001 From: Joffrey JAFFEUX Date: Mon, 6 Apr 2020 15:06:26 +0200 Subject: [PATCH] FIX: prevents registering multiple `topic-notifications-button:changed` (#9356) A large topic page will always have the bottom tracking button, and will also have the timeline, meaning we already had 2 tracking events. But it gets even worse when you know that the timeline button is a component connector which will trigger `didInsertElement` very frequently, meaning we were constantly adding more and more appEvents handlers. --- .../discourse/widgets/topic-timeline.js | 3 ++- .../components/topic-notifications-button.js | 24 +++++++++++-------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/app/assets/javascripts/discourse/widgets/topic-timeline.js b/app/assets/javascripts/discourse/widgets/topic-timeline.js index 617a13bcd48..5f3cabfb8e8 100644 --- a/app/assets/javascripts/discourse/widgets/topic-timeline.js +++ b/app/assets/javascripts/discourse/widgets/topic-timeline.js @@ -388,7 +388,8 @@ createWidget("timeline-footer-controls", { topic, showFullTitle: false, appendReason: false, - placement: "bottom-end" + placement: "bottom-end", + mountedAsWidget: true }, ["notificationLevel"] ) diff --git a/app/assets/javascripts/select-kit/components/topic-notifications-button.js b/app/assets/javascripts/select-kit/components/topic-notifications-button.js index 747685c2aef..a0c16738b5b 100644 --- a/app/assets/javascripts/select-kit/components/topic-notifications-button.js +++ b/app/assets/javascripts/select-kit/components/topic-notifications-button.js @@ -10,21 +10,25 @@ export default Component.extend({ didInsertElement() { this._super(...arguments); - this.appEvents.on( - "topic-notifications-button:changed", - this, - "_changeTopicNotificationLevel" - ); + if (!this.mountedAsWidget) { + this.appEvents.on( + "topic-notifications-button:changed", + this, + "_changeTopicNotificationLevel" + ); + } }, willDestroyElement() { this._super(...arguments); - this.appEvents.off( - "topic-notifications-button:changed", - this, - "_changeTopicNotificationLevel" - ); + if (!this.mountedAsWidget) { + this.appEvents.off( + "topic-notifications-button:changed", + this, + "_changeTopicNotificationLevel" + ); + } }, _changeTopicNotificationLevel(level) {