1
0
mirror of https://gitcode.com/gh_mirrors/re/react-native-pushy.git synced 2025-11-22 23:46:10 +08:00
Code Issues Packages Projects Releases Wiki Activity GitHub Gitee

Compare commits

...

6 Commits

13 changed files with 307 additions and 117 deletions

View File

@@ -1,5 +1,6 @@
{ {
"lockfileVersion": 1, "lockfileVersion": 1,
"configVersion": 0,
"workspaces": { "workspaces": {
"": { "": {
"name": "testHotUpdate", "name": "testHotUpdate",

View File

@@ -25,7 +25,7 @@ import { LocalSvg } from 'react-native-svg/css';
import TestConsole from './TestConsole'; import TestConsole from './TestConsole';
import _updateConfig from '../update.json'; import _updateConfig from '../update.json';
import { UpdateProvider, Pushy, Cresc, useUpdate } from 'react-native-update'; import { UpdateProvider, Pushy, useUpdate } from 'react-native-update';
const { appKey } = _updateConfig[Platform.OS]; const { appKey } = _updateConfig[Platform.OS];
function App() { function App() {
@@ -53,7 +53,7 @@ function App() {
return ( return (
<View style={styles.container}> <View style={styles.container}>
<Text style={styles.welcome}>22使Pushy热更新服务</Text> <Text style={styles.welcome}>使Pushy热更新服务</Text>
<View style={{ flexDirection: 'row' }}> <View style={{ flexDirection: 'row' }}>
<Text> <Text>
{useDefaultAlert ? '当前使用' : '当前不使用'}alert更新提示 {useDefaultAlert ? '当前使用' : '当前不使用'}alert更新提示

View File

@@ -19,6 +19,33 @@
7. meta 信息及开放 API提供更高扩展性。 7. meta 信息及开放 API提供更高扩展性。
8. 提供付费的专人技术支持。 8. 提供付费的专人技术支持。
### 与其他热更新库对比
| 对比维度 | react-native-update | expo-update | react-native-code-push |
|---------|---------------------|-------------|------------------------|
| **价格/成本** | 提供免费额度,多级梯度付费(最低约 66 元/月),流量不单独计费 | 提供免费额度,多级梯度付费(最低约 136 元/月),超出流量额外计费 | ❌ **已停运**Microsoft App Center 已于 2025 年 3 月 31 日停止服务) |
| **更新包大小** | ⭐⭐⭐⭐⭐ 几十 KB增量更新 | ⭐⭐⭐ 全量更新(通常几十 MB | ❌ **已停运** |
| **中国地区访问速度** | ⭐⭐⭐⭐⭐ 阿里云 CDN速度极快 | ⭐⭐ 国外服务器,可能较慢 | ❌ **已停运** |
| **iOS 支持** | ✅ 支持 | ✅ 支持 | ❌ **已停运** |
| **Android 支持** | ✅ 支持 | ✅ 支持 | ❌ **已停运** |
| **鸿蒙支持** | ✅ 支持 | ❌ 不支持 | ❌ **已停运** |
| **Expo 支持** | ✅ 支持 | ✅ 支持 | ❌ **已停运** |
| **RN 版本支持** | ⭐⭐⭐⭐⭐ 第一时间支持最新版本 | ⭐⭐⭐⭐ 跟随 Expo SDK | ❌ **已停运** |
| **新架构支持** | ✅ 支持 | ✅ 支持 | ❌ **已停运** |
| **Hermes 支持** | ✅ 支持 | ✅ 支持 | ❌ **已停运** |
| **崩溃回滚** | ✅ 自动回滚机制 | ✅ 支持 | ❌ **已停运** |
| **管理界面** | ✅ 命令行工具 + Web 管理界面 | ✅ Expo Dashboard | ❌ **已停运** |
| **CI/CD 集成** | ✅ 支持 | ✅ 支持 | ❌ **已停运** |
| **API 扩展性** | ✅ Meta 信息 + 开放 API | ⚠️ 有限 | ❌ **已停运** |
| **中文文档/支持** | ⭐⭐⭐⭐⭐ 完整中文文档,中文社区支持 | ⭐⭐ 英文为主 | ❌ **已停运** |
| **技术支持** | ✅ 付费专人技术支持 | ⚠️ 社区支持 | ❌ **已停运** |
| **服务器部署** | ✅ 可托管也可付费私有部署 | ✅ Expo 托管EAS Update | ❌ **已停运** |
| **更新策略** | 灵活配置(静默/提示/立即/延迟) | 相对固定 | ❌ **已停运** |
| **流量消耗** | ⭐⭐⭐⭐⭐ 极低(增量更新) | ⭐⭐⭐ 较高(全量更新) | ❌ **已停运** |
| **更新成功率** | ⭐⭐⭐⭐⭐ 极高(国内 CDN 优势) | ⭐⭐⭐ 中等 | ❌ **已停运** |
### 本地开发 ### 本地开发
``` ```

View File

@@ -1,7 +1,10 @@
package cn.reactnative.modules.update; package cn.reactnative.modules.update;
import android.content.Context; import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Build;
import android.util.Log; import android.util.Log;
import com.facebook.react.bridge.Arguments; import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.WritableMap; import com.facebook.react.bridge.WritableMap;
@@ -25,7 +28,6 @@ import java.util.ArrayList;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.Iterator; import java.util.Iterator;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.CRC32;
import java.util.HashMap; import java.util.HashMap;
import okio.BufferedSink; import okio.BufferedSink;
@@ -98,9 +100,6 @@ class DownloadTask extends AsyncTask<DownloadTaskParams, long[], Void> {
while ((bytesRead = source.read(sink.buffer(), DOWNLOAD_CHUNK_SIZE)) != -1) { while ((bytesRead = source.read(sink.buffer(), DOWNLOAD_CHUNK_SIZE)) != -1) {
received += bytesRead; received += bytesRead;
sink.emit(); sink.emit();
if (UpdateContext.DEBUG) {
Log.d("react-native-update", "Progress " + received + "/" + contentLength);
}
int percentage = (int)(received * 100.0 / contentLength + 0.5); int percentage = (int)(received * 100.0 / contentLength + 0.5);
if (percentage > currentPercentage) { if (percentage > currentPercentage) {
@@ -199,10 +198,6 @@ class DownloadTask extends AsyncTask<DownloadTaskParams, long[], Void> {
return fout.toByteArray(); return fout.toByteArray();
} }
private String getCRC32AsDecimal(long crc32Value) {
return String.valueOf(crc32Value & 0xFFFFFFFFL);
}
private void copyFilesWithBlacklist(String current, File from, File to, JSONObject blackList) throws IOException { private void copyFilesWithBlacklist(String current, File from, File to, JSONObject blackList) throws IOException {
File[] files = from.listFiles(); File[] files = from.listFiles();
for (File file : files) { for (File file : files) {
@@ -252,57 +247,209 @@ class DownloadTask extends AsyncTask<DownloadTaskParams, long[], Void> {
} }
} }
private void copyFromResource(HashMap<String, ArrayList<File> > resToCopy) throws IOException { private String findDrawableFallback(String originalToPath, HashMap<String, String> copiesMap, HashMap<String, ZipEntry> availableEntries) {
SafeZipFile zipFile = new SafeZipFile(new File(context.getPackageResourcePath())); // 检查是否是 drawable 路径
Enumeration<? extends ZipEntry> entries = zipFile.entries(); if (!originalToPath.contains("drawable")) {
while (entries.hasMoreElements()) { return null;
ZipEntry ze = entries.nextElement(); }
String fn = ze.getName(); // 提取文件名(路径的最后部分)
ArrayList<File> targets = resToCopy.get(fn); int lastSlash = originalToPath.lastIndexOf('/');
if (targets != null) { if (lastSlash == -1) {
File lastTarget = null; return null;
for (File target: targets) { }
String fileName = originalToPath.substring(lastSlash + 1);
// 定义密度优先级(从高到低)
String[] densities = {"xxxhdpi", "xxhdpi", "xhdpi", "hdpi", "mdpi", "ldpi"};
// 尝试找到相同文件名但不同密度的 key
for (String density : densities) {
// 构建可能的 key 路径(替换密度部分)
String fallbackToPath = originalToPath.replaceFirst("drawable-[^/]+", "drawable-" + density);
// 检查这个 key 是否在 copies 映射中
if (copiesMap.containsKey(fallbackToPath)) {
String fallbackFromPath = copiesMap.get(fallbackToPath);
// 检查对应的 value 路径是否在 APK 中存在
if (availableEntries.containsKey(fallbackFromPath)) {
if (UpdateContext.DEBUG) { if (UpdateContext.DEBUG) {
Log.d("react-native-update", "Copying from resource " + fn + " to " + target); Log.d("react-native-update", "Found fallback for " + originalToPath + ": " + fallbackToPath + " -> " + fallbackFromPath);
}
if (lastTarget != null) {
copyFile(lastTarget, target);
} else {
zipFile.unzipToFile(ze, target);
lastTarget = target;
} }
return fallbackFromPath;
} }
} }
} }
zipFile.close();
return null;
} }
private void copyFromResourceV2(HashMap<String, ArrayList<File>> resToCopy2) throws IOException { private void copyFromResource(HashMap<String, ArrayList<File> > resToCopy, HashMap<String, String> copiesMap) throws IOException {
SafeZipFile zipFile = new SafeZipFile(new File(context.getPackageResourcePath())); if (UpdateContext.DEBUG) {
Enumeration<? extends ZipEntry> entries = zipFile.entries(); Log.d("react-native-update", "copyFromResource called, resToCopy size: " + resToCopy.size());
while (entries.hasMoreElements()) { }
ZipEntry ze = entries.nextElement();
String fn = ze.getName(); // 收集所有 APK 路径(包括基础 APK 和所有 split APK
long zipCrc32 = ze.getCrc(); ArrayList<String> apkPaths = new ArrayList<>();
String crc32Decimal = getCRC32AsDecimal(zipCrc32); apkPaths.add(context.getPackageResourcePath());
ArrayList<File> targets = resToCopy2.get(crc32Decimal);
if (targets != null) { // 获取所有 split APK 路径(用于资源分割的情况)
File lastTarget = null; try {
for (File target: targets) { ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo(
context.getPackageName(), 0);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && appInfo.splitSourceDirs != null) {
for (String splitPath : appInfo.splitSourceDirs) {
apkPaths.add(splitPath);
if (UpdateContext.DEBUG) { if (UpdateContext.DEBUG) {
Log.d("react-native-update", "Copying from resource " + fn + " to " + target); Log.d("react-native-update", "Found split APK: " + splitPath);
}
if (lastTarget != null) {
copyFile(lastTarget, target);
} else {
zipFile.unzipToFile(ze, target);
lastTarget = target;
} }
} }
} }
} catch (PackageManager.NameNotFoundException e) {
if (UpdateContext.DEBUG) {
Log.w("react-native-update", "Failed to get application info: " + e.getMessage());
}
}
// 第一遍:从所有 APK 中收集所有可用的 zip 条目
HashMap<String, ZipEntry> availableEntries = new HashMap<>();
HashMap<String, SafeZipFile> zipFileMap = new HashMap<>(); // 保存每个路径对应的 ZipFile
HashMap<String, SafeZipFile> entryToZipFileMap = new HashMap<>(); // 保存每个条目对应的 ZipFile
for (String apkPath : apkPaths) {
SafeZipFile zipFile = new SafeZipFile(new File(apkPath));
zipFileMap.put(apkPath, zipFile);
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry ze = entries.nextElement();
String entryName = ze.getName();
// 如果条目已存在,保留第一个(基础 APK 优先)
if (!availableEntries.containsKey(entryName)) {
availableEntries.put(entryName, ze);
entryToZipFileMap.put(entryName, zipFile);
}
}
}
// 使用基础 APK 的 ZipFile 作为主要操作对象
SafeZipFile zipFile = zipFileMap.get(context.getPackageResourcePath());
// 处理所有需要复制的文件
HashMap<String, ArrayList<File>> remainingFiles = new HashMap<>(resToCopy);
for (String fromPath : new ArrayList<>(remainingFiles.keySet())) {
if (UpdateContext.DEBUG) {
Log.d("react-native-update", "Processing fromPath: " + fromPath);
}
ArrayList<File> targets = remainingFiles.get(fromPath);
if (targets == null || targets.isEmpty()) {
continue;
}
ZipEntry ze = availableEntries.get(fromPath);
String actualSourcePath = fromPath;
// 如果文件不存在,尝试 fallback
if (ze == null) {
if (UpdateContext.DEBUG) {
Log.d("react-native-update", "File not found in APK: " + fromPath + ", trying fallback");
}
// 找到对应的 to 路径(从 copiesMap 的反向查找)
String toPath = null;
for (String to : copiesMap.keySet()) {
if (copiesMap.get(to).equals(fromPath)) {
toPath = to;
break;
}
}
if (toPath != null) {
if (UpdateContext.DEBUG) {
Log.d("react-native-update", "Found toPath: " + toPath + " for fromPath: " + fromPath);
}
String fallbackFromPath = findDrawableFallback(toPath, copiesMap, availableEntries);
if (fallbackFromPath != null) {
ze = availableEntries.get(fallbackFromPath);
actualSourcePath = fallbackFromPath;
// 确保 fallback 路径也在 entryToZipFileMap 中
if (!entryToZipFileMap.containsKey(fallbackFromPath)) {
// 查找包含该 fallback 路径的 ZipFile
for (String apkPath : apkPaths) {
SafeZipFile testZipFile = zipFileMap.get(apkPath);
if (testZipFile != null) {
try {
ZipEntry testEntry = testZipFile.getEntry(fallbackFromPath);
if (testEntry != null) {
entryToZipFileMap.put(fallbackFromPath, testZipFile);
break;
}
} catch (Exception e) {
// 继续查找
}
}
}
}
if (UpdateContext.DEBUG) {
Log.w("react-native-update", "Using fallback: " + fallbackFromPath + " for " + fromPath);
}
} else {
if (UpdateContext.DEBUG) {
Log.w("react-native-update", "No fallback found for: " + fromPath + " (toPath: " + toPath + ")");
}
}
} else {
if (UpdateContext.DEBUG) {
Log.w("react-native-update", "No toPath found for fromPath: " + fromPath);
}
}
}
if (ze != null) {
File lastTarget = null;
for (File target: targets) {
if (UpdateContext.DEBUG) {
Log.d("react-native-update", "Copying from resource " + actualSourcePath + " to " + target);
}
try {
// 确保目标文件的父目录存在
File parentDir = target.getParentFile();
if (parentDir != null && !parentDir.exists()) {
parentDir.mkdirs();
}
if (lastTarget != null) {
copyFile(lastTarget, target);
} else {
// 从保存的映射中获取包含该条目的 ZipFile
SafeZipFile sourceZipFile = entryToZipFileMap.get(actualSourcePath);
if (sourceZipFile == null) {
sourceZipFile = zipFile; // 回退到基础 APK
}
sourceZipFile.unzipToFile(ze, target);
lastTarget = target;
}
} catch (IOException e) {
if (UpdateContext.DEBUG) {
Log.w("react-native-update", "Failed to copy resource " + actualSourcePath + " to " + target + ": " + e.getMessage());
}
// 继续处理下一个目标
}
}
remainingFiles.remove(fromPath);
}
}
// 处理剩余的文件(如果还有的话)
if (!remainingFiles.isEmpty() && UpdateContext.DEBUG) {
for (String fromPath : remainingFiles.keySet()) {
Log.w("react-native-update", "Resource not found and no fallback available: " + fromPath);
}
}
// 关闭所有 ZipFile
for (SafeZipFile zf : zipFileMap.values()) {
zf.close();
} }
zipFile.close();
} }
private void doPatchFromApk(DownloadTaskParams param) throws IOException, JSONException { private void doPatchFromApk(DownloadTaskParams param) throws IOException, JSONException {
@@ -311,8 +458,7 @@ class DownloadTask extends AsyncTask<DownloadTaskParams, long[], Void> {
removeDirectory(param.unzipDirectory); removeDirectory(param.unzipDirectory);
param.unzipDirectory.mkdirs(); param.unzipDirectory.mkdirs();
HashMap<String, ArrayList<File>> copyList = new HashMap<String, ArrayList<File>>(); HashMap<String, ArrayList<File>> copyList = new HashMap<String, ArrayList<File>>();
HashMap<String, ArrayList<File>> copiesv2List = new HashMap<String, ArrayList<File>>(); HashMap<String, String> copiesMap = new HashMap<String, String>(); // to -> from 映射
Boolean isV2 = false;
boolean foundDiff = false; boolean foundDiff = false;
boolean foundBundlePatch = false; boolean foundBundlePatch = false;
@@ -331,58 +477,32 @@ class DownloadTask extends AsyncTask<DownloadTaskParams, long[], Void> {
JSONObject obj = (JSONObject)new JSONTokener(json).nextValue(); JSONObject obj = (JSONObject)new JSONTokener(json).nextValue();
JSONObject copies = obj.getJSONObject("copies"); JSONObject copies = obj.getJSONObject("copies");
JSONObject copiesv2 = obj.getJSONObject("copiesv2");
Iterator<?> keys = copies.keys(); Iterator<?> keys = copies.keys();
Iterator<?> keysV2 = copiesv2.keys(); while( keys.hasNext() ) {
if(keysV2.hasNext()){ String to = (String)keys.next();
isV2 = true; String from = copies.getString(to);
while( keysV2.hasNext() ) { if (from.isEmpty()) {
String from = (String)keysV2.next(); from = to;
String to = copiesv2.getString(from);
if (from.isEmpty()) {
from = to;
}
ArrayList<File> target = null;
if (!copiesv2List.containsKey(from)) {
target = new ArrayList<File>();
copiesv2List.put(from, target);
} else {
target = copiesv2List.get((from));
}
File toFile = new File(param.unzipDirectory, to);
// Fixing a Zip Path Traversal Vulnerability
// https://support.google.com/faqs/answer/9294009
String canonicalPath = toFile.getCanonicalPath();
if (!canonicalPath.startsWith(param.unzipDirectory.getCanonicalPath() + File.separator)) {
throw new SecurityException("Illegal name: " + to);
}
target.add(toFile);
} }
}else{ // 保存 copies 映射关系to -> from
while( keys.hasNext() ) { copiesMap.put(to, from);
String to = (String)keys.next();
String from = copies.getString(to); ArrayList<File> target = null;
if (from.isEmpty()) { if (!copyList.containsKey(from)) {
from = to; target = new ArrayList<File>();
} copyList.put(from, target);
ArrayList<File> target = null; } else {
if (!copyList.containsKey(from)) { target = copyList.get((from));
target = new ArrayList<File>();
copyList.put(from, target);
} else {
target = copyList.get((from));
}
File toFile = new File(param.unzipDirectory, to);
// Fixing a Zip Path Traversal Vulnerability
// https://support.google.com/faqs/answer/9294009
String canonicalPath = toFile.getCanonicalPath();
if (!canonicalPath.startsWith(param.unzipDirectory.getCanonicalPath() + File.separator)) {
throw new SecurityException("Illegal name: " + to);
}
target.add(toFile);
} }
File toFile = new File(param.unzipDirectory, to);
// Fixing a Zip Path Traversal Vulnerability
// https://support.google.com/faqs/answer/9294009
String canonicalPath = toFile.getCanonicalPath();
if (!canonicalPath.startsWith(param.unzipDirectory.getCanonicalPath() + File.separator)) {
throw new SecurityException("Illegal name: " + to);
}
target.add(toFile);
} }
continue; continue;
} }
@@ -411,12 +531,15 @@ class DownloadTask extends AsyncTask<DownloadTaskParams, long[], Void> {
throw new Error("bundle patch not found"); throw new Error("bundle patch not found");
} }
if(isV2){ if (UpdateContext.DEBUG) {
copyFromResourceV2(copiesv2List); Log.d("react-native-update", "copyList size: " + copyList.size() + ", copiesMap size: " + copiesMap.size());
}else{ for (String from : copyList.keySet()) {
copyFromResource(copyList); Log.d("react-native-update", "copyList entry: " + from + " -> " + copyList.get(from).size() + " targets");
}
} }
copyFromResource(copyList, copiesMap);
if (UpdateContext.DEBUG) { if (UpdateContext.DEBUG) {
Log.d("react-native-update", "Unzip finished"); Log.d("react-native-update", "Unzip finished");
} }

View File

@@ -66,9 +66,8 @@ public class SafeZipFile extends ZipFile {
throw new SecurityException("Illegal name: " + name); throw new SecurityException("Illegal name: " + name);
} }
if (UpdateContext.DEBUG) {
Log.d("RNUpdate", "Unzipping " + name); Log.d("react-native-update", "Unzipping " + name);
}
if (ze.isDirectory()) { if (ze.isDirectory()) {
target.mkdirs(); target.mkdirs();

View File

@@ -19,9 +19,13 @@ public class UpdateContext {
private File rootDir; private File rootDir;
private Executor executor; private Executor executor;
public static boolean DEBUG = false; public static boolean DEBUG = true;
private static ReactInstanceManager mReactInstanceManager; private static ReactInstanceManager mReactInstanceManager;
private static boolean isUsingBundleUrl = false; private static boolean isUsingBundleUrl = false;
// Singleton instance
private static UpdateContext sInstance;
private static final Object sLock = new Object();
public UpdateContext(Context context) { public UpdateContext(Context context) {
this.context = context; this.context = context;
@@ -54,13 +58,17 @@ public class UpdateContext {
boolean buildTimeChanged = !buildTime.equals(storedBuildTime); boolean buildTimeChanged = !buildTime.equals(storedBuildTime);
if (packageVersionChanged || buildTimeChanged) { if (packageVersionChanged || buildTimeChanged) {
// Execute cleanUp before clearing SharedPreferences to avoid race condition
this.cleanUp();
SharedPreferences.Editor editor = sp.edit(); SharedPreferences.Editor editor = sp.edit();
editor.clear(); editor.clear();
editor.putString("packageVersion", packageVersion); editor.putString("packageVersion", packageVersion);
editor.putString("buildTime", buildTime); editor.putString("buildTime", buildTime);
editor.apply(); // Use commit() instead of apply() to ensure synchronous write completion
// This prevents race condition where getBundleUrl() might read null values
this.cleanUp(); // if called before apply() completes
editor.commit();
} }
} }
@@ -227,12 +235,26 @@ public class UpdateContext {
return mReactInstanceManager; return mReactInstanceManager;
} }
/**
* Get singleton instance of UpdateContext
*/
public static UpdateContext getInstance(Context context) {
if (sInstance == null) {
synchronized (sLock) {
if (sInstance == null) {
sInstance = new UpdateContext(context.getApplicationContext());
}
}
}
return sInstance;
}
public static String getBundleUrl(Context context) { public static String getBundleUrl(Context context) {
return new UpdateContext(context.getApplicationContext()).getBundleUrl(); return getInstance(context).getBundleUrl();
} }
public static String getBundleUrl(Context context, String defaultAssetsUrl) { public static String getBundleUrl(Context context, String defaultAssetsUrl) {
return new UpdateContext(context.getApplicationContext()).getBundleUrl(defaultAssetsUrl); return getInstance(context).getBundleUrl(defaultAssetsUrl);
} }
public String getBundleUrl() { public String getBundleUrl() {

View File

@@ -27,7 +27,7 @@ public class UpdateModule extends NativePushySpec {
} }
public UpdateModule(ReactApplicationContext reactContext) { public UpdateModule(ReactApplicationContext reactContext) {
this(reactContext, new UpdateContext(reactContext.getApplicationContext())); this(reactContext, UpdateContext.getInstance(reactContext));
} }
@Override @Override

View File

@@ -40,7 +40,7 @@ public class UpdateModule extends ReactContextBaseJavaModule {
} }
public UpdateModule(ReactApplicationContext reactContext) { public UpdateModule(ReactApplicationContext reactContext) {
this(reactContext, new UpdateContext(reactContext.getApplicationContext())); this(reactContext, UpdateContext.getInstance(reactContext));
} }
@Override @Override

View File

@@ -37,10 +37,10 @@ export class UpdateContext {
this.preferences.putSync('packageVersion', packageVersion); this.preferences.putSync('packageVersion', packageVersion);
this.preferences.flush(); this.preferences.flush();
} else if (storedVersion && packageVersion !== storedVersion) { } else if (storedVersion && packageVersion !== storedVersion) {
this.cleanUp();
this.preferences.clear(); this.preferences.clear();
this.preferences.putSync('packageVersion', packageVersion); this.preferences.putSync('packageVersion', packageVersion);
this.preferences.flush(); this.preferences.flush();
this.cleanUp();
} }
} catch (e) { } catch (e) {
console.error('Failed to init preferences:', e); console.error('Failed to init preferences:', e);

View File

@@ -1,6 +1,6 @@
{ {
"name": "react-native-update", "name": "react-native-update",
"version": "10.35.7", "version": "10.36.0",
"description": "react-native hot update", "description": "react-native hot update",
"main": "src/index", "main": "src/index",
"scripts": { "scripts": {

View File

@@ -1,6 +1,7 @@
import { createContext, useContext } from 'react'; import { createContext, useContext } from 'react';
import { CheckResult, ProgressData } from './type'; import { CheckResult, ProgressData } from './type';
import { Pushy, Cresc } from './client'; import { Pushy, Cresc } from './client';
import i18n from './i18n';
const noop = () => {}; const noop = () => {};
const asyncNoop = () => Promise.resolve(); const asyncNoop = () => Promise.resolve();
@@ -50,7 +51,16 @@ export const UpdateContext = createContext<{
lastError?: Error; lastError?: Error;
}>(defaultContext); }>(defaultContext);
export const useUpdate = () => useContext(UpdateContext); export const useUpdate = __DEV__ ? () => {
const context = useContext(UpdateContext);
// 检查是否在 UpdateProvider 内部使用
if (!context.client) {
throw new Error(i18n.t('error_use_update_outside_provider'));
}
return context;
} : () => useContext(UpdateContext);
/** @deprecated Please use `useUpdate` instead */ /** @deprecated Please use `useUpdate` instead */
export const usePushy = useUpdate; export const usePushy = useUpdate;

View File

@@ -71,4 +71,8 @@ export default {
// Development environment messages // Development environment messages
dev_incremental_update_disabled: dev_incremental_update_disabled:
'Currently in development environment, incremental hot update cannot be executed and restart will not take effect. If you need to test effective full hot update in development environment (but will reconnect to metro after restart), please enable "ignore timestamp" switch and retry.', 'Currently in development environment, incremental hot update cannot be executed and restart will not take effect. If you need to test effective full hot update in development environment (but will reconnect to metro after restart), please enable "ignore timestamp" switch and retry.',
// Context error messages
error_use_update_outside_provider:
'useUpdate must be used within an UpdateProvider. Please wrap your component tree with <UpdateProvider client={...}>.',
}; };

View File

@@ -68,4 +68,8 @@ export default {
// Development environment messages // Development environment messages
dev_incremental_update_disabled: dev_incremental_update_disabled:
'当前是开发环境,无法执行增量式热更新,重启不会生效。如果需要在开发环境中测试可生效的全量热更新(但也会在再次重启后重新连接 metro请打开"忽略时间戳"开关再重试。', '当前是开发环境,无法执行增量式热更新,重启不会生效。如果需要在开发环境中测试可生效的全量热更新(但也会在再次重启后重新连接 metro请打开"忽略时间戳"开关再重试。',
// Context error messages
error_use_update_outside_provider:
'useUpdate 必须在 UpdateProvider 内部使用。请使用 <UpdateProvider client={...}> 包裹您的组件树。',
}; };