mirror of
https://github.com/vector-im/element-web.git
synced 2026-05-05 04:06:44 +02:00
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:
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.
|
||||
*/
|
||||
|
||||
import { throttle } from "lodash";
|
||||
import { type Room } from "matrix-js-sdk/src/matrix";
|
||||
|
||||
import { NotificationLevel } from "./NotificationLevel";
|
||||
@ -63,23 +64,27 @@ export class SpaceNotificationState extends NotificationState {
|
||||
this.calculateTotalState();
|
||||
};
|
||||
|
||||
private calculateTotalState(): void {
|
||||
const snapshot = this.snapshot();
|
||||
private calculateTotalState = throttle(
|
||||
(): void => {
|
||||
const snapshot = this.snapshot();
|
||||
|
||||
this._count = 0;
|
||||
this._level = NotificationLevel.None;
|
||||
for (const [roomId, state] of Object.entries(this.states)) {
|
||||
const room = this.rooms.find((r) => r.roomId === roomId);
|
||||
const roomTags = room ? RoomListStore.instance.getTagsForRoom(room) : [];
|
||||
this._count = 0;
|
||||
this._level = NotificationLevel.None;
|
||||
for (const [roomId, state] of Object.entries(this.states)) {
|
||||
const room = this.rooms.find((r) => r.roomId === roomId);
|
||||
const roomTags = room ? RoomListStore.instance.getTagsForRoom(room) : [];
|
||||
|
||||
// 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;
|
||||
// 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;
|
||||
|
||||
this._count += state.count;
|
||||
this._level = Math.max(this.level, state.level);
|
||||
}
|
||||
this._count += state.count;
|
||||
this._level = Math.max(this.level, state.level);
|
||||
}
|
||||
|
||||
// finally, publish an update if needed
|
||||
this.emitIfUpdated(snapshot);
|
||||
}
|
||||
// finally, publish an update if needed
|
||||
this.emitIfUpdated(snapshot);
|
||||
},
|
||||
100,
|
||||
{ leading: false, trailing: true },
|
||||
);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user