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:
@@ -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,23 +64,27 @@ export class SpaceNotificationState extends NotificationState {
|
|||||||
this.calculateTotalState();
|
this.calculateTotalState();
|
||||||
};
|
};
|
||||||
|
|
||||||
private calculateTotalState(): void {
|
private calculateTotalState = throttle(
|
||||||
const snapshot = this.snapshot();
|
(): void => {
|
||||||
|
const snapshot = this.snapshot();
|
||||||
|
|
||||||
this._count = 0;
|
this._count = 0;
|
||||||
this._level = NotificationLevel.None;
|
this._level = NotificationLevel.None;
|
||||||
for (const [roomId, state] of Object.entries(this.states)) {
|
for (const [roomId, state] of Object.entries(this.states)) {
|
||||||
const room = this.rooms.find((r) => r.roomId === roomId);
|
const room = this.rooms.find((r) => r.roomId === roomId);
|
||||||
const roomTags = room ? RoomListStore.instance.getTagsForRoom(room) : [];
|
const roomTags = room ? RoomListStore.instance.getTagsForRoom(room) : [];
|
||||||
|
|
||||||
// We ignore unreads in LowPriority rooms, see https://github.com/vector-im/element-web/issues/16836
|
// We ignore unreads in LowPriority rooms, see https://github.com/vector-im/element-web/issues/16836
|
||||||
if (roomTags.includes(DefaultTagID.LowPriority) && state.level === NotificationLevel.Activity) continue;
|
if (roomTags.includes(DefaultTagID.LowPriority) && state.level === NotificationLevel.Activity) continue;
|
||||||
|
|
||||||
this._count += state.count;
|
this._count += state.count;
|
||||||
this._level = Math.max(this.level, state.level);
|
this._level = Math.max(this.level, state.level);
|
||||||
}
|
}
|
||||||
|
|
||||||
// finally, publish an update if needed
|
// finally, publish an update if needed
|
||||||
this.emitIfUpdated(snapshot);
|
this.emitIfUpdated(snapshot);
|
||||||
}
|
},
|
||||||
|
100,
|
||||||
|
{ leading: false, trailing: true },
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user