Skip to content

Commit

Permalink
Prevent double-loading stories
Browse files Browse the repository at this point in the history
When revealing new "pages" before all previous stories finished loading,
the effect hook would initiate multiple requests for the same story.

We now keep track of which requests are in flight/progress to make sure
we only send one request per story.
  • Loading branch information
pmeinhardt committed May 17, 2022
1 parent 532a5f9 commit 9bb792d
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/components/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,20 @@ function List({ keys }: Props) {

const slice = useMemo(() => keys.slice(0, size), [keys, size]);

const loading = useSet<number>();

useEffect(() => {
slice
.filter((key) => !(key in data))
.filter((key) => !(key in data) && !loading.has(key))
.forEach((key) => {
queue.add(async () => {
const result = await get(`item/${key}.json`);
setData((prev) => ({ ...prev, [key]: result }));
});

loading.add(key);
});
}, [size]);
}, [data, loading, slice]);

const selection = useSet<number>();
const [cursor, setCursor] = useState(undefined);
Expand Down

0 comments on commit 9bb792d

Please sign in to comment.