Skip to content

Commit ef0e21b

Browse files
author
Raphaël Droz
committed
Make FlowFile async-first and use a couple of Jasmine expectAsync()
1 parent a09eb5a commit ef0e21b

File tree

5 files changed

+35
-65
lines changed

5 files changed

+35
-65
lines changed

src/AsyncFlowFile.js

-47
This file was deleted.

src/Flow.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import Eventizer from './Eventizer';
66
import FlowFile from './FlowFile';
7-
import AsyncFlowFile from './AsyncFlowFile';
87
import {each, webAPIFileRead} from './tools';
98

109
/**
@@ -533,7 +532,7 @@ export default class Flow extends Eventizer {
533532
* @param {File} file
534533
* @param Any other parameters supported by addFiles.
535534
*
536-
* @return (async) An initialized <AsyncFlowFile>.
535+
* @return (async) An initialized <FlowFile>.
537536
*/
538537
async addFile(file, ...args) {
539538
return (await this.addFiles([file], ...args))[0];
@@ -545,7 +544,7 @@ export default class Flow extends Eventizer {
545544
* @param {FileList|Array} fileList
546545
* @param {Event} [event] event is optional
547546
*
548-
* @return Promise{[<AsyncFlowFile>,...]} The promise of getting an array of AsyncFlowFile.
547+
* @return Promise{[<FlowFile>,...]} The promise of getting an array of FlowFile.
549548
*/
550549
async addFiles(fileList, event = null, initFileFn = this.opts.initFileFn) {
551550
let item, file, flowfile, uniqueIdentifier, states = [];
@@ -559,7 +558,7 @@ export default class Flow extends Eventizer {
559558
}
560559

561560
// ToDo: parallelizable ?
562-
var flowFile = new AsyncFlowFile(this, file, uniqueIdentifier),
561+
var flowFile = new FlowFile(this, file, uniqueIdentifier),
563562
state = flowFile.bootstrap(event, initFileFn);
564563
states.push(state);
565564
}

src/FlowFile.js

+30-12
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,11 @@ export default class FlowFile {
182182

183183
/**
184184
* Resume file upload
185-
* @function
185+
* @return Promise
186186
*/
187187
resume() {
188188
this.paused = false;
189-
this.flowObj.upload();
189+
return this.flowObj.upload();
190190
}
191191

192192
/**
@@ -220,21 +220,25 @@ export default class FlowFile {
220220
* Retry aborted file upload
221221
* @function
222222
*/
223-
retry() {
224-
this.bootstrap('retry');
225-
this.flowObj.upload();
223+
async retry() {
224+
await this.bootstrap('retry');
225+
return await this.flowObj.upload();
226226
}
227227

228-
/**
229-
* Clear current chunks and slice file again
230-
* @function
231-
*/
232-
bootstrap(event = null, initFileFn = this.flowObj.opts.initFileFn) {
233-
if (typeof initFileFn === "function") {
234-
initFileFn(this);
228+
async bootstrap(event = null, initFileFn = this.flowObj.opts.initFileFn) {
229+
/**
230+
* Asynchronous initialization function, if defined, is run
231+
* Then _bootstrap follow-up occurs
232+
* And, optionally (in case of initial FlowFile creation), the `file-added` event is fired.
233+
*/
234+
if (typeof initFileFn === 'function') {
235+
await initFileFn(this, event);
235236
}
236237

237238
this._bootstrap();
239+
240+
// console.log("Flowfile returns [async]", this._bootstrapped);
241+
return this;
238242
}
239243

240244
_bootstrap() {
@@ -295,6 +299,20 @@ export default class FlowFile {
295299
return uploading;
296300
}
297301

302+
/**
303+
* Indicates if string is being read at the moment
304+
* @function
305+
* @returns {boolean}
306+
*/
307+
isReading() {
308+
for (let chunk of this.chunks) {
309+
if (chunk.status() === 'reading') {
310+
return true;
311+
}
312+
}
313+
return false;
314+
}
315+
298316
/**
299317
* Indicates if file is has finished uploading and received a response
300318
* @function

test/fileAddSpec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ describe('fileAdd event', function() {
156156
new File(['GGG'], 'GGG.bin'),
157157
]);
158158
expect(flowfiles[0].name).toBe('bbb.bin');
159-
expect(await flowfiles[0].file.text()).toBe('xxx');
159+
expectAsync(flowfiles[0].file.text()).toBe('xxx');
160160
});
161161

162162
it('A files-added hook can actually change files', async function() {

test/setupSpec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ describe('setup', function() {
3333
});
3434

3535
it('should show methods initial state', async function() {
36-
expect(await flow.uploadNextChunk()).toBe(false);
36+
expectAsync(flow.uploadNextChunk()).toBe(false);
3737

3838
expect(flow.progress()).toBe(0);
3939
expect(flow.isUploading()).toBe(false);

0 commit comments

Comments
 (0)