diff options
author | Alexander NeonXP Kiryukhin <a.kiryukhin@mail.ru> | 2019-06-09 20:54:31 +0300 |
---|---|---|
committer | Alexander NeonXP Kiryukhin <a.kiryukhin@mail.ru> | 2019-06-09 20:54:31 +0300 |
commit | 3d08794c93bc055c46de0d809aa7316d209da34d (patch) | |
tree | 4649817a8d812f72250cff390861f41e189d2b0c | |
parent | 2a5b10f500c8909a371010bc67cce3896f5fa781 (diff) |
smart caching
-rw-r--r-- | src/index.js | 29 | ||||
-rw-r--r-- | src/tile.js | 38 |
2 files changed, 61 insertions, 6 deletions
diff --git a/src/index.js b/src/index.js index ff719e7..e69a005 100644 --- a/src/index.js +++ b/src/index.js @@ -1,7 +1,8 @@ import React, { PureComponent } from 'react' import { Image, View, PixelRatio, PanResponder } from 'react-native' import Svg, { Polyline, Polygon } from 'react-native-svg' - +import * as FileSystem from 'expo-file-system'; +import Tile from './tile' import debounce from './debounce' const DEBOUNCE_DELAY = 60 @@ -86,10 +87,27 @@ export default class Map extends PureComponent { deltaY: 0, deltaScale: 0, } + FileSystem.makeDirectoryAsync(FileSystem.cacheDirectory + 'tiles').catch(NOOP) } componentDidMount() { this.syncToProps() + this.gc = setInterval(() => { + const time = +(new Date()) / 1000 + FileSystem.readDirectoryAsync(FileSystem.cacheDirectory + 'tiles').then(list => { + list.map(item => { + FileSystem.getInfoAsync(FileSystem.cacheDirectory + 'tiles/' + item).then(finfo => { + if (time - finfo.modificationTime > 1000 * 60) { + FileSystem.deleteAsync(finfo.uri) + } + }) + }) + }) + }, 1000) + } + + componentWillUnmount() { + clearInterval(this.gc) } componentWillMount() { @@ -465,7 +483,7 @@ export default class Map extends PureComponent { } this.setState({ - pixelDelta: null, + pixelDelta: [0, 0], zoomDelta: 0 }, NOOP) @@ -538,7 +556,6 @@ export default class Map extends PureComponent { const { oldTiles } = this.state const dprs = [1, PixelRatio.get()] const mapUrl = this.props.provider || providers['wikimedia'] - const { tileMinX, tileMaxX, @@ -613,10 +630,10 @@ export default class Map extends PureComponent { } } - return tiles.map(tile => (<Image + return tiles.map(tile => (<Tile key={tile.key} - source={{ uri: tile.url, cache: 'force-cache' }} - resizeMethod={"scale"} + tileKey={tile.key} + source={tile.url} style={{ height: tile.height, width: tile.width, diff --git a/src/tile.js b/src/tile.js new file mode 100644 index 0000000..044f086 --- /dev/null +++ b/src/tile.js @@ -0,0 +1,38 @@ +import React, { Component } from "react"; +import { Image, View, ActivityIndicator } from "react-native"; +import * as FileSystem from 'expo-file-system'; + +export default class Tile extends Component { + constructor(props) { + super(props) + this.state = { + loaded: false, + uri: '', + } + } + componentDidMount() { + this._mounted = true; + this.load() + } + load = async () => { + const fileUri = FileSystem.cacheDirectory + 'tiles/' + this.props.tileKey + '.png' + let finfo = await FileSystem.getInfoAsync(fileUri) + if (!finfo.exists) { + finfo = await FileSystem.downloadAsync(this.props.source, fileUri) + } + if (this._mounted) { + this.setState({ loaded: true, uri: finfo.uri }) + } + this.props.onLoad && this.props.onLoad() + } + + componentWillUnmount() { + this._mounted = false; + } + render() { + if (!this.state.loaded) { + return null + } + return (<Image style={this.props.style} source={{ uri: this.state.uri }} resizeMethod={"scale"} />) + } +}
\ No newline at end of file |