Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable30] fix(files): throttle favorite with max 5 simultaneous requests #49805

Merged
merged 3 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion apps/files/src/actions/favoriteAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions dist/files-init.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/files-init.js.map

Large diffs are not rendered by default.

Loading