DEV: Unsubscribe from MessageBus channel when leaving review-index route (#18395)

We subscribe to a couple of MessageBus channels when the review-index route is entered, but we should unsubscribe when exiting the route otherwise callbacks would leak every time the review-index is entered and that might cause subtle and weird bugs or errors.
This commit is contained in:
Osama Sayegh 2022-09-28 12:58:19 +03:00 committed by GitHub
parent 567fcaecb8
commit b10b5eb17b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 21 additions and 9 deletions

View File

@ -51,7 +51,7 @@ export default DiscourseRoute.extend({
},
activate() {
this.messageBus.subscribe("/reviewable_claimed", (data) => {
this._updateClaimedBy = (data) => {
const reviewables = this.controller.reviewables;
if (reviewables) {
const user = data.user
@ -63,13 +63,9 @@ export default DiscourseRoute.extend({
}
});
}
});
};
const channel = this.currentUser.redesigned_user_menu_enabled
? `/reviewable_counts/${this.currentUser.id}`
: "/reviewable_counts";
this.messageBus.subscribe(channel, (data) => {
this._updateReviewables = (data) => {
if (data.updates) {
this.controller.reviewables.forEach((reviewable) => {
const updates = data.updates[reviewable.id];
@ -78,15 +74,31 @@ export default DiscourseRoute.extend({
}
});
}
});
};
this.messageBus.subscribe("/reviewable_claimed", this._updateClaimedBy);
this.messageBus.subscribe(
this._reviewableCountsChannel(),
this._updateReviewables
);
},
deactivate() {
this.messageBus.unsubscribe("/reviewable_claimed");
this.messageBus.unsubscribe("/reviewable_claimed", this._updateClaimedBy);
this.messageBus.unsubscribe(
this._reviewableCountsChannel(),
this._updateReviewables
);
},
@action
refreshRoute() {
this.refresh();
},
_reviewableCountsChannel() {
return this.currentUser.redesigned_user_menu_enabled
? `/reviewable_counts/${this.currentUser.id}`
: "/reviewable_counts";
},
});