Skip to content

Commit

Permalink
Fixed #92, check whether assets are already downloaded.
Browse files Browse the repository at this point in the history
  • Loading branch information
Denyllon committed Aug 20, 2020
1 parent fcbd32b commit 64f6d61
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 19 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ Packages which should be installed within your project:

***If you're using this package with Expo Tools, please make sure your `Expo SDK` version is higher than or equal to `v38.0.0`, otherwise use the `v2.2.3` version of this package, which should work from `v33.0.0`.***

***In bare Ract Native application you need to also install the `react-native-unimodules` package, and configure the content of `ios` and `android` build directiories like it's described [here](https://docs.expo.io/bare/installing-unimodules/#installation).***

### Installing

Get package from NPM in your React app:
Expand Down Expand Up @@ -385,3 +387,24 @@ Bugfixes:
- [#82](https://github.com/highcharts/highcharts-react-native/issues/82) - Unable to set `backgroundColor` due to old version of `react-native-webview`,
- [#85](https://github.com/highcharts/highcharts-react-native/issues/85) - Error: Failed to compile,
- [#86](https://github.com/highcharts/highcharts-react-native/issues/86) - Can't render the chart on Android

### 3.0.1
- Moved the `react`, `react-native-unimodules`, `react-native-webview` packages into `peerDependencies`.
- Docs improved

### 3.1.0
- The `package.json` cleaned-up, unused dependencies removed.
- Unhandled promise rejections resolved
- Docs improved

Bugfixes:
- [#91](https://github.com/highcharts/highcharts-react-native/issues/91) - Error expo without expo and no charts

### 3.1.1:
- Restored the `react-native-webview` back into the `peerDependencies`.
- Docs improved

### 3.1.2
- Docs improved
Bugfixes:
- [#92] - Charts sometimes fail to render
23 changes: 23 additions & 0 deletions dist/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ Packages which should be installed within your project:

***If you're using this package with Expo Tools, please make sure your `Expo SDK` version is higher than or equal to `v38.0.0`, otherwise use the `v2.2.3` version of this package, which should work from `v33.0.0`.***

***In bare Ract Native application you need to also install the `react-native-unimodules` package, and configure the content of `ios` and `android` build directiories like it's described [here](https://docs.expo.io/bare/installing-unimodules/#installation).***

### Installing

Get package from NPM in your React app:
Expand Down Expand Up @@ -385,3 +387,24 @@ Bugfixes:
- [#82](https://github.com/highcharts/highcharts-react-native/issues/82) - Unable to set `backgroundColor` due to old version of `react-native-webview`,
- [#85](https://github.com/highcharts/highcharts-react-native/issues/85) - Error: Failed to compile,
- [#86](https://github.com/highcharts/highcharts-react-native/issues/86) - Can't render the chart on Android

### 3.0.1
- Moved the `react`, `react-native-unimodules`, `react-native-webview` packages into `peerDependencies`.
- Docs improved

### 3.1.0
- The `package.json` cleaned-up, unused dependencies removed.
- Unhandled promise rejections resolved
- Docs improved

Bugfixes:
- [#91](https://github.com/highcharts/highcharts-react-native/issues/91) - Error expo without expo and no charts

### 3.1.1:
- Restored the `react-native-webview` back into the `peerDependencies`.
- Docs improved

### 3.1.2
- Docs improved
Bugfixes:
- [#92] - Charts sometimes fail to render
2 changes: 1 addition & 1 deletion dist/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@highcharts/highcharts-react-native",
"version": "3.1.1",
"version": "3.1.2",
"license": "SEE LICENSE IN <LICENSE>",
"repository": {
"type": "git",
Expand Down
38 changes: 21 additions & 17 deletions dist/src/HighchartsReactNative.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ export default class HighchartsReactNative extends React.PureComponent {
};
}

getHcAssets = async (useCDN) => {
setHcAssets = async (useCDN) => {
try {
await this.setLayout()
await this.getScript('highcharts', null, useCDN)
await this.getScript('highcharts-more', null, useCDN)
await this.getScript('highcharts-3d', null, useCDN)
await this.addScript('highcharts', null, useCDN)
await this.addScript('highcharts-more', null, useCDN)
await this.addScript('highcharts-3d', null, useCDN)
for (const mod of this.state.modules) {
await this.getScript(mod, true, useCDN)
await this.addScript(mod, true, useCDN)
}
this.setState({
hcModulesReady: true
Expand All @@ -47,38 +47,42 @@ export default class HighchartsReactNative extends React.PureComponent {
console.error("Failed to fetch scripts or layout. " + error.message)
}
}

getAssetAsString = async (asset) => {
const downloadedModules = await FileSystem.readDirectoryAsync(FileSystem.cacheDirectory)
let fileName = 'ExponentAsset-' + asset.hash + '.' + asset.type

getScript = async (name, isModule, useCDN) => {
let inline;
if (!downloadedModules.includes(fileName)) {
await asset.downloadAsync()
}

return await FileSystem.readAsStringAsync(FileSystem.cacheDirectory + fileName)
}

addScript = async (name, isModule, useCDN) => {
if(useCDN) {
const response = await fetch(
httpProto + cdnPath + (isModule ? 'modules/' : '') + name + '.js'
).catch((error) => {
throw error
})
inline = await response.text()
stringifiedScripts[name] = await response.text()
} else {
const script = Asset.fromModule(
isModule &&
name !== 'highcharts-more' &&
name !== 'highcharts-3d' ?
HighchartsModules.modules[name] : HighchartsModules[name]
)

await script.downloadAsync()
inline = await FileSystem.readAsStringAsync(script.localUri)
stringifiedScripts[name] = await this.getAssetAsString(script)
}

stringifiedScripts[name] = inline
}

setLayout = async () => {
const indexHtml = Asset.fromModule(require('../highcharts-layout/index.html'))
await indexHtml.downloadAsync()
const htmlString = await FileSystem.readAsStringAsync(indexHtml.localUri)

this.setState({
layoutHTML: htmlString
layoutHTML: await this.getAssetAsString(indexHtml)
})
}

Expand Down Expand Up @@ -108,7 +112,7 @@ export default class HighchartsReactNative extends React.PureComponent {
};
this.webviewRef = null

this.getHcAssets(this.state.useCDN)
this.setHcAssets(this.state.useCDN)
}
componentDidUpdate() {
this.webviewRef && this.webviewRef.postMessage(this.serialize(this.props.options, true));
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"main": "node_modules/expo/AppEntry.js",
"name": "highcharts-react-native",
"version": "3.1.1",
"version": "3.1.2",
"scripts": {
"start": "expo start",
"android": "expo start --android",
Expand Down

0 comments on commit 64f6d61

Please sign in to comment.