Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

editor: cancel image download on switching notes

Abdullah Atta7f176942ba32
diff --git a/packages/editor/src/extensions/image/component.tsx b/packages/editor/src/extensions/image/component.tsx
index 4cb4b9955..acc854e78 100644
--- a/packages/editor/src/extensions/image/component.tsx
+++ b/packages/editor/src/extensions/image/component.tsx
@@ -20,7 +20,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
import { ThemeUIStyleObject } from "@theme-ui/core";
import { Box, Flex, Image, Text } from "@theme-ui/components";
import { ImageAttributes } from "./image";
-import { useEffect, useState } from "react";
+import { useEffect, useRef, useState } from "react";
import { ReactNodeViewProps } from "../react";
import { DesktopOnly } from "../../components/responsive";
import { Icon } from "@notesnook/ui";
@@ -54,6 +54,7 @@ export function ImageComponent(
const [bloburl, setBloburl] = useState<string | undefined>(
toBlobURL("", "image", mime, hash)
);
+ const controllerRef = useRef(new AbortController());
const isMobile = useIsMobile();
const { inView, ref: imageRef } = useObserver<HTMLImageElement>({
@@ -91,9 +92,10 @@ export function ImageComponent(
}, [inView]);
useEffect(() => {
+ const controller = controllerRef.current;
return () => {
- if (!hash) return;
- revokeBloburl(hash);
+ controller.abort();
+ if (hash) revokeBloburl(hash);
};
}, []);
@@ -296,8 +298,12 @@ export function ImageComponent(
);
if (src && !DataURL.isValid(src) && canParse(src)) {
- const image = await downloadImage(src, downloadOptions);
- if (!image) return;
+ const image = await downloadImage(src, {
+ ...downloadOptions,
+ signal: controllerRef.current.signal
+ }).catch(console.error);
+ if (!image || !imageRef.current) return;
+
const { url, size, blob, mimeType } = image;
imageRef.current.src = url;
const dataurl = await toDataURL(blob);
diff --git a/packages/editor/src/utils/downloader.ts b/packages/editor/src/utils/downloader.ts
index 1a03d3980..13ca55dd1 100644
--- a/packages/editor/src/utils/downloader.ts
+++ b/packages/editor/src/utils/downloader.ts
@@ -20,7 +20,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
import DataURL from "@notesnook/core/dist/utils/dataurl";
export type DownloadOptions = {
- corsHost: string;
+ corsHost?: string;
+ signal?: AbortSignal;
};
/**
@@ -73,7 +74,8 @@ export async function downloadImage(url: string, options?: DownloadOptions) {
const response = await fetch(corsifiedURL, {
mode: "cors",
credentials: "omit",
- cache: "force-cache"
+ cache: "force-cache",
+ signal: options?.signal
});
if (!response.ok) throw new Error(`invalid status code ${response.status}`);