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

feat: support concurrency limit #541

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ React.render(<Upload />, container);
|style | object | {}| root component inline style |
|className | string | - | root component className |
|disabled | boolean | false | whether disabled |
|component | "div"|"span" | "span"| wrap component name |
|component | "div"|"span" | "span"|
|action| string &#124; function(file): string &#124; Promise&lt;string&gt; | | form action url |
|method | string | post | request method |
|directory| boolean | false | support upload whole directory |
Expand All @@ -69,6 +69,7 @@ React.render(<Upload />, container);
|accept | string | | input accept attribute |
|capture | string | | input capture attribute |
|multiple | boolean | false | only support ie10+|
|concurrencyLimit | number | | asynchronously posts files with the concurrency limit |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

concurrencyLimit 的默认值是多少?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

默认值是 undefined,即不使用 concurrencyLimit

|onStart | function| | start upload file |
|onError| function| | error callback |
|onSuccess | function | | success callback |
Expand Down
16 changes: 10 additions & 6 deletions src/AjaxUploader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import defaultRequest from './request';
import traverseFileTree from './traverseFileTree';
import getUid from './uid';
import asyncPool from './asyncPool';

interface ParsedFileInfo {
origin: RcFile;
Expand Down Expand Up @@ -111,17 +112,20 @@
return this.processFile(file, originFiles);
});

const { onBatchStart, concurrencyLimit } = this.props;
// Batch upload files
Promise.all(postFiles).then(fileList => {
const { onBatchStart } = this.props;

onBatchStart?.(fileList.map(({ origin, parsedFile }) => ({ file: origin, parsedFile })));

fileList
.filter(file => file.parsedFile !== null)
.forEach(file => {
const parsedFiles = fileList.filter(file => file.parsedFile !== null);
if (concurrencyLimit) {
// Asynchronously posts files with the concurrency limit.
asyncPool(concurrencyLimit, parsedFiles, async file => this.post(file));

Check warning on line 123 in src/AjaxUploader.tsx

View check run for this annotation

Codecov / codecov/patch

src/AjaxUploader.tsx#L123

Added line #L123 was not covered by tests
} else {
parsedFiles.forEach(file => {
this.post(file);
});
}
});
};

Expand Down Expand Up @@ -162,7 +166,7 @@
const { data } = this.props;
let mergedData: Record<string, unknown>;
if (typeof data === 'function') {
mergedData = await data(file);
mergedData = data(file);
} else {
mergedData = data;
}
Expand Down
39 changes: 39 additions & 0 deletions src/asyncPool.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Asynchronously processes an array of items with a concurrency limit.
*
* @template T - Type of the input items.
* @template U - Type of the result of the asynchronous task.
*
* @param {number} concurrencyLimit - The maximum number of asynchronous tasks to execute concurrently.
* @param {T[]} items - The array of items to process asynchronously.
* @param {(item: T) => Promise<U>} asyncTask - The asynchronous task to be performed on each item.
*
* @returns {Promise<U[]>} - A promise that resolves to an array of results from the asynchronous tasks.
*/
export default async function asyncPool<T, U>(
concurrencyLimit: number,
items: T[],
asyncTask: (item: T) => Promise<U>,
): Promise<U[]> {
const tasks: Promise<U>[] = [];
const pendings: Promise<U>[] = []

Check warning on line 19 in src/asyncPool.ts

View check run for this annotation

Codecov / codecov/patch

src/asyncPool.ts#L17-L19

Added lines #L17 - L19 were not covered by tests


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

多了一个空行。

for (const item of items) {
const task = asyncTask(item);
tasks.push(task);

Check warning on line 24 in src/asyncPool.ts

View check run for this annotation

Codecov / codecov/patch

src/asyncPool.ts#L22-L24

Added lines #L22 - L24 were not covered by tests

if (concurrencyLimit <= items.length) {
task.then(() => {
pendings.splice(pendings.indexOf(task), 1)

Check warning on line 28 in src/asyncPool.ts

View check run for this annotation

Codecov / codecov/patch

src/asyncPool.ts#L27-L28

Added lines #L27 - L28 were not covered by tests
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

分号都补一下。

})
pendings.push(task)

Check warning on line 30 in src/asyncPool.ts

View check run for this annotation

Codecov / codecov/patch

src/asyncPool.ts#L30

Added line #L30 was not covered by tests

if (pendings.length >= concurrencyLimit) {
await Promise.race(pendings)

Check warning on line 33 in src/asyncPool.ts

View check run for this annotation

Codecov / codecov/patch

src/asyncPool.ts#L33

Added line #L33 was not covered by tests
}
}
}

return Promise.all(tasks)

Check warning on line 38 in src/asyncPool.ts

View check run for this annotation

Codecov / codecov/patch

src/asyncPool.ts#L38

Added line #L38 was not covered by tests
}
1 change: 1 addition & 0 deletions src/interface.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export interface UploadProps
input?: React.CSSProperties;
};
hasControlInside?: boolean;
concurrencyLimit?: number;
}

export interface UploadProgressEvent extends Partial<ProgressEvent> {
Expand Down
Loading