Skip to content

Commit 5521ea3

Browse files
committed
Merge pull request #18 from eternalsky/master
加入 setOptions
2 parents c3e8cde + 5d1dc40 commit 5521ea3

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,21 @@ videos = {
223223

224224
静态变量,获得版本号。
225225

226+
### UploadCore.setOptions
227+
228+
修改已经初始化后的 Options,目前支持
229+
230+
```javascript
231+
['name', 'url', 'params', 'action', 'data', 'headers',
232+
'withCredentials', 'timeout', 'chunkEnable', 'chunkSize',
233+
'chunkRetries', 'chunkProcessThreads', 'autoPending',
234+
'auto', 'capcity', 'queueCapcity', 'sizeLimit',
235+
'fileSizeLimit']
236+
```
237+
238+
参数 | 类型 | 描述
239+
--- |----- | ------
240+
options | `object` | 传入参数,同初始化
226241

227242
### UploadCore.addConstraint
228243

demo/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ const core = new Core({
2121
preventDuplicate: false
2222
});
2323

24+
window.core = core;
25+
core.setOptions({
26+
queueCapcity: 2
27+
});
28+
2429
// 上传完成
2530
core.on(Events.FILE_UPLOAD_COMPLETING, response => {
2631
let json = response.getJson();

src/core.js

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,14 @@ class Core extends Emitter {
2727
this.constraints = new Constraints;
2828
this.filters = new Filters;
2929

30-
if (this.capcity > 0) {
31-
this.addConstraint(() => this.stat.getTotal() >= this.capcity);
32-
}
30+
this.addConstraint(() => {
31+
if (this.capcity > 0) {
32+
return this.stat.getTotal() >= this.capcity
33+
}
34+
else {
35+
return false;
36+
}
37+
});
3338

3439
if (this.accept && this.accept.length > 0) {
3540
this.addFilter((file) => {
@@ -62,7 +67,7 @@ class Core extends Emitter {
6267
options.fitlers.forEach(filter => this.addFilter(filter));
6368
}
6469

65-
const request = options.request || {};
70+
let request = options.request || {};
6671
REQUEST_OPTIONS.forEach((key) => {
6772
if (options.hasOwnProperty(key)) {
6873
request[key] = options[key];
@@ -72,6 +77,23 @@ class Core extends Emitter {
7277
this.requestOptions = request;
7378
}
7479

80+
setOptions(options) {
81+
if (typeof options === 'object' && !(options instanceof Array)) {
82+
this.autoPending = options.autoPending || options.auto || this.autoPending;
83+
this.capcity = options.capcity || options.queueCapcity || this.capcity;
84+
this.sizeLimit = parseSize(options.sizeLimit || options.fileSizeLimit || this.sizeLimit);
85+
let requestOptions = this.requestOptions
86+
REQUEST_OPTIONS.forEach((key) => {
87+
if (options.hasOwnProperty(key)) {
88+
requestOptions[key] = options[key];
89+
}
90+
});
91+
}
92+
else {
93+
console && console.error('setOptions: type error, options should be an object/hashMap');
94+
}
95+
}
96+
7597
createFileRequest(file) {
7698
return new FileRequest(file, this.requestOptions);
7799
}

0 commit comments

Comments
 (0)