Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 81cda7c

Browse files
dbkrturt2live
andauthored
Fix freeze on room switch (#7884)
* Fix freeze on room switch updateServerCandidates was called on every room member event and not throttled. Fixes element-hq/element-web#21127 * Move import * Disable throttling in tests * Types Co-authored-by: Travis Ralston <travisr@matrix.org> Co-authored-by: Travis Ralston <travisr@matrix.org>
1 parent 5c5dc69 commit 81cda7c

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

src/utils/permalinks/Permalinks.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ limitations under the License.
1515
*/
1616

1717
import isIp from "is-ip";
18+
import { throttle } from "lodash";
1819
import * as utils from "matrix-js-sdk/src/utils";
1920
import { Room } from "matrix-js-sdk/src/models/room";
2021
import { EventType } from "matrix-js-sdk/src/@types/event";
@@ -91,7 +92,10 @@ export class RoomPermalinkCreator {
9192
// We support being given a roomId as a fallback in the event the `room` object
9293
// doesn't exist or is not healthy for us to rely on. For example, loading a
9394
// permalink to a room which the MatrixClient doesn't know about.
94-
constructor(room: Room, roomId: string = null) {
95+
// Some of the tests done by this class are relatively expensive, so normally
96+
// throttled to not happen on every update. Pass false as the shouldThrottle
97+
// param to disable this behaviour, eg. for tests.
98+
constructor(room: Room, roomId: string | null = null, shouldThrottle = true) {
9599
this.room = room;
96100
this.roomId = room ? room.roomId : roomId;
97101
this.highestPlUserId = null;
@@ -104,6 +108,12 @@ export class RoomPermalinkCreator {
104108
if (!this.roomId) {
105109
throw new Error("Failed to resolve a roomId for the permalink creator to use");
106110
}
111+
112+
if (shouldThrottle) {
113+
this.updateServerCandidates = throttle(
114+
this.updateServerCandidates, 200, { leading: true, trailing: true },
115+
);
116+
}
107117
}
108118

109119
load() {
@@ -260,7 +270,7 @@ export class RoomPermalinkCreator {
260270
this.populationMap = populationMap;
261271
}
262272

263-
private updateServerCandidates() {
273+
private updateServerCandidates = () => {
264274
let candidates = [];
265275
if (this.highestPlUserId) {
266276
candidates.push(getServerName(this.highestPlUserId));
@@ -279,7 +289,7 @@ export class RoomPermalinkCreator {
279289
candidates = candidates.concat(remainingServers);
280290

281291
this._serverCandidates = candidates;
282-
}
292+
};
283293
}
284294

285295
export function makeGenericPermalink(entityId: string): string {

test/utils/permalinks/Permalinks-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ describe('Permalinks', function() {
122122
},
123123
member95,
124124
]);
125-
const creator = new RoomPermalinkCreator(room);
125+
const creator = new RoomPermalinkCreator(room, null, false);
126126
creator.load();
127127
expect(creator._serverCandidates[0]).toBe("pl_95");
128128
member95.membership = "left";

0 commit comments

Comments
 (0)