From af11271ff761006815e020173ccf2e29b6c57565 Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Fri, 1 Nov 2024 02:04:58 +0000 Subject: [PATCH] Make image-sizes LRU Code is copy paste from useImageDimensions.ts --- src/lib/media/image-sizes.ts | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/lib/media/image-sizes.ts b/src/lib/media/image-sizes.ts index b7787ec171..017300b41e 100644 --- a/src/lib/media/image-sizes.ts +++ b/src/lib/media/image-sizes.ts @@ -2,7 +2,23 @@ import {Image} from 'react-native' import type {Dimensions} from '#/lib/media/types' -const sizes: Map = new Map() +type CacheStorageItem = {key: string; value: T} +const createCache = (cacheSize: number) => ({ + _storage: [] as CacheStorageItem[], + get(key: string) { + const {value} = + this._storage.find(({key: storageKey}) => storageKey === key) || {} + return value + }, + set(key: string, value: T) { + if (this._storage.length >= cacheSize) { + this._storage.shift() + } + this._storage.push({key, value}) + }, +}) + +const sizes = createCache(50) const activeRequests: Map> = new Map() export function get(uri: string): Dimensions | undefined {