Skip to content

Commit 7aafa89

Browse files
committed
feat: pass chunk as last event argument
1 parent 47a1b80 commit 7aafa89

2 files changed

Lines changed: 46 additions & 22 deletions

File tree

src/flow.js

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -786,43 +786,44 @@
786786
* For internal usage only.
787787
* Callback when something happens within the chunk.
788788
* @function
789+
* @param {FlowChunk} chunk
789790
* @param {string} event can be 'progress', 'success', 'error' or 'retry'
790791
* @param {string} [message]
791792
*/
792-
chunkEvent: function (event, message) {
793+
chunkEvent: function (chunk, event, message) {
793794
switch (event) {
794795
case 'progress':
795796
if (Date.now() - this._lastProgressCallback <
796797
this.flowObj.opts.progressCallbacksInterval) {
797798
break;
798799
}
799800
this.measureSpeed();
800-
this.flowObj.fire('fileProgress', this);
801+
this.flowObj.fire('fileProgress', this, chunk);
801802
this.flowObj.fire('progress');
802803
this._lastProgressCallback = Date.now();
803804
break;
804805
case 'error':
805806
this.error = true;
806807
this.abort(true);
807-
this.flowObj.fire('fileError', this, message);
808-
this.flowObj.fire('error', message, this);
808+
this.flowObj.fire('fileError', this, message, chunk);
809+
this.flowObj.fire('error', message, this, chunk);
809810
break;
810811
case 'success':
811812
if (this.error) {
812813
return;
813814
}
814815
this.measureSpeed();
815-
this.flowObj.fire('fileProgress', this);
816+
this.flowObj.fire('fileProgress', this, chunk);
816817
this.flowObj.fire('progress');
817818
this._lastProgressCallback = Date.now();
818819
if (this.isComplete()) {
819820
this.currentSpeed = 0;
820821
this.averageSpeed = 0;
821-
this.flowObj.fire('fileSuccess', this, message);
822+
this.flowObj.fire('fileSuccess', this, message, chunk);
822823
}
823824
break;
824825
case 'retry':
825-
this.flowObj.fire('fileRetry', this);
826+
this.flowObj.fire('fileRetry', this, chunk);
826827
break;
827828
}
828829
},
@@ -1121,6 +1122,17 @@
11211122

11221123
var $ = this;
11231124

1125+
1126+
/**
1127+
* Send chunk event
1128+
* @param event
1129+
* @param {...} args arguments of a callback
1130+
*/
1131+
this.event = function (event, args) {
1132+
args = Array.prototype.slice.call(arguments);
1133+
args.unshift($);
1134+
$.fileObj.chunkEvent.apply($.fileObj, args);
1135+
};
11241136
/**
11251137
* Catch progress event
11261138
* @param {ProgressEvent} event
@@ -1130,7 +1142,7 @@
11301142
$.loaded = event.loaded ;
11311143
$.total = event.total;
11321144
}
1133-
$.fileObj.chunkEvent('progress');
1145+
$.event('progress', event);
11341146
};
11351147

11361148
/**
@@ -1156,10 +1168,10 @@
11561168
this.doneHandler = function(event) {
11571169
var status = $.status();
11581170
if (status === 'success' || status === 'error') {
1159-
$.fileObj.chunkEvent(status, $.message());
1171+
$.event(status, $.message());
11601172
$.flowObj.uploadNextChunk();
11611173
} else {
1162-
$.fileObj.chunkEvent('retry', $.message());
1174+
$.event('retry', $.message());
11631175
$.pendingRetry = true;
11641176
$.abort();
11651177
$.retries++;

test/uploadSpec.js

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -211,13 +211,15 @@ describe('upload file', function() {
211211
flow.addFile(new Blob(['12']));
212212
var file = flow.files[0];
213213
expect(file.chunks.length).toBe(2);
214-
expect(file.chunks[0].status()).toBe('pending');
215-
expect(file.chunks[1].status()).toBe('pending');
214+
var firstChunk = file.chunks[0];
215+
var secondChunk = file.chunks[1];
216+
expect(firstChunk.status()).toBe('pending');
217+
expect(secondChunk.status()).toBe('pending');
216218

217219
flow.upload();
218220
expect(requests.length).toBe(1);
219-
expect(file.chunks[0].status()).toBe('uploading');
220-
expect(file.chunks[1].status()).toBe('pending');
221+
expect(firstChunk.status()).toBe('uploading');
222+
expect(secondChunk.status()).toBe('pending');
221223

222224
expect(error).not.toHaveBeenCalled();
223225
expect(progress).not.toHaveBeenCalled();
@@ -226,8 +228,8 @@ describe('upload file', function() {
226228

227229
requests[0].respond(400);
228230
expect(requests.length).toBe(2);
229-
expect(file.chunks[0].status()).toBe('uploading');
230-
expect(file.chunks[1].status()).toBe('pending');
231+
expect(firstChunk.status()).toBe('uploading');
232+
expect(secondChunk.status()).toBe('pending');
231233

232234
expect(error).not.toHaveBeenCalled();
233235
expect(progress).not.toHaveBeenCalled();
@@ -236,8 +238,8 @@ describe('upload file', function() {
236238

237239
requests[1].respond(200);
238240
expect(requests.length).toBe(3);
239-
expect(file.chunks[0].status()).toBe('success');
240-
expect(file.chunks[1].status()).toBe('uploading');
241+
expect(firstChunk.status()).toBe('success');
242+
expect(secondChunk.status()).toBe('uploading');
241243

242244
expect(error).not.toHaveBeenCalled();
243245
expect(progress.callCount).toBe(1);
@@ -246,8 +248,8 @@ describe('upload file', function() {
246248

247249
requests[2].respond(400);
248250
expect(requests.length).toBe(4);
249-
expect(file.chunks[0].status()).toBe('success');
250-
expect(file.chunks[1].status()).toBe('uploading');
251+
expect(firstChunk.status()).toBe('success');
252+
expect(secondChunk.status()).toBe('uploading');
251253

252254
expect(error).not.toHaveBeenCalled();
253255
expect(progress.callCount).toBe(1);
@@ -259,7 +261,7 @@ describe('upload file', function() {
259261
expect(file.chunks.length).toBe(0);
260262

261263
expect(error.callCount).toBe(1);
262-
expect(error).toHaveBeenCalledWith(file, 'Err');
264+
expect(error).toHaveBeenCalledWith(file, 'Err', secondChunk);
263265
expect(progress.callCount).toBe(1);
264266
expect(success).not.toHaveBeenCalled();
265267
expect(retry.callCount).toBe(2);
@@ -384,7 +386,7 @@ describe('upload file', function() {
384386
file.chunks[0].preprocessFinished();
385387
expect(requests.length).toBe(1);
386388
requests[0].respond(200, [], "response");
387-
expect(success).wasCalledWith(file, "response");
389+
expect(success).wasCalledWith(file, "response", file.chunks[0]);
388390
expect(error).not.toHaveBeenCalled();
389391
});
390392

@@ -407,6 +409,16 @@ describe('upload file', function() {
407409
expect(preprocess).wasNotCalledWith(secondFile.chunks[0]);
408410
});
409411

412+
it('should set chunk as a third event parameter', function () {
413+
var success = jasmine.createSpy('success');
414+
flow.on('fileSuccess', success);
415+
flow.addFile(new Blob(['abc']));
416+
var file = flow.files[0];
417+
flow.upload();
418+
requests[0].respond(200, [], "response");
419+
expect(success).wasCalledWith(file, "response", file.chunks[0]);
420+
});
421+
410422
it('should have upload speed', function() {
411423
var clock = sinon.useFakeTimers();
412424
flow.opts.testChunks = false;

0 commit comments

Comments
 (0)