From 08ac354ba5f62db6929ab9761601c0871c73e679 Mon Sep 17 00:00:00 2001 From: sunnylqm Date: Fri, 4 Oct 2019 22:27:33 +0800 Subject: [PATCH] =?UTF-8?q?=E9=81=BF=E5=85=8D=E5=9C=A8debug=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F=E4=B8=8B=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Example/testHotUpdate/src/index.js | 6 +++- docs/guide2.md | 8 ++++-- lib/index.js | 44 ++++++++++++++++-------------- 3 files changed, 35 insertions(+), 23 deletions(-) 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 => {});