You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-11-28 05:03:59 +03:00
Cache the joined member count for a room state
Pushrule evaluation needs the count of joined room members and filtering the list for joined members takes a nontrivial amount of time, but caching it is trivial, both code and memory wise.
This commit is contained in:
@@ -69,9 +69,24 @@ function RoomState(roomId) {
|
|||||||
this._displayNameToUserIds = {};
|
this._displayNameToUserIds = {};
|
||||||
this._userIdsToDisplayNames = {};
|
this._userIdsToDisplayNames = {};
|
||||||
this._tokenToInvite = {}; // 3pid invite state_key to m.room.member invite
|
this._tokenToInvite = {}; // 3pid invite state_key to m.room.member invite
|
||||||
|
this._joinedMemberCount = null; // cache of the number of joined members
|
||||||
}
|
}
|
||||||
utils.inherits(RoomState, EventEmitter);
|
utils.inherits(RoomState, EventEmitter);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the number of joined members in this room
|
||||||
|
* This method caches the result.
|
||||||
|
* @return {integer} The number of members in this room whose membership is 'join'
|
||||||
|
*/
|
||||||
|
RoomState.prototype.getJoinedMemberCount = function() {
|
||||||
|
if (this._joinedMemberCount === null) {
|
||||||
|
this._joinedMemberCount = this.getMembers().filter((m) => {
|
||||||
|
return m.membership === 'join';
|
||||||
|
}).length;
|
||||||
|
}
|
||||||
|
return this._joinedMemberCount;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get all RoomMembers in this room.
|
* Get all RoomMembers in this room.
|
||||||
* @return {Array<RoomMember>} A list of RoomMembers.
|
* @return {Array<RoomMember>} A list of RoomMembers.
|
||||||
@@ -218,6 +233,7 @@ RoomState.prototype.setStateEvents = function(stateEvents) {
|
|||||||
delete self._sentinels[userId];
|
delete self._sentinels[userId];
|
||||||
|
|
||||||
self.members[userId] = member;
|
self.members[userId] = member;
|
||||||
|
self._joinedMemberCount = null;
|
||||||
self.emit("RoomState.members", event, self, member);
|
self.emit("RoomState.members", event, self, member);
|
||||||
} else if (event.getType() === "m.room.power_levels") {
|
} else if (event.getType() === "m.room.power_levels") {
|
||||||
const members = utils.values(self.members);
|
const members = utils.values(self.members);
|
||||||
|
|||||||
@@ -156,9 +156,7 @@ function PushProcessor(client) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const memberCount = Object.keys(room.currentState.members).filter(function(m) {
|
const memberCount = room.currentState.getJoinedMemberCount();
|
||||||
return room.currentState.members[m].membership == 'join';
|
|
||||||
}).length;
|
|
||||||
|
|
||||||
const m = cond.is.match(/^([=<>]*)([0-9]*)$/);
|
const m = cond.is.match(/^([=<>]*)([0-9]*)$/);
|
||||||
if (!m) {
|
if (!m) {
|
||||||
|
|||||||
Reference in New Issue
Block a user