Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/ui/widgets/EmbeddedDisplay/useOpiFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,25 +77,35 @@ export function useOpiFile(file: File): WidgetDescription {
const fileExt = file.path.split(".").pop() || "json";
const [contents, setContents] = useState<WidgetDescription>(EMPTY_WIDGET);

useEffect((): void => {
useEffect(() => {
let isMounted = true;
const fetchData = async (): Promise<void> => {
if (fetchPromises.hasOwnProperty(file.path)) {
// This resource has been requested; once the cached
// promise has been resolved the fileCache should be
// populated.
await fetchPromises[file.path];
setContents(fileCache[file.path]);
if (isMounted) {
setContents(fileCache[file.path]);
}
} else {
const fetchPromise = fetchAndConvert(file.path, file.defaultProtocol);
// Populate the promises cache.
fetchPromises[file.path] = fetchPromise;
const contents = await fetchPromise;
// Populate the file cache.
fileCache[file.path] = contents;
setContents(contents);
if (isMounted) {
setContents(contents);
}
}
};
fetchData();

// Tidy up in case component is unmounted
return () => {
isMounted = false;
};
}, [file.path, file.defaultProtocol, fileExt]);

return contents;
Expand Down