Skip to content

Commit

Permalink
fix: limit
Browse files Browse the repository at this point in the history
  • Loading branch information
ioanmo226 committed May 21, 2024
1 parent ec27373 commit a63ca8f
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class InboxListThreadsModule extends ViewModule<InboxView> {
if (threads?.length) {
await promiseAllWithLimit(
10,
threads.map(t => this.renderInboxItem(t.id))
threads.map(t => () => this.renderInboxItem(t.id))
);
} else {
Xss.sanitizeRender('.threads', `<p>No encrypted messages in ${Xss.escape(labelId)} yet. ${Ui.retryLink()}</p>`);
Expand Down
2 changes: 1 addition & 1 deletion extension/js/common/api/email-provider/gmail/gmail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export class Gmail extends EmailProviderApi implements EmailProviderInterface {
public msgsGet = async (msgIds: string[], format: GmailResponseFormat): Promise<GmailRes.GmailMsg[]> => {
return await promiseAllWithLimit(
10,
msgIds.map(id => this.msgGet(id, format))
msgIds.map(id => () => this.msgGet(id, format))
);
};

Expand Down
7 changes: 4 additions & 3 deletions extension/js/common/core/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -523,13 +523,14 @@ export const checkValidURL = (url: string): boolean => {
* Resolves when all promises are resolved or rejects when any promise is rejected.
*
* @param concurrency - The maximum number of promises to run at the same time.
* @param tasks - An array of promises.
* @param tasks - An array of functions that return promises.
* @returns A Promise that resolves to an array of the resolved values of the input promises.
*/
export const promiseAllWithLimit = async <V>(concurrency: number, tasks: Promise<V>[]): Promise<V[]> => {
export const promiseAllWithLimit = async <V>(concurrency: number, tasks: (() => Promise<V>)[]): Promise<V[]> => {
let results: V[] = [];
while (tasks.length) {
results = results.concat(await Promise.all(tasks.splice(0, concurrency)));
const currentTasks = tasks.splice(0, concurrency).map(task => task());
results = results.concat(await Promise.all(currentTasks));
}
return results;
};

0 comments on commit a63ca8f

Please sign in to comment.