Throttle notification state calculation (#31922)

Because every room in a space will emit a notification state change
when push rules change so we would otherwise recalculate the space
notification state for every room in the space, On^2 style.
This commit is contained in:
David Baker
2026-01-30 17:09:02 +00:00
committed by GitHub
parent 25d24d478f
commit 7dbffb348d
@@ -6,6 +6,7 @@ SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Com
Please see LICENSE files in the repository root for full details. Please see LICENSE files in the repository root for full details.
*/ */
import { throttle } from "lodash";
import { type Room } from "matrix-js-sdk/src/matrix"; import { type Room } from "matrix-js-sdk/src/matrix";
import { NotificationLevel } from "./NotificationLevel"; import { NotificationLevel } from "./NotificationLevel";
@@ -63,7 +64,8 @@ export class SpaceNotificationState extends NotificationState {
this.calculateTotalState(); this.calculateTotalState();
}; };
private calculateTotalState(): void { private calculateTotalState = throttle(
(): void => {
const snapshot = this.snapshot(); const snapshot = this.snapshot();
this._count = 0; this._count = 0;
@@ -81,5 +83,8 @@ export class SpaceNotificationState extends NotificationState {
// finally, publish an update if needed // finally, publish an update if needed
this.emitIfUpdated(snapshot); this.emitIfUpdated(snapshot);
} },
100,
{ leading: false, trailing: true },
);
} }