Use RateLimiter instead of waiting for each attachment to load sequentially (#7546)

Signed-off-by: denis-tingaikin <denis.tingajkin@xored.com>
This commit is contained in:
Denis Tingaikin 2024-12-25 18:31:25 +03:00 committed by GitHub
parent 84020b3305
commit faac57091c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -14,7 +14,7 @@
--> -->
<script lang="ts"> <script lang="ts">
import { Attachment } from '@hcengineering/attachment' import { Attachment } from '@hcengineering/attachment'
import { Account, Class, Doc, IdMap, Markup, Ref, Space, generateId, toIdMap } from '@hcengineering/core' import { RateLimiter, Account, Class, Doc, IdMap, Markup, Ref, Space, generateId, toIdMap } from '@hcengineering/core'
import { Asset, IntlString, setPlatformStatus, unknownError } from '@hcengineering/platform' import { Asset, IntlString, setPlatformStatus, unknownError } from '@hcengineering/platform'
import { import {
DraftController, DraftController,
@ -183,12 +183,14 @@
await tick() await tick()
const list = inputFile.files const list = inputFile.files
if (list === null || list.length === 0) return if (list === null || list.length === 0) return
const limiter = new RateLimiter(10)
for (let index = 0; index < list.length; index++) { for (let index = 0; index < list.length; index++) {
const file = list.item(index) const file = list.item(index)
if (file !== null) { if (file !== null) {
await createAttachment(file) await limiter.add(() => createAttachment(file))
} }
} }
await limiter.waitProcessing()
inputFile.value = '' inputFile.value = ''
progress = false progress = false
} }
@ -196,13 +198,16 @@
async function fileDrop (e: DragEvent): Promise<void> { async function fileDrop (e: DragEvent): Promise<void> {
progress = true progress = true
const list = e.dataTransfer?.files const list = e.dataTransfer?.files
const limiter = new RateLimiter(10)
if (list === undefined || list.length === 0) return if (list === undefined || list.length === 0) return
for (let index = 0; index < list.length; index++) { for (let index = 0; index < list.length; index++) {
const file = list.item(index) const file = list.item(index)
if (file !== null) { if (file !== null) {
await createAttachment(file) await limiter.add(() => createAttachment(file))
} }
} }
await limiter.waitProcessing()
progress = false progress = false
} }
@ -257,17 +262,17 @@
return return
} }
saved = true saved = true
const promises: Promise<any>[] = [] const limiter = new RateLimiter(10)
newAttachments.forEach((p) => { newAttachments.forEach((p) => {
const attachment = attachments.get(p) const attachment = attachments.get(p)
if (attachment !== undefined) { if (attachment !== undefined) {
promises.push(saveAttachment(attachment)) void limiter.add(() => saveAttachment(attachment))
} }
}) })
removedAttachments.forEach((p) => { removedAttachments.forEach((p) => {
promises.push(deleteAttachment(p)) void limiter.add(() => deleteAttachment(p))
}) })
await Promise.all(promises) await limiter.waitProcessing()
newAttachments.clear() newAttachments.clear()
removedAttachments.clear() removedAttachments.clear()
saveDraft() saveDraft()