npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-native-smart-image-cache

v1.0.10

Published

A caching system for the react-native image component. Allows the manual refreshing of an image as well as setting a timeout for the cache to redownload images after a certain period of time.

Downloads

16

Readme

react-native-smart-image-cache

A caching system for the react-native image component. Allows the manual refreshing of an image as well as setting a timeout for the cache to redownload images after a certain period of time.

Installation

This package depends on having react-native-fetch-blob installed into your project. Follow the instructions for installation at react-native-fetch-blob. At the very least, run yarn add react-native-fetch-blob, then react-native link and add

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

to your android/app/src/main/AndroidManifest.xml file. Then install the react-native-smart-image-cache package.

yarn add react-native-smart-image-cache

Usage

To use the included SmartImage component, use the import statement to load the module.

import { SmartImage } from 'react-native-smart-image-cache'

Pass in style props as usual. To load an image, pass the url prop to the component. The image will be downloaded and saved to the device. You can use the fallbackURI property to set a local uri the component can fall back to if the download fails. You can also pass httpHeaders for use in the image download with the httpHeaders property. When the component is reloaded, it will check the cache for the image. If the image exists and is younger than the cache timeout (1/2 hour) the image is redownloaded and saved to the device. To force an image refresh, set the reload prop to "force". This will force the component to download the image. Here is a simple example of forcing an image to redownload.

export default class App extends Component {
  constructor() {
    super(); 
    this.state = {
      url: "https://picsum.photos/200/200/?random",
      reload: "",
    }
  }
  
  ForceImageDownload() {
    this.setState({ reload: "force" },function() { //Updates props on the SmartImage, triggering the image download if "reload" is force
      this.setState({reload: ""}) //Set reload to nothing after to make sure any other property changes don't trigger a reload
    });
  }
  
  render() {
    <View>
      <Button title="Force Image Download" onPress={() => { this.ForceImageDownload() }} />
      <SmartImage style={{height:200, width: 200}} url={this.state.url} reload={this.state.reload} />
    </View>
  }
}

The cache can also be used by itself, the currently implemented methods are: GetCache(timeout), Subscribe(url, callback, httpHeaders = {}), Unsubscribe(url, callback), GetURIFromURL(url, httpHeaders={}) and DownloadImage(url, httpHeaders = {}). Always get the cache reference from the Promise returned by GetCache. You can set the timeout of the cache by passing in a value the FIRST time you call this function. The cache is a static object so it will be initilized with this value. Next, you can subscribe a component to listen whenever a specified url in the cache updates by calling Subscribe. Pass in the url you want to listen to and the function that should be run when the uri for the url updates. The callback needs to be in the form of function(uri) to work properly. Unsubscribe removes the specified function from receiving notifications of url->uri updates. GetURIFromURL will return the uri for the specified url. If the url doesn't yet have a cache entry, it's downloaded. Subscribers who subscribed via the Subscribe function will be notified when the download is complete. (There is currently a bug in the SmartImageCache component that triggers the notification with a uri set to "". I can't figure out why it does this, but if you just ignore the "" passed in, everything else seems to work okay.) If the cache entry is invalid due to age, the image is redownloaded and subscribers are notified. To force an image download, call DownloadImage with the url of the image to cache. Subscribers will be notified when the download completes. To use the cache by itself, just include it:

import { SmartImageCache } from 'react-native-smart-image-cache'

You can also delete the cache by calling DeleteCache() on the cache object. This can be useful for clobbering cached profile pictures on logout for example.