From 3d08794c93bc055c46de0d809aa7316d209da34d Mon Sep 17 00:00:00 2001 From: Alexander NeonXP Kiryukhin Date: Sun, 9 Jun 2019 20:54:31 +0300 Subject: smart caching --- src/index.js | 29 +++++++++++++++++++++++------ src/tile.js | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 6 deletions(-) create mode 100644 src/tile.js 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 => ( ( { + 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 () + } +} \ No newline at end of file -- cgit v1.2.3