diff --git a/Example/testHotUpdate/src/index.js b/Example/testHotUpdate/src/index.js index 3e1e896..42af99b 100644 --- a/Example/testHotUpdate/src/index.js +++ b/Example/testHotUpdate/src/index.js @@ -49,6 +49,10 @@ export default class App extends Component { }; checkUpdate = () => { + if (__DEV__) { + // 开发模式不支持热更新,跳过检查 + return; + } checkUpdate(appKey).then(info => { if (info.expired) { Alert.alert('提示', '您的应用版本已更新,请前往应用商店下载新的版本', [ @@ -63,7 +67,7 @@ export default class App extends Component { ]); } }).catch(err => { - Alert.alert('提示', '检查更新失败.'); + console.warn(err); }); }; diff --git a/docs/guide2.md b/docs/guide2.md index 9a6a2f9..3bee1ba 100644 --- a/docs/guide2.md +++ b/docs/guide2.md @@ -87,7 +87,7 @@ import _updateConfig from './update.json'; const {appKey} = _updateConfig[Platform.OS]; class MyProject extends Component { - componentWillMount(){ + componentDidMount(){ if (isFirstTime) { Alert.alert('提示', '这是当前版本第一次启动,是否要模拟启动失败?失败将回滚到上一版本', [ {text: '是', onPress: ()=>{throw new Error('模拟启动失败,请重启应用')}}, @@ -109,6 +109,10 @@ class MyProject extends Component { }); }; checkUpdate = () => { + if (__DEV__) { + // 开发模式不支持热更新,跳过检查 + return; + } checkUpdate(appKey).then(info => { if (info.expired) { Alert.alert('提示', '您的应用版本已更新,请前往应用商店下载新的版本', [ @@ -123,7 +127,7 @@ class MyProject extends Component { ]); } }).catch(err => { - Alert.alert('提示', '更新失败.'); + console.warn(err); }); }; render() { diff --git a/lib/index.js b/lib/index.js index 970d59c..32faf76 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,11 +1,7 @@ -/** - * Created by tdzl2003 on 4/4/16. - */ +import { NativeAppEventEmitter, NativeModules } from 'react-native'; +const { HotUpdate } = NativeModules; -const HotUpdate = require('react-native').NativeModules.HotUpdate; -import {NativeAppEventEmitter} from 'react-native'; - -let host = 'https://update.reactnative.cn/api'; +const host = 'https://update.reactnative.cn/api'; export const downloadRootDir = HotUpdate.downloadRootDir; export const packageVersion = HotUpdate.packageVersion; @@ -36,11 +32,20 @@ There is available update: diffUrl: 'http://update-packages.reactnative.cn/hash', } */ + +function assertRelease() { + if (__DEV__) { + throw new Error('react-native-update can only run on RELEASE version.'); + } +} + + export async function checkUpdate(APPKEY) { + assertRelease(); const resp = await fetch(`${host}/checkUpdate/${APPKEY}`, { method: 'POST', headers: { - 'Accept': 'application/json', + Accept: 'application/json', 'Content-Type': 'application/json', }, body: JSON.stringify({ @@ -53,10 +58,11 @@ export async function checkUpdate(APPKEY) { throw new Error((await resp.json()).message); } - return await resp.json(); + return resp.json(); } export async function downloadUpdate(options) { + assertRelease(); if (!options.update) { return; } @@ -81,23 +87,21 @@ export async function downloadUpdate(options) { return options.hash; } -export async function switchVersion(hash) { - HotUpdate.reloadUpdate({hashName:hash}); +export function switchVersion(hash) { + assertRelease(); + HotUpdate.reloadUpdate({ hashName: hash }); } -export async function switchVersionLater(hash) { - HotUpdate.setNeedUpdate({hashName:hash}); +export function switchVersionLater(hash) { + assertRelease(); + HotUpdate.setNeedUpdate({ hashName: hash }); } export function markSuccess() { + assertRelease(); HotUpdate.markSuccess(); } +NativeAppEventEmitter.addListener('RCTHotUpdateDownloadProgress', params => {}); -NativeAppEventEmitter.addListener('RCTHotUpdateDownloadProgress',(params)=>{ - -}) - -NativeAppEventEmitter.addListener('RCTHotUpdateUnzipProgress',(params)=>{ - -}) \ No newline at end of file +NativeAppEventEmitter.addListener('RCTHotUpdateUnzipProgress', params => {});