Skip to content

Commit 5ac4063

Browse files
committed
refactor
1 parent b3f156a commit 5ac4063

File tree

2 files changed

+51
-34
lines changed

2 files changed

+51
-34
lines changed

file-uploader-progress-overlay.html

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@
9999
<div class="detail">
100100
<div>[[_computeUploadingTitle(destinationText, localize)]]</div>
101101
<div class="detail__destination">[[destinationText]]</div>
102-
<div>[[_computeLoadedDetail(currentFile, uploadingFiles.length, localize)]]</div>
102+
<div>[[_computeLoadedDetail(currentFile, uploadingFiles.size, localize)]]</div>
103103
<button class="detail__button" on-tap="_stopTapped">[[localize('stop')]]</button>
104104
<paper-progress id="paperProgress" indeterminate></paper-progress>
105105
</div>
@@ -130,7 +130,7 @@
130130
value: true
131131
},
132132

133-
uploadingFiles: Array,
133+
uploadingFiles: Map,
134134

135135
// The file that is being currently uploaded.
136136
currentFile: Object,
@@ -186,10 +186,18 @@
186186
return this.localize('Uploading');
187187
},
188188

189-
_computeLoadedDetail(currentFile, uploadingFilesLength) {
189+
_computeLoadedDetail(currentFile, uploadingFilesSize) {
190+
let current = 0;
191+
192+
for (let [file, detail] of this.uploadingFiles) {
193+
if (detail.state !== 'uploaded') {
194+
current += 1;
195+
}
196+
}
197+
190198
return this.localize('LoadedDetail',
191-
'current', this.uploadingFiles.indexOf(currentFile) + 1,
192-
'total', uploadingFilesLength);
199+
'current', current,
200+
'total', uploadingFilesSize);
193201
}
194202

195203

file-uploader.html

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,28 @@
1717

1818
properties: {
1919

20-
// Files that are currently being uploaded
20+
/**
21+
* Files that are currently being uploaded.
22+
* It's a Map where keys are files and values are upload details.
23+
*/
2124
uploadingFiles: {
22-
type: Array,
25+
type: Map,
2326
readOnly: true,
24-
value: _ => []
27+
value: _ => {
28+
return new Map();
29+
}
2530
},
2631

32+
// @optional
2733
url: String,
2834

35+
// @optional
2936
method: {
3037
type: String,
3138
value: 'POST',
3239
},
3340

41+
// @optional
3442
headers: Object,
3543

3644
uploading: {
@@ -40,24 +48,18 @@
4048
},
4149

4250
// Current XMLHTTPRequest
43-
_currentXHR: Object,
44-
45-
_filesUploadDetail: {
46-
type: WeakMap,
47-
value: _ => {
48-
return new WeakMap();
49-
}
50-
}
51+
_currentXHR: Object
5152

5253
},
5354

5455
/**
5556
* Add a single file to the uploading queue.
57+
*
5658
* @param {!File}
5759
*/
58-
addFile(file) {
60+
addFile(file, options) {
5961
if (!this._isFileOnQueue(file)) {
60-
this.push('uploadingFiles', file);
62+
this.uploadingFiles.set(file, {xhrParams: options, state: 'queue'});
6163

6264
if (!this.uploading) {
6365
this._uploadStart();
@@ -76,9 +78,9 @@
7678
},
7779

7880
_resumeQueue() {
79-
for (const file of this.uploadingFiles) {
80-
if (!this._filesUploadDetail.get(file)) {
81-
this._upload(file);
81+
for (const [file, detail] of this.uploadingFiles) {
82+
if (detail.state === 'queue') {
83+
this._uploadFile(file, detail.xhrParams);
8284
break;
8385
}
8486
}
@@ -94,8 +96,7 @@
9496

9597
this._progressOverlay = this.create('file-uploader-progress', {
9698
opened: true,
97-
uploadingFiles: this.uploadingFiles,
98-
filesUploadDetail: this._filesUploadDetail
99+
uploadingFiles: this.uploadingFiles
99100
});
100101

101102
this._progressOverlay.addEventListener('request-cancel-upload', _ => {
@@ -105,22 +106,24 @@
105106
this._resumeQueue();
106107
},
107108

108-
_upload(file) {
109+
_uploadFile(file, xhrParams) {
109110
if (!this.uploading) {
110111
return;
111112
}
112113

113114
this._progressOverlay.currentFile = file;
115+
const detail = this.uploadingFiles.get(file);
114116

115-
this._xhr().then(response => {
117+
this._xhr(xhrParams).then(response => {
116118
const data = {file, url: response.url};
117119

118-
this._filesUploadDetail.set(file, {state: 'uploaded'});
119-
this.fire('file-uploaded', data);
120+
detail.state = 'uploaded';
121+
this.fire('file-uploaded', {file, detail});
120122
return data;
121123
}).catch(error => {
122-
this._filesUploadDetail.set(file, {state: 'failed'});
123-
this.fire('file-upload-error', {file, error});
124+
detail.state = 'failed';
125+
detail.error = error;
126+
this.fire('error', {file, detail});
124127
throw error;
125128
}).then(_ => {
126129
this._resumeQueue();
@@ -136,15 +139,21 @@
136139
this._progressOverlay.opened = false;
137140
// this._progressOverlay is automatically removed when it's closed
138141
this._progressOverlay = null;
139-
this._filesUploadDetail = new WeakMap();
140-
this.uploadingFiles([]);
142+
this.uploadingFiles.clear();
141143
this._setUploading(false);
142144
},
143145

144146
_isFileOnQueue(file) {
145-
return this.uploadingFiles.find(f => {
146-
return f.name === file.name && f.size === file.size;
147-
});
147+
let result = false;
148+
149+
for (let [f, detail] of this.uploadingFiles) {
150+
if (f.name === file.name && f.size === file.size) {
151+
result = true;
152+
break;
153+
}
154+
}
155+
156+
return result;
148157
},
149158

150159
_xhr({file, url = this.url, method = this.method, headers = this.headers} = {}) {

0 commit comments

Comments
 (0)