forked from flowjs/flow.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ability to use asynchronous initFileFn.
This is a follow-up (and proper implementation) of flowjs#296 (reverted in e867d54) This could be use to initialize a stream, a reader, fetch a remote resource, ... during the FlowFile initialization. `asyncAddFile` and `asyncAddFiles` are introduced which support this mechanism. These function return promises of one (or multiple) FlowFile(s). To implement this: - An AsyncFlowFile class extending a FlowFile is created, overriding the bootstrap part - In order to keep code-duplication low, a filterFileList generator yield files in way common to async and non-async addFiles() functions. The possibly async' nature of initializing file hit back events firing. (flowjs#319) in general and `fileAdded` in particular (flowjs#271) and the current confusión between hooks altering bootstraping and simple events. - fileAdded is now assumed an event. If we detect a return value, a warning is sent. - preFilterFile is introduced and allow filtering the raw file before bootstrap() - postFilterFile is introduced and allow filtering the FlowFile after a (possibly async) bootstrap() - filesAdded is preserved About parameters: - `initFileFn` is only used during bootstrap - If such a helper exists, it's tightly related to addFile* functions. As such, it was deemed adequate to provide it as a parameter to `*addFile*()` (but `opts.initFileFn` is still supported)
- Loading branch information
Raphaël Droz
committed
Jan 13, 2021
1 parent
7d84d31
commit 7a81c7b
Showing
2 changed files
with
171 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import FlowFile from './FlowFile'; | ||
|
||
/** | ||
* AsyncFlowFile class | ||
* @name AsyncFlowFile | ||
*/ | ||
export default class AsyncFlowFile extends FlowFile { | ||
|
||
/** | ||
* Retry aborted file upload | ||
* @function | ||
*/ | ||
async retry() { | ||
await this.bootstrap('retry'); | ||
return this.flowObj.upload(); | ||
} | ||
|
||
async bootstrap(event = null, initFileFn = this.flowObj.opts.initFileFn) { | ||
/** | ||
* Asynchronous initialization function, if defined, is run | ||
* Then _bootstrap follow-up occurs | ||
* And, optionally (in case of initial FlowFile creation), the `fileAdded` event is fired. | ||
*/ | ||
if (typeof initFileFn === 'function') { | ||
await initFileFn(this, event); | ||
} | ||
|
||
this._bootstrap(); | ||
if (event !== 'retry') { | ||
this.flowObj.fire('fileAdded', this, event); | ||
} | ||
|
||
// console.log("Flowfile returns [async]", this._bootstrapped); | ||
return this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters