Skip to content

Commit

Permalink
Combine file-added and its filtering into one loop
Browse files Browse the repository at this point in the history
* Make addFiles call files-added hook with a file list filtered by every individual file-added like in v2 and old synchronous v3 functions
* Cleanup: removed the intermediary state objects and array
* Cleanup: rename flowfiles array to flowFiles
  • Loading branch information
ilessiivi committed Oct 11, 2021
1 parent b0d603f commit 29b0ea6
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions src/Flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ export default class Flow extends Eventizer {
* @return Promise{[<FlowFile>,...]} 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) {
Expand All @@ -546,29 +546,27 @@ 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]);
}
this.files.push(file);
}
await this.hook('files-submitted', this.files, event);

return flowfiles;
return flowFiles;
}

/**
Expand Down

0 comments on commit 29b0ea6

Please sign in to comment.