Skip to content
This repository was archived by the owner on Apr 15, 2025. It is now read-only.

Commit 74715a6

Browse files
committed
Convert package to typescript and fix some type issues along the way
1 parent 24e37d7 commit 74715a6

14 files changed

+6429
-1001
lines changed

.eslintrc.js

Lines changed: 0 additions & 50 deletions
This file was deleted.

.gitignore

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
/node_modules
22
/coverage
3-
/.idea
3+
/.idea
4+
5+
# build
6+
dist/
7+
8+
# TypeScript incremental compilation cache
9+
*.tsbuildinfo

README.md

Lines changed: 68 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ React Native Higher Order Component that adds advanced caching functionality to
99

1010
## Features
1111

12-
* **Drop in Replacement** for native \<Image\> component.
13-
* **Automatically Cache** remote image files to local filesystem to increase performance.
14-
* **Automatically Persist** remote image files to local filesystem _forever_ with a simple component prop flag.
12+
- **Drop in Replacement** for native \<Image\> component.
13+
- **Automatically Cache** remote image files to local filesystem to increase performance.
14+
- **Automatically Persist** remote image files to local filesystem _forever_ with a simple component prop flag.
1515

1616
## Installation
1717

@@ -37,45 +37,43 @@ To troubleshoot linking, refer to [the react-native-fs installation instructions
3737

3838
## Usage
3939

40-
React Native Image Cache HOC creates an advanced image component, \<CacheableImage\>, that is a drop in replacement for the standard \<Image\> component.
40+
React Native Image Cache HOC creates an advanced image component, \<CacheableImage\>, that is a drop in replacement for the standard \<Image\> component.
4141

4242
**Differences between the advanced image component and standard image component API are as follows:**
4343

4444
1. **Modified "source" Prop:** The advanced component "source" prop only accepts a web accessible url (there's no reason to use this library to render files that already exist on the local filesystem), and it does NOT accept an array of urls.
4545
2. **New "permanent" Prop:** The new, optional (defaults to False), "permanent" prop determines if the image file should be stored forever on the local filesystem instead of written to a temperary cache that is subject to occasional pruning.
46-
3. **New "placeholder" Prop:** The new, optional (defaults to standard Image component), "placeholder" prop determines component to render while remote image file is downloading.
46+
3. **New "placeholder" Prop:** The new, optional (defaults to standard Image component), "placeholder" prop determines component to render while remote image file is downloading.
4747

4848
**TL;DR: To cache image files for performance, simply use \<CacheableImage\> as a drop in replacement for \<Image\>. To store files permanently add a permanent={true} prop to \<CacheableImage\>.**
4949

5050
```js
51+
import React, { Component } from 'react'
52+
import { StyleSheet, Text, View, Image } from 'react-native'
5153

52-
import React, { Component } from 'react';
53-
import {
54-
StyleSheet,
55-
Text,
56-
View,
57-
Image
58-
} from 'react-native';
59-
60-
import imageCacheHoc from 'react-native-image-cache-hoc';
54+
import { imageCacheHoc } from 'react-native-image-cache-hoc'
6155

6256
/**
63-
* Pass the native <Image> component into imageCacheHoc() to create the advanced image component <CacheableImage>.
64-
*
65-
* imageCacheHoc() takes an options object as the second parameter (refer to options section of README.md)
66-
*/
57+
* Pass the native <Image> component into imageCacheHoc() to create the advanced image component <CacheableImage>.
58+
*
59+
* imageCacheHoc() takes an options object as the second parameter (refer to options section of README.md)
60+
*/
6761
const CacheableImage = imageCacheHoc(Image, {
68-
fileHostWhitelist: ['i.redd.it']
69-
});
62+
fileHostWhitelist: ['i.redd.it'],
63+
})
7064

7165
export default class App extends Component<{}> {
7266
render() {
7367
return (
7468
<View style={styles.container}>
7569
<Text style={styles.welcome}>Welcome to React Native!</Text>
76-
<CacheableImage style={styles.image} source={{uri: 'https://i.redd.it/rc29s4bz61uz.png'}} permanent={false} />
70+
<CacheableImage
71+
style={styles.image}
72+
source={{ uri: 'https://i.redd.it/rc29s4bz61uz.png' }}
73+
permanent={false}
74+
/>
7775
</View>
78-
);
76+
)
7977
}
8078
}
8179

@@ -92,11 +90,10 @@ const styles = StyleSheet.create({
9290
margin: 10,
9391
},
9492
image: {
95-
width:150,
96-
height: 204
97-
}
98-
});
99-
93+
width: 150,
94+
height: 204,
95+
},
96+
})
10097
```
10198

10299
## Options
@@ -105,28 +102,27 @@ React Native Image Cache HOC accepts an options object in order to tweak standar
105102

106103
```js
107104
imageCacheHoc(Image, {
108-
109-
// Allow http urls.
105+
// Allow http urls.
110106
// Defaults to https only.
111107
validProtocols: ['http', 'https'],
112-
113-
// Use domain host whitelist.
108+
109+
// Use domain host whitelist.
114110
// Defaults to allowing urls from all domain hosts.
115111
fileHostWhitelist: ['localhost', 'i.redd.it'],
116-
117-
// Namespace the directory that stores files to avoid collisions with other app libraries.
112+
113+
// Namespace the directory that stores files to avoid collisions with other app libraries.
118114
// Defaults to 'react-native-image-cache-hoc'.
119115
fileDirName: 'example-app-files-namespace',
120-
121-
// Max size of file cache in bytes before pruning occurs.
122-
// Note that cache size can exceed this limit,
123-
// but sequential writes to the cache will trigger cache pruning
116+
117+
// Max size of file cache in bytes before pruning occurs.
118+
// Note that cache size can exceed this limit,
119+
// but sequential writes to the cache will trigger cache pruning
124120
// which will delete cached files until total cache size is below this limit before writing.
125121
// Defaults to 15 MB.
126122
cachePruneTriggerLimit: 1024 * 1024 * 10,
127-
128-
// Default placeholder component to render while remote image file is downloading.
129-
// Can be overridden with placeholder prop like <CacheableImage placeholder={placeHolderObject} />.
123+
124+
// Default placeholder component to render while remote image file is downloading.
125+
// Can be overridden with placeholder prop like <CacheableImage placeholder={placeHolderObject} />.
130126
//
131127
// Placeholder Object is structed like:
132128
// const placeHolderObject = {
@@ -141,11 +137,10 @@ imageCacheHoc(Image, {
141137
defaultPlaceholder: {
142138
component: ActivityIndicator,
143139
props: {
144-
style: activityIndicatorStyle
145-
}
146-
}
147-
148-
});
140+
style: activityIndicatorStyle,
141+
},
142+
},
143+
})
149144
```
150145

151146
## Using Loading Placeholders
@@ -177,7 +172,7 @@ const styles = StyleSheet.create({
177172
}
178173
});
179174

180-
// This placeholder object will be used as a placeholder component for all instances of <CacheableImage>
175+
// This placeholder object will be used as a placeholder component for all instances of <CacheableImage>
181176
// unless individual <CacheableImage> uses "placeholder" prop to override this default.
182177
const defaultPlaceholderObject = {
183178
component: ActivityIndicator,
@@ -222,35 +217,32 @@ The CacheableImage class returned by React Native Image Cache HOC includes a cou
222217
Use this method if you need to download a file to the local filesystem prior to rendering \<CacheableImage\> for some reason (perhaps to pre-warm the local cache). If calling this method repeatedly to cache a long list of files, be sure to use a queue and limit concurrency so your app performance does not suffer.
223218

224219
```js
225-
import imageCacheHoc from 'react-native-image-cache-hoc';
226-
const CacheableImage = imageCacheHoc(Image);
227-
CacheableImage.cacheFile('https://i.redd.it/17ymhqwgbswz.jpg')
228-
.then( localFileInfo => {
229-
console.log(localFileInfo);
230-
// The https://i.redd.it/17ymhqwgbswz.jpg remote file is now saved to local fs.
231-
// Since permanent was not set, this file is subject to cache pruning.
232-
});
233-
234-
CacheableImage.cacheFile('https://i.redd.it/hhhim0kc5swz.jpg', true)
235-
.then( localFileInfo => {
236-
console.log(localFileInfo);
237-
// The https://i.redd.it/hhhim0kc5swz.jpg remote file is now saved to local fs permanently.
238-
});
220+
import { imageCacheHoc } from 'react-native-image-cache-hoc'
221+
const CacheableImage = imageCacheHoc(Image)
222+
CacheableImage.cacheFile('https://i.redd.it/17ymhqwgbswz.jpg').then((localFileInfo) => {
223+
console.log(localFileInfo)
224+
// The https://i.redd.it/17ymhqwgbswz.jpg remote file is now saved to local fs.
225+
// Since permanent was not set, this file is subject to cache pruning.
226+
})
227+
228+
CacheableImage.cacheFile('https://i.redd.it/hhhim0kc5swz.jpg', true).then((localFileInfo) => {
229+
console.log(localFileInfo)
230+
// The https://i.redd.it/hhhim0kc5swz.jpg remote file is now saved to local fs permanently.
231+
})
239232
```
240233

241234
**CacheableImage.flush()**
242235

243236
Delete all locally stored image files created by react-native-image-cache-hoc (cache AND permanent). Calling this method will cause a performance hit on your app until the local files are rebuilt.
244237

245238
```js
246-
import imageCacheHoc from 'react-native-image-cache-hoc';
247-
const CacheableImage = imageCacheHoc(Image);
248-
CacheableImage.flush()
249-
.then( flushResults => {
250-
console.log(flushResults);
251-
// All local filles created by 'react-native-image-cache-hoc' are now destroyed.
252-
// They will be rebuilt by future <CacheableImage> renders.
253-
});
239+
import { imageCacheHoc } from 'react-native-image-cache-hoc'
240+
const CacheableImage = imageCacheHoc(Image)
241+
CacheableImage.flush().then((flushResults) => {
242+
console.log(flushResults)
243+
// All local filles created by 'react-native-image-cache-hoc' are now destroyed.
244+
// They will be rebuilt by future <CacheableImage> renders.
245+
})
254246
```
255247

256248
## Jest Test Support
@@ -261,28 +253,25 @@ React Native Image Cache HOC must be run in a native environment to work correct
261253

262254
```js
263255
jest.mock('react-native-image-cache-hoc', () => {
256+
const mockComponent = require('react-native/jest/mockComponent')
257+
const MockCacheableImage = mockComponent('Image')
264258

265-
const mockComponent = require('react-native/jest/mockComponent');
266-
const MockCacheableImage = mockComponent('Image');
267-
268259
// Add mock static methods
269260
// To see how to use jest.fn() to return mock data in your tests see the following:
270261
// https://facebook.github.io/jest/docs/en/mock-function-api.html
271-
MockCacheableImage.cacheFile = jest.fn();
272-
MockCacheableImage.flush = jest.fn();
262+
MockCacheableImage.cacheFile = jest.fn()
263+
MockCacheableImage.flush = jest.fn()
273264

274-
return function() {
275-
return MockCacheableImage;
265+
return function () {
266+
return MockCacheableImage
276267
}
277-
278-
});
268+
})
279269
```
280270

281-
282271
## Warning
283272

284273
iOS only allows requests to https urls. If you need to load image files using http you will need to make additional react native config changes.
285274

286275
> By default, iOS will block any request that's not encrypted using SSL. If you need to fetch from a cleartext URL (one that begins with http) you will first need to add an App Transport Security exception. If you know ahead of time what domains you will need access to, it is more secure to add exceptions just for those domains; if the domains are not known until runtime you can disable ATS completely. Note however that from January 2017, Apple's App Store review will require reasonable justification for disabling ATS.
287276
288-
https://facebook.github.io/react-native/docs/network.html
277+
https://facebook.github.io/react-native/docs/network.html

0 commit comments

Comments
 (0)