Skip to content

Commit b2a1388

Browse files
author
Raphaël Droz
authored
assignBrowse / onDrop: Consider the possibility of an async initFileFn (#350)
* assignBrowse/onDrop: Consider the possibility of an async initFileFn When calling *addFile(), FlowChunk are created which, among other things look for the file size to compute the endByte (and the number of chunks) If an "initFileFn" is declared *AND* this function is async' it's sane to assume we need to use `asyncAddFile()` which will bootstrap slightly differently. By giving the time to run an async `initFileFn` it can, for example, allow the chunk size to be computed differently regarding the stream initialization. An example is a stream-crypto later that could had an overhead affecting FlowFile.size Note: In the case of assignBrowse, the input is set to readonly for the time (async) initFileFn() runs
1 parent caf4964 commit b2a1388

File tree

1 file changed

+43
-7
lines changed

1 file changed

+43
-7
lines changed

src/Flow.js

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,25 @@ export default class Flow extends Eventizer {
142142
}
143143
}
144144

145+
/**
146+
* On drop event when file/stream initialization is asynchronous
147+
* @function
148+
* @param {MouseEvent} event
149+
*/
150+
async asyncOnDrop(event) {
151+
if (this.opts.onDropStopPropagation) {
152+
event.stopPropagation();
153+
}
154+
event.preventDefault();
155+
var dataTransfer = event.dataTransfer;
156+
if (dataTransfer.items && dataTransfer.items[0] &&
157+
dataTransfer.items[0].webkitGetAsEntry) {
158+
this.webkitReadDataTransfer(event);
159+
} else {
160+
await this.asyncAddFiles(dataTransfer.files, event);
161+
}
162+
}
163+
145164
/**
146165
* Prevent default
147166
* @function
@@ -341,12 +360,25 @@ export default class Flow extends Eventizer {
341360
input.setAttribute(key, value);
342361
});
343362
// When new files are added, simply append them to the overall list
344-
input.addEventListener('change', (e) => {
345-
if (e.target.value) {
346-
this.addFiles(e.target.files, e);
347-
e.target.value = '';
348-
}
349-
}, false);
363+
// but adapt to the case where initFileFn is async.
364+
var callback = this.opts.initFileFn
365+
&& typeof this.opts.initFileFn === 'function'
366+
&& this.opts.initFileFn.constructor.name === 'AsyncFunction'
367+
? async e => {
368+
if (e.target.value) {
369+
input.setAttribute('readonly', 'readonly');
370+
await this.asyncAddFiles(e.target.files, e);
371+
e.target.value = '';
372+
input.removeAttribute('readonly');
373+
}
374+
}
375+
: e => {
376+
if (e.target.value) {
377+
this.addFiles(e.target.files, e);
378+
e.target.value = '';
379+
}
380+
};
381+
input.addEventListener('change', callback, false);
350382
}, this);
351383
}
352384

@@ -360,7 +392,11 @@ export default class Flow extends Eventizer {
360392
domNodes = [domNodes];
361393
}
362394

363-
this._onDropBound = this.onDrop.bind(this);
395+
this._onDropBound = this.opts.initFileFn
396+
&& typeof this.opts.initFileFn === 'function'
397+
&& this.opts.initFileFn.constructor.name === 'AsyncFunction'
398+
? this.asyncOnDrop.bind(this)
399+
: this.onDrop.bind(this);
364400
for (let domNode of domNodes) {
365401
domNode.addEventListener('dragover', this.preventEvent, false);
366402
domNode.addEventListener('dragenter', this.preventEvent, false);

0 commit comments

Comments
 (0)