Skip to content

Commit 393b723

Browse files
authored
Fix File Progress Type (#1140)
* Update RESTController.js * Update RESTController-test.js * Update ParseFile-test.js * Move type at the end of the param * Update progress method's documentation
1 parent 824713f commit 393b723

File tree

4 files changed

+22
-11
lines changed

4 files changed

+22
-11
lines changed

src/ParseFile.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,17 @@ class ParseFile {
233233
* be used for this request.
234234
* <li>sessionToken: A valid session token, used for making a request on
235235
* behalf of a specific user.
236-
* <li>progress: In Browser only, callback for upload progress
236+
* <li>progress: In Browser only, callback for upload progress. For example:
237+
* <pre>
238+
* let parseFile = new Parse.File(name, file);
239+
* parseFile.save({
240+
* progress: (progressValue, loaded, total, { type }) => {
241+
* if (type === "upload" && progressValue !== null) {
242+
* // Update the UI using progressValue
243+
* }
244+
* }
245+
* });
246+
* </pre>
237247
* </ul>
238248
* @return {Promise} Promise that is resolved when the save finishes.
239249
*/

src/RESTController.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -159,27 +159,28 @@ const RESTController = {
159159
headers[key] = customHeaders[key];
160160
}
161161

162-
function handleProgress(event) {
162+
function handleProgress(type, event) {
163163
if (options && typeof options.progress === 'function') {
164164
if (event.lengthComputable) {
165165
options.progress(
166166
event.loaded / event.total,
167167
event.loaded,
168-
event.total
168+
event.total,
169+
{ type }
169170
);
170171
} else {
171-
options.progress(null);
172+
options.progress(null, null, null, { type });
172173
}
173174
}
174175
}
175176

176177
xhr.onprogress = (event) => {
177-
handleProgress(event);
178+
handleProgress('download', event);
178179
};
179180

180181
if (xhr.upload) {
181182
xhr.upload.onprogress = (event) => {
182-
handleProgress(event);
183+
handleProgress('upload', event);
183184
}
184185
}
185186

src/__tests__/ParseFile-test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ jest.setMock('../LocalDatastore', mockLocalDatastore);
3030
function generateSaveMock(prefix) {
3131
return function(name, payload, options) {
3232
if (options && typeof options.progress === 'function') {
33-
options.progress(0.5, 5, 10);
33+
options.progress(0.5, 5, 10, { type: 'upload' });
3434
}
3535
return Promise.resolve({
3636
name: name,
@@ -236,7 +236,7 @@ describe('ParseFile', () => {
236236
jest.spyOn(options, 'progress');
237237

238238
return file.save(options).then(function(f) {
239-
expect(options.progress).toHaveBeenCalledWith(0.5, 5, 10);
239+
expect(options.progress).toHaveBeenCalledWith(0.5, 5, 10, { type: 'upload' });
240240
expect(f).toBe(file);
241241
expect(f.name()).toBe('progress.txt');
242242
expect(f.url()).toBe('http://files.parsetfss.com/a/progress.txt');

src/__tests__/RESTController-test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -444,8 +444,8 @@ describe('RESTController', () => {
444444
jest.spyOn(options, 'progress');
445445

446446
RESTController.ajax('POST', 'files/upload.txt', {}, {}, options).then(({ response, status }) => {
447-
expect(options.progress).toHaveBeenCalledWith(0.5, 5, 10);
448-
expect(options.progress).toHaveBeenCalledTimes(2);
447+
expect(options.progress).toHaveBeenCalledWith(0.5, 5, 10, { type: 'download' });
448+
expect(options.progress).toHaveBeenCalledWith(0.5, 5, 10, { type: 'upload' });
449449
expect(response).toEqual({ success: true });
450450
expect(status).toBe(200);
451451
done();
@@ -468,7 +468,7 @@ describe('RESTController', () => {
468468
jest.spyOn(options, 'progress');
469469

470470
RESTController.ajax('POST', 'files/upload.txt', {}, {}, options).then(({ response, status }) => {
471-
expect(options.progress).toHaveBeenCalledWith(null);
471+
expect(options.progress).toHaveBeenCalledWith(null, null, null, { type: 'upload' });
472472
expect(response).toEqual({ success: true });
473473
expect(status).toBe(200);
474474
done();

0 commit comments

Comments
 (0)