UNPKG

@applicaster/zapp-react-native-utils

Version:

Applicaster Zapp React Native utilities package

55 lines (48 loc) 1.38 kB
import { Image } from "./Image"; /** * Image Cache manager. It is exported as such to behave * as a singleton and make sure the cache is shared across * all components in the app */ export const ImageCache = (function (cache, imageCache) { /** * adds an image to the cached based on its url * if it's already there, will be skipped. * the object stored in the cache is an `Image` object * constructed from the provided url * @param {string} imageUrl to set the cache for. */ imageCache.cache = function (imageUrl) { if (cache.has(imageUrl)) { return cache.get(imageUrl); } else { const image = new Image(imageUrl); cache.set(imageUrl, image); return image; } }; /** * removes an image from the cache */ imageCache.remove = function (imageUrl) { cache.delete(imageUrl); }; /** * retrieves an image from the cache. if it doesn't exist, will return null * @param {string} imageUrl * @returns {Image} */ imageCache.get = function (imageUrl) { return cache.has(imageUrl) ? cache.get(imageUrl) : null; }; /** * resets the cache by clearing all entries * only used for tests ! will do nothing if not in test env. */ imageCache.reset = function () { if (process.env.NODE_ENV === "test") { cache.clear(); } }; return imageCache; })(new Map(), {});