Skip to content

Commit

Permalink
- Added ability to cancel the processing of streams promise when stre…
Browse files Browse the repository at this point in the history
…ams has changed. This fixes #44.
  • Loading branch information
supriyaamisshra committed Mar 9, 2022
1 parent f4f0980 commit d99ea44
Showing 1 changed file with 26 additions and 14 deletions.
40 changes: 26 additions & 14 deletions packages/react-app/src/views/Home.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,33 @@ export default function Home({

const [sData, setData] = useState([]);

useEffect(async () => {
// parallely load all available streams data
Promise.all(
streams.map(async (stream) => {
const summary = await resolveStreamSummary(stream.stream, mainnetProvider);
return {...stream, 3: summary.cap, percent: summary.percent};
})
).then(results => {
setData(results);
useEffect(() => {
let shouldCancel = false;
const fetchStreams = async () => {
// parallely load all available streams data
Promise.all(
streams.map(async (stream) => {
const summary = await resolveStreamSummary(stream.stream, mainnetProvider);
return { ...stream, 3: summary.cap, percent: summary.percent };
})
).then(results => {
// process promised streams only when this effect call is not cancelled.
if (!shouldCancel) {
setData(results);

// Wait until list is almost fully loaded to render
if (results.length >= 18) {
setReady(true);
}
});
// Wait until list is almost fully loaded to render
if (results.length >= 18) {
setReady(true);
}
}
});
}

fetchStreams()
.catch(console.error);

// cleanup callback
return () => shouldCancel = true;
}, [streams]);

const createNewStream = async () => {
Expand Down

0 comments on commit d99ea44

Please sign in to comment.