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

Fix freeze on room switch #7884

Merged
merged 4 commits into from
Feb 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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