DEV: Ensure topic-tracking-state state change callbacks are cleaned up (#17069)

This commit is contained in:
Alan Guo Xiang Tan 2022-06-13 14:53:02 +08:00 committed by GitHub
parent 94c3bbc2d1
commit 95fa4c5d52
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 15 deletions

View File

@ -4,6 +4,20 @@ import GlimmerComponent from "discourse/components/glimmer";
import CategorySectionLink from "discourse/lib/sidebar/categories-section/category-section-link";
export default class SidebarCategoriesSection extends GlimmerComponent {
constructor() {
super(...arguments);
this.callbackId = this.topicTrackingState.onStateChange(() => {
this.sectionLinks.forEach((sectionLink) => {
sectionLink.refreshCounts();
});
});
}
willDestroy() {
this.topicTrackingState.offStateChange(this.callbackId);
}
@cached
get sectionLinks() {
return this.site.trackedCategoriesList.map((trackedCategory) => {
@ -13,8 +27,4 @@ export default class SidebarCategoriesSection extends GlimmerComponent {
});
});
}
willDestroy() {
this.sectionLinks.forEach((sectionLink) => sectionLink.teardown());
}
}

View File

@ -14,20 +14,11 @@ export default class CategorySectionLink {
constructor({ category, topicTrackingState }) {
this.category = category;
this.topicTrackingState = topicTrackingState;
this.callbackId = this.topicTrackingState.onStateChange(
this._refreshCounts
);
this._refreshCounts();
}
teardown() {
this.topicTrackingState.offStateChange(this.callbackId);
this.refreshCounts();
}
@bind
_refreshCounts() {
refreshCounts() {
this.totalUnread = this.topicTrackingState.countUnread({
categoryId: this.category.id,
});