create effect to load blob from streaming url
Use this on the web for files that need to wait for the full stream
This commit is contained in:
parent
1992e78c2e
commit
1f6dd89e55
1 changed files with 45 additions and 0 deletions
45
ui/effects/use-stream.js
Normal file
45
ui/effects/use-stream.js
Normal file
|
@ -0,0 +1,45 @@
|
|||
// @flow
|
||||
import React from 'react';
|
||||
import https from 'https';
|
||||
|
||||
// Returns web blob from the streaming url
|
||||
export default function useStream(url: (?string) => any) {
|
||||
|
||||
const [state, setState] = React.useState({
|
||||
error: false,
|
||||
content: null,
|
||||
loading: false,
|
||||
});
|
||||
|
||||
React.useEffect(() => {
|
||||
if (url) {
|
||||
let chunks = [];
|
||||
|
||||
// Start loading state
|
||||
setState({loading: true})
|
||||
|
||||
https.get(
|
||||
url,
|
||||
response => {
|
||||
if (response.statusCode >= 200 && response.statusCode < 300) {
|
||||
let chunks = []
|
||||
response.on('data', function(chunk) {
|
||||
chunks.push(chunk);
|
||||
});
|
||||
response.on('end', () => {
|
||||
const buffer = Buffer.concat(chunks)
|
||||
const blob = new Blob([buffer])
|
||||
console.info(response)
|
||||
setState({ content: blob, loading: false });
|
||||
});
|
||||
} else {
|
||||
console.info(response)
|
||||
setState({ error: true, loading: false });
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
}, []);
|
||||
|
||||
return state;
|
||||
}
|
Loading…
Reference in a new issue