flowjs / flow.js

A JavaScript library providing multiple simultaneous, stable, fault-tolerant and resumable/restartable file uploads via the HTML5 File API.
Other
2.96k stars 346 forks source link

Concurrent readFileFn calls tip #300

Open drzraf opened 4 years ago

drzraf commented 4 years ago

I'm reading from a stream instead of a blob I could slice. In order to solve this problem, I had to implement a poor-man semaphore based on readState.

    async read(flowObj, startByte, endByte, fileType, chunk) {
        var prev_chunk = chunk.offset > 0 ? flowObj.chunks[chunk.offset - 1] : null;
        if (prev_chunk) {
            while(prev_chunk.readState != 2) {
                await new Promise(resolve => setTimeout(resolve, 500));
            }
        }
        flowObj.readState = 1;
        var blob = await this._reader.readFooBar();
        chunk.readFinished(blob);
    }

But I think a more elegant system could be built-in within flow.js. Eg: streamRead: <boolean> : Set to true to avoid concurrent calls to read() And readState could be made a Promise that readFinished resolves so that other chunks could await based on this promise completion status.

See also: #296 and #304