diff options
author | skjnldsv <skjnldsv@protonmail.com> | 2024-12-11 19:05:24 +0100 |
---|---|---|
committer | skjnldsv <skjnldsv@protonmail.com> | 2024-12-11 19:05:24 +0100 |
commit | 0862632b2c8f11cd5d3742a7061e2797ec59f230 (patch) | |
tree | 1e16fc58fbd0369a80820db32baf94d6af6175ec | |
parent | 09d4a3f84245f9022180f6894edb590c3cf5fd34 (diff) | |
download | nextcloud-server-0862632b2c8f11cd5d3742a7061e2797ec59f230.tar.gz nextcloud-server-0862632b2c8f11cd5d3742a7061e2797ec59f230.zip |
fix(files): throttle favorite with max 5 simultaneous requests
Signed-off-by: skjnldsv <skjnldsv@protonmail.com>
-rw-r--r-- | apps/files/src/actions/favoriteAction.ts | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/apps/files/src/actions/favoriteAction.ts b/apps/files/src/actions/favoriteAction.ts index 7d7e6c01e03..ad1994cc916 100644 --- a/apps/files/src/actions/favoriteAction.ts +++ b/apps/files/src/actions/favoriteAction.ts @@ -11,6 +11,7 @@ import { encodePath } from '@nextcloud/paths' import { generateUrl } from '@nextcloud/router' import { isPublicShare } from '@nextcloud/sharing/public' import axios from '@nextcloud/axios' +import PQueue from 'p-queue' import Vue from 'vue' import StarOutlineSvg from '@mdi/svg/svg/star-outline.svg?raw' @@ -18,6 +19,8 @@ import StarSvg from '@mdi/svg/svg/star.svg?raw' import logger from '../logger.ts' +const queue = new PQueue({ concurrency: 5 }) + // If any of the nodes is not favorited, we display the favorite action. const shouldFavorite = (nodes: Node[]): boolean => { return nodes.some(node => node.attributes.favorite !== 1) @@ -91,7 +94,25 @@ export const action = new FileAction({ }, async execBatch(nodes: Node[], view: View) { const willFavorite = shouldFavorite(nodes) - return Promise.all(nodes.map(async node => await favoriteNode(node, view, willFavorite))) + + // Map each node to a promise that resolves with the result of exec(node) + const promises = nodes.map(node => { + // Create a promise that resolves with the result of exec(node) + const promise = new Promise<boolean>(resolve => { + queue.add(async () => { + try { + await favoriteNode(node, view, willFavorite) + resolve(true) + } catch (error) { + logger.error('Error while adding file to favorite', { error, source: node.source, node }) + resolve(false) + } + }) + }) + return promise + }) + + return Promise.all(promises) }, order: -50, |