Skip to content
Merged
Show file tree
Hide file tree
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
52 changes: 52 additions & 0 deletions src/components/Image/BaseImage.ios.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import {Image as ExpoImage} from 'expo-image';
import type {ImageLoadEventData} from 'expo-image';
import {useCallback, useContext, useEffect, useRef} from 'react';
import type {AttachmentSource} from '@components/Attachments/types';
import {AttachmentStateContext} from '@pages/media/AttachmentModalScreen/AttachmentModalBaseContent/AttachmentStateContextProvider';
import type {BaseImageProps} from './types';

function BaseImage({onLoad, source, ...props}: BaseImageProps) {
const isLoadedRef = useRef(false);
const attachmentContext = useContext(AttachmentStateContext);
const {setAttachmentLoaded, isAttachmentLoaded} = attachmentContext || {};

useEffect(() => {
if (isAttachmentLoaded?.(source as AttachmentSource)) {
return;
}
setAttachmentLoaded(source as AttachmentSource, false);
// eslint-disable-next-line react-compiler/react-compiler
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const imageLoadedSuccessfully = useCallback(
(event: ImageLoadEventData) => {
setAttachmentLoaded(source as AttachmentSource, true);
if (!onLoad) {
return;
}
if (isLoadedRef.current === true) {
return;
}

// We override `onLoad`, so both web and native have the same signature
const {width, height} = event.source;
isLoadedRef.current = true;
onLoad({nativeEvent: {width, height}});
},
[onLoad, setAttachmentLoaded, source],
);

return (
<ExpoImage
// Only subscribe to onLoad when a handler is provided to avoid unnecessary event registrations, optimizing performance.
onLoad={onLoad ? imageLoadedSuccessfully : undefined}
source={source}
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
// Temporary solution only, since other cache policies are causing memory leaks on iOS
cachePolicy="none"
/>
);
}

export default BaseImage;
3 changes: 2 additions & 1 deletion src/components/ImageSVG/index.ios.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@ function ImageSVG({src, width = '100%', height = '100%', fill, contentFit = 'cov
return (
<Image
onLoadEnd={onLoadEnd}
cachePolicy="memory-disk"
contentFit={contentFit}
source={src}
style={[{width, height}, style]}
// eslint-disable-next-line react/jsx-props-no-spreading
{...tintColorProp}
// Temporary solution only, since other cache policies are causing memory leaks on iOS
cachePolicy="none"
/>
);
}
Expand Down
Loading