Skip to content

Commit

Permalink
feat(cache): Add clear() method.
Browse files Browse the repository at this point in the history
  • Loading branch information
wcandillon committed Apr 15, 2017
1 parent 409a06f commit efb9bab
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,29 @@ import {CachedImage} from "react-native-img-cache";

### ImageCache

#### clear()

Remove cache entries and all physical files.

```js
ImageCache.get().clear();
```

#### bust(uri)

`ImageCache` can be used to bust an image from the local cache.
This removes the cache entry but it **does not remove any physical files**.

```js
ImageCache.getCache().bust("https://i.ytimg.com/vi/yaqe1qesQ8c/maxresdefault.jpg");
ImageCache.get().bust("https://i.ytimg.com/vi/yaqe1qesQ8c/maxresdefault.jpg");
```

#### cancel(uri)

It can also be used to cancel the download of an image. This can be very useful when [scrolling through images](https://medium.com/@wcandillon/image-pipeline-with-react-native-listview-b92d4768b17c).

```js
ImageCache.getCache().cancel("https://i.ytimg.com/vi/yaqe1qesQ8c/maxresdefault.jpg");
ImageCache.get().cancel("https://i.ytimg.com/vi/yaqe1qesQ8c/maxresdefault.jpg");
```

#### on(uri, observer, immutable)
Expand All @@ -65,7 +74,7 @@ const immutable = true;
const observer = (path: string) => {
console.log(`path of the image in the cache: ${path}`);
};
ImageCache.getCache().on(uri, observer, immutable);
ImageCache.get().on(uri, observer, immutable);
```

We use the observer pattern instead of a promise because a mutable image might have different version with different paths in the cache.
Expand All @@ -75,5 +84,5 @@ We use the observer pattern instead of a promise because a mutable image might h
Observers can be deregistered using `dispose`:

```js
ImageCache.getCache().dispose(uri, observer);
ImageCache.get().dispose(uri, observer);
```
15 changes: 12 additions & 3 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,24 @@ export class ImageCache {

private constructor() {}

static getCache(): ImageCache {
static get(): ImageCache {
if (!ImageCache.instance) {
ImageCache.instance = new ImageCache();
}
return ImageCache.instance;
}

static getCache(): ImageCache {
return ImageCache.get();
}

private cache: { [uri: string]: CacheEntry } = {};

clear() {
this.cache = {};
return RNFetchBlob.fs.unlink(BASE_DIR);
}

on(uri: string, handler: CacheHandler, immutable?: boolean) {
if (!this.cache[uri]) {
this.cache[uri] = {
Expand Down Expand Up @@ -143,15 +152,15 @@ export class CachedImage extends Component<CachedImageProps, CachedImageState>

private dispose() {
if (this.uri) {
ImageCache.getCache().dispose(this.uri, this.handler);
ImageCache.get().dispose(this.uri, this.handler);
}
}

private observe(uri: string, mutable: boolean) {
if (uri !== this.uri) {
this.dispose();
this.uri = uri;
ImageCache.getCache().on(uri, this.handler, !mutable);
ImageCache.get().on(uri, this.handler, !mutable);
}
}

Expand Down

0 comments on commit efb9bab

Please sign in to comment.