Skip to content

Commit

Permalink
create effect for file stream: fix #2777
Browse files Browse the repository at this point in the history
Use this effect instead of the "file://" protocol
  • Loading branch information
btzr-io authored and Sean Yesmunt committed May 13, 2020
1 parent 98820ad commit dc10a2d
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions ui/effects/use-stream-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// @flow
import React from 'react';

// Returns a blob from the download path
export default function useFileStream(fileStream: (?string) => any) {

const [state, setState] = React.useState({
error: false,
content: null,
loading: true,
});

React.useEffect(() => {
if (fileStream) {
let chunks = []
const stream = fileStream();

stream.on('data', chunk => {
chunks.push(chunk)
});

stream.on('end', () => {
const buffer = Buffer.concat(chunks)
const blob = new Blob([buffer])
setState({ content: blob, loading: false });
});

stream.on('error', () => {
setState({ error: true, loading: false });

});
}
}, []);

return state;
}

0 comments on commit dc10a2d

Please sign in to comment.