mirror of
https://github.com/nextcloud/server.git
synced 2026-04-29 10:03:32 -04:00
fix(files): throttle favorite with max 5 simultaneous requests
Signed-off-by: skjnldsv <skjnldsv@protonmail.com>
This commit is contained in:
parent
00d225aacb
commit
9205f373fc
1 changed files with 22 additions and 1 deletions
|
|
@ -7,6 +7,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'
|
||||
|
|
@ -15,6 +16,8 @@ import StarSvg from '@mdi/svg/svg/star.svg?raw'
|
|||
import logger from '../logger.ts'
|
||||
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)
|
||||
|
|
@ -80,7 +83,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,
|
||||
|
|
|
|||
Loading…
Reference in a new issue