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

Commit

Permalink
Fix freeze on room switch (#7884)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
dbkr and turt2live authored Feb 23, 2022
1 parent 5c5dc69 commit 81cda7c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
16 changes: 13 additions & 3 deletions src/utils/permalinks/Permalinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ limitations under the License.
*/

import isIp from "is-ip";
import { throttle } from "lodash";
import * as utils from "matrix-js-sdk/src/utils";
import { Room } from "matrix-js-sdk/src/models/room";
import { EventType } from "matrix-js-sdk/src/@types/event";
Expand Down Expand Up @@ -91,7 +92,10 @@ export class RoomPermalinkCreator {
// We support being given a roomId as a fallback in the event the `room` object
// doesn't exist or is not healthy for us to rely on. For example, loading a
// permalink to a room which the MatrixClient doesn't know about.
constructor(room: Room, roomId: string = null) {
// Some of the tests done by this class are relatively expensive, so normally
// throttled to not happen on every update. Pass false as the shouldThrottle
// param to disable this behaviour, eg. for tests.
constructor(room: Room, roomId: string | null = null, shouldThrottle = true) {
this.room = room;
this.roomId = room ? room.roomId : roomId;
this.highestPlUserId = null;
Expand All @@ -104,6 +108,12 @@ export class RoomPermalinkCreator {
if (!this.roomId) {
throw new Error("Failed to resolve a roomId for the permalink creator to use");
}

if (shouldThrottle) {
this.updateServerCandidates = throttle(
this.updateServerCandidates, 200, { leading: true, trailing: true },
);
}
}

load() {
Expand Down Expand Up @@ -260,7 +270,7 @@ export class RoomPermalinkCreator {
this.populationMap = populationMap;
}

private updateServerCandidates() {
private updateServerCandidates = () => {
let candidates = [];
if (this.highestPlUserId) {
candidates.push(getServerName(this.highestPlUserId));
Expand All @@ -279,7 +289,7 @@ export class RoomPermalinkCreator {
candidates = candidates.concat(remainingServers);

this._serverCandidates = candidates;
}
};
}

export function makeGenericPermalink(entityId: string): string {
Expand Down
2 changes: 1 addition & 1 deletion test/utils/permalinks/Permalinks-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ describe('Permalinks', function() {
},
member95,
]);
const creator = new RoomPermalinkCreator(room);
const creator = new RoomPermalinkCreator(room, null, false);
creator.load();
expect(creator._serverCandidates[0]).toBe("pl_95");
member95.membership = "left";
Expand Down

0 comments on commit 81cda7c

Please sign in to comment.