diff options
author | skjnldsv <skjnldsv@protonmail.com> | 2024-12-11 19:05:24 +0100 |
---|---|---|
committer | backportbot[bot] <backportbot[bot]@users.noreply.github.com> | 2024-12-11 19:39:59 +0000 |
commit | dadab6ed149728dd45a34d930ca415912694b7c8 (patch) | |
tree | d2a62fec95b5c9e264a91021a3698ce3fb25773b | |
parent | a793438b0d209da6c04072aeafa0a7cca7ec49dd (diff) | |
download | nextcloud-server-dadab6ed149728dd45a34d930ca415912694b7c8.tar.gz nextcloud-server-dadab6ed149728dd45a34d930ca415912694b7c8.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 ac2254f534f..33a68e8da05 100644 --- a/apps/files/src/actions/favoriteAction.ts +++ b/apps/files/src/actions/favoriteAction.ts @@ -24,6 +24,7 @@ import { generateUrl } from '@nextcloud/router' import { Permission, type Node, View, FileAction } from '@nextcloud/files' import { translate as t } from '@nextcloud/l10n' import axios from '@nextcloud/axios' +import PQueue from 'p-queue' import Vue from 'vue' import StarOutlineSvg from '@mdi/svg/svg/star-outline.svg?raw' @@ -32,6 +33,8 @@ import StarSvg from '@mdi/svg/svg/star.svg?raw' import logger from '../logger.js' import { encodePath } from '@nextcloud/paths' +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) @@ -97,7 +100,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, |