diff --git a/src/Flow.js b/src/Flow.js index 4cc37cf..f1cb92c 100644 --- a/src/Flow.js +++ b/src/Flow.js @@ -536,7 +536,7 @@ export default class Flow extends Eventizer { * @return Promise{[,...]} The promise of getting an array of FlowFile. */ async addFiles(fileList, event = null, initFileFn = this.opts.initFileFn) { - let item, file, flowfile, uniqueIdentifier, states = []; + let item, file, uniqueIdentifier, flowFiles = []; const iterator = this.filterFileList(fileList, event); while ((item = iterator.next()) && !item.done) { @@ -546,21 +546,19 @@ export default class Flow extends Eventizer { continue; } - // ToDo: parallelizable ? - var flowFile = new FlowFile(this, file, uniqueIdentifier), - state = flowFile.bootstrap(event, initFileFn); - states.push(state); - } + let flowFile = new FlowFile(this, file, uniqueIdentifier); + await flowFile.bootstrap(event, initFileFn); + await this.hook('file-added', flowFile, event); - var flowfiles = await Promise.all(states); - for (let ff of flowfiles) { - await this.hook('file-added', ff, event); + if(flowFile && flowFile.file) { + flowFiles.push(flowFile); + } } - await this.hook('files-added', flowfiles, event); + await this.hook('files-added', flowFiles, event); - flowfiles = flowfiles.filter(e => e && e.file); - for (let file of flowfiles) { + flowFiles = flowFiles.filter(flowFile => flowFile && flowFile.file); + for (let file of flowFiles) { if (this.opts.singleFile && this.files.length > 0) { await this.removeFile(this.files[0]); } @@ -568,7 +566,7 @@ export default class Flow extends Eventizer { } await this.hook('files-submitted', this.files, event); - return flowfiles; + return flowFiles; } /** diff --git a/test/fileAddSpec.js b/test/fileAddSpec.js index c564fc5..db77569 100644 --- a/test/fileAddSpec.js +++ b/test/fileAddSpec.js @@ -95,6 +95,23 @@ describe('fileAdd event', function() { expect(valid).toBeTruthy(); }); + it('should validate file-added filtering before files-added', async function() { + var valid = false; + flow.on('file-added', (flowFile) => { + if(flowFile.name === 'f2') { + delete flowFile.file; + } + }); + flow.on('files-added', (files) => { + valid = files.length === 1; + }); + await flow.addFiles([ + new File(['file'], 'f1'), + new File(['file2'], 'f2') + ]); + expect(valid).toBeTruthy(); + }); + it('should validate multiple filter-file hooks', async function() { const customFunction = jasmine.createSpy('fn'); flow.on('filter-file', async () => {