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

Commit a1b369a

Browse files
committed
Execute pinned message 10 by 10
1 parent 52846d1 commit a1b369a

File tree

3 files changed

+78
-6
lines changed

3 files changed

+78
-6
lines changed

src/hooks/usePinnedEvents.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ 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.
@@ -175,12 +176,11 @@ export function useFetchedPinnedEvents(room: Room, pinnedEventIds: string[]): Ar
175176
const cli = useMatrixClientContext();
176177

177178
return useAsyncMemo(
178-
() =>
179-
Promise.all(
180-
pinnedEventIds.map(
181-
async (eventId): Promise<MatrixEvent | null> => fetchPinnedEvent(room, eventId, cli),
182-
),
183-
),
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+
},
184184
[cli, room, pinnedEventIds],
185185
null,
186186
);

src/utils/promise.ts

+15
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/utils/promise-test.ts

+57
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+
setTimeout(() => {
35+
promise2Called = true;
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(false);
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)