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

Commit 6b384fe

Browse files
authored
Fix huge usage bandwidth and performance issue of pinned message banner. (#37)
* Return only the first 100 pinned messages * Execute pinned message 10 by 10
1 parent 5740bdb commit 6b384fe

File tree

5 files changed

+100
-11
lines changed

5 files changed

+100
-11
lines changed

src/hooks/usePinnedEvents.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,22 @@ import { ReadPinsEventId } from "../components/views/right_panel/types";
2424
import { useMatrixClientContext } from "../contexts/MatrixClientContext";
2525
import { useAsyncMemo } from "./useAsyncMemo";
2626
import PinningUtils from "../utils/PinningUtils";
27+
import { batch } from "../utils/promise.ts";
2728

2829
/**
2930
* Get the pinned event IDs from a room.
31+
* The number of pinned events is limited to 100.
3032
* @param room
3133
*/
3234
function getPinnedEventIds(room?: Room): string[] {
33-
return (
35+
const eventIds: string[] =
3436
room
3537
?.getLiveTimeline()
3638
.getState(EventTimeline.FORWARDS)
3739
?.getStateEvents(EventType.RoomPinnedEvents, "")
38-
?.getContent()?.pinned ?? []
39-
);
40+
?.getContent()?.pinned ?? [];
41+
// Limit the number of pinned events to 100
42+
return eventIds.slice(0, 100);
4043
}
4144

4245
/**
@@ -173,12 +176,11 @@ export function useFetchedPinnedEvents(room: Room, pinnedEventIds: string[]): Ar
173176
const cli = useMatrixClientContext();
174177

175178
return useAsyncMemo(
176-
() =>
177-
Promise.all(
178-
pinnedEventIds.map(
179-
async (eventId): Promise<MatrixEvent | null> => fetchPinnedEvent(room, eventId, cli),
180-
),
181-
),
179+
() => {
180+
const fetchPromises = pinnedEventIds.map((eventId) => () => fetchPinnedEvent(room, eventId, cli));
181+
// Fetch the pinned events in batches of 10
182+
return batch(fetchPromises, 10);
183+
},
182184
[cli, room, pinnedEventIds],
183185
null,
184186
);

src/utils/promise.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,18 @@ export async function retry<T, E extends Error>(
4040
}
4141
throw lastErr;
4242
}
43+
44+
/**
45+
* Batch promises into groups of a given size.
46+
* Execute the promises in parallel, but wait for all promises in a batch to resolve before moving to the next batch.
47+
* @param funcs - The promises to batch
48+
* @param batchSize - The number of promises to execute in parallel
49+
*/
50+
export async function batch<T>(funcs: Array<() => Promise<T>>, batchSize: number): Promise<T[]> {
51+
const results: T[] = [];
52+
for (let i = 0; i < funcs.length; i += batchSize) {
53+
const batch = funcs.slice(i, i + batchSize);
54+
results.push(...(await Promise.all(batch.map((f) => f()))));
55+
}
56+
return results;
57+
}

test/components/views/right_panel/PinnedMessagesCard-test.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,21 @@ describe("<PinnedMessagesCard />", () => {
196196
expect(asFragment()).toMatchSnapshot();
197197
});
198198

199+
it("should not show more than 100 messages", async () => {
200+
const events = Array.from({ length: 120 }, (_, i) =>
201+
mkMessage({
202+
event: true,
203+
room: "!room:example.org",
204+
user: "@alice:example.org",
205+
msg: `The message ${i}`,
206+
ts: i,
207+
}),
208+
);
209+
await initPinnedMessagesCard(events, []);
210+
211+
expect(screen.queryAllByRole("listitem")).toHaveLength(100);
212+
});
213+
199214
it("should updates when messages are pinned", async () => {
200215
// Start with nothing pinned
201216
const { addLocalPinEvent, addNonLocalPinEvent } = await initPinnedMessagesCard([], []);

test/components/views/right_panel/__snapshots__/PinnedMessagesCard-test.tsx.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ exports[`<PinnedMessagesCard /> unpin all should not allow to unpinall 1`] = `
358358
aria-label="Open menu"
359359
class="_icon-button_bh2qc_17"
360360
data-state="closed"
361-
id="radix-18"
361+
id="radix-218"
362362
role="button"
363363
style="--cpd-icon-button-size: 24px;"
364364
tabindex="0"
@@ -424,7 +424,7 @@ exports[`<PinnedMessagesCard /> unpin all should not allow to unpinall 1`] = `
424424
aria-label="Open menu"
425425
class="_icon-button_bh2qc_17"
426426
data-state="closed"
427-
id="radix-19"
427+
id="radix-219"
428428
role="button"
429429
style="--cpd-icon-button-size: 24px;"
430430
tabindex="0"

test/utils/promise-test.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2024 New Vector Ltd.
3+
*
4+
* SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
5+
* Please see LICENSE files in the repository root for full details.
6+
*
7+
*/
8+
9+
import { batch } from "../../src/utils/promise.ts";
10+
11+
describe("promise.ts", () => {
12+
describe("batch", () => {
13+
afterEach(() => jest.useRealTimers());
14+
15+
it("should batch promises into groups of a given size", async () => {
16+
const promises = [() => Promise.resolve(1), () => Promise.resolve(2), () => Promise.resolve(3)];
17+
const batchSize = 2;
18+
const result = await batch(promises, batchSize);
19+
expect(result).toEqual([1, 2, 3]);
20+
});
21+
22+
it("should wait for the current batch to finish to request the next one", async () => {
23+
jest.useFakeTimers();
24+
25+
let promise1Called = false;
26+
const promise1 = () =>
27+
new Promise<number>((resolve) => {
28+
promise1Called = true;
29+
resolve(1);
30+
});
31+
let promise2Called = false;
32+
const promise2 = () =>
33+
new Promise<number>((resolve) => {
34+
promise2Called = true;
35+
setTimeout(() => {
36+
resolve(2);
37+
}, 10);
38+
});
39+
40+
let promise3Called = false;
41+
const promise3 = () =>
42+
new Promise<number>((resolve) => {
43+
promise3Called = true;
44+
resolve(3);
45+
});
46+
const batchSize = 2;
47+
const batchPromise = batch([promise1, promise2, promise3], batchSize);
48+
49+
expect(promise1Called).toBe(true);
50+
expect(promise2Called).toBe(true);
51+
expect(promise3Called).toBe(false);
52+
53+
jest.advanceTimersByTime(11);
54+
expect(await batchPromise).toEqual([1, 2, 3]);
55+
});
56+
});
57+
});

0 commit comments

Comments
 (0)