Skip to content

Commit

Permalink
Initialize new openseadragon instance once. (#208)
Browse files Browse the repository at this point in the history
  • Loading branch information
mathewjordan authored May 1, 2024
1 parent 36309e3 commit 17a4625
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/components/Image/OSD/OSD.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
Wrapper,
} from "src/components/Image/Image.styled";
import OpenSeadragon, { Options } from "openseadragon";
import React, { useEffect, useState } from "react";
import React, { useEffect, useRef, useState } from "react";

import Controls from "src/components/Image/Controls/Controls";
import { OpenSeadragonImageTypes } from "src/types/open-seadragon";
Expand All @@ -30,8 +30,14 @@ const OSD: React.FC<OSDProps> = ({
const [osdUri, setOsdUri] = useState<string>();
const [openSeadragon, setOpenSeadragon] = useState<OpenSeadragon.Viewer>();

const initializeOSD = useRef(false);

useEffect(() => {
if (!openSeadragon) setOpenSeadragon(OpenSeadragon(config));
if (!initializeOSD.current) {
initializeOSD.current = true;

if (!openSeadragon) setOpenSeadragon(OpenSeadragon(config));
}

return () => openSeadragon?.destroy();
}, []);
Expand All @@ -43,7 +49,7 @@ const OSD: React.FC<OSDProps> = ({

useEffect(() => {
if (openSeadragon && uri !== osdUri) {
openSeadragon.forceRedraw();
openSeadragon?.forceRedraw();
setOsdUri(uri);
}
}, [openSeadragon, osdUri, uri]);
Expand All @@ -52,7 +58,7 @@ const OSD: React.FC<OSDProps> = ({
if (osdUri && openSeadragon) {
switch (imageType) {
case "simpleImage":
openSeadragon.addSimpleImage({
openSeadragon?.addSimpleImage({
url: osdUri,
});
break;
Expand All @@ -62,23 +68,23 @@ const OSD: React.FC<OSDProps> = ({
if (!tileSource)
throw new Error(`No tile source found for ${osdUri}`);

openSeadragon.addTiledImage({
tileSource: tileSource,
openSeadragon?.addTiledImage({
tileSource,
});
} catch (e) {
console.error(e);
}
});
break;
default:
openSeadragon.close();
openSeadragon?.close();
console.warn(
`Unable to render ${osdUri} in OpenSeadragon as type: "${imageType}"`,
);
break;
}
}
}, [imageType, osdUri, openSeadragon]);
}, [imageType, osdUri]);

return (
<Wrapper
Expand Down

0 comments on commit 17a4625

Please sign in to comment.