mirror of
https://gitcode.com/gh_mirrors/re/react-native-pushy.git
synced 2025-09-16 12:51:44 +08:00
Compare commits
20 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
c26080325d | ||
![]() |
ec0a9e0a3e | ||
![]() |
b34b70ed01 | ||
![]() |
fddae3d79a | ||
![]() |
7fc0d717d5 | ||
![]() |
686209d50e | ||
![]() |
80b0401b02 | ||
![]() |
786d32d92f | ||
![]() |
27327dbe12 | ||
![]() |
96a81cc36d | ||
![]() |
0aa063c584 | ||
![]() |
4d72d49fe4 | ||
![]() |
53d07406ef | ||
![]() |
d0804cfe15 | ||
![]() |
35939286d0 | ||
![]() |
f49ce30cef | ||
![]() |
e6cd25a2d8 | ||
![]() |
0cc96ee59c | ||
![]() |
6dd71a36e8 | ||
![]() |
8db8d2162e |
@@ -68,6 +68,9 @@ export default class App extends Component {
|
||||
});
|
||||
},
|
||||
});
|
||||
if (!hash) {
|
||||
return;
|
||||
}
|
||||
Alert.alert('提示', '下载完毕,是否重启应用?', [
|
||||
{
|
||||
text: '是',
|
||||
|
@@ -152,7 +152,7 @@ public class UpdateContext {
|
||||
}
|
||||
editor.putBoolean("firstTime", true);
|
||||
editor.putBoolean("firstTimeOk", false);
|
||||
editor.putBoolean("rolledBack", false);
|
||||
editor.putString("rolledBackVersion", null);
|
||||
editor.apply();
|
||||
}
|
||||
|
||||
@@ -181,8 +181,8 @@ public class UpdateContext {
|
||||
return sp.getBoolean("firstTime", false);
|
||||
}
|
||||
|
||||
public boolean isRolledBack() {
|
||||
return sp.getBoolean("rolledBack", false);
|
||||
public String rolledBackVersion() {
|
||||
return sp.getString("rolledBackVersion", null);
|
||||
}
|
||||
|
||||
public void markSuccess() {
|
||||
@@ -209,7 +209,7 @@ public class UpdateContext {
|
||||
|
||||
public void clearRollbackMark() {
|
||||
SharedPreferences.Editor editor = sp.edit();
|
||||
editor.putBoolean("rolledBack", false);
|
||||
editor.putString("rolledBackVersion", null);
|
||||
editor.apply();
|
||||
|
||||
this.cleanUp();
|
||||
@@ -265,6 +265,7 @@ public class UpdateContext {
|
||||
|
||||
private String rollBack() {
|
||||
String lastVersion = sp.getString("lastVersion", null);
|
||||
String currentVersion = sp.getString("currentVersion", null);
|
||||
SharedPreferences.Editor editor = sp.edit();
|
||||
if (lastVersion == null) {
|
||||
editor.remove("currentVersion");
|
||||
@@ -273,7 +274,7 @@ public class UpdateContext {
|
||||
}
|
||||
editor.putBoolean("firstTimeOk", true);
|
||||
editor.putBoolean("firstTime", false);
|
||||
editor.putBoolean("rolledBack", true);
|
||||
editor.putString("rolledBackVersion", currentVersion);
|
||||
editor.apply();
|
||||
return lastVersion;
|
||||
}
|
||||
|
@@ -54,9 +54,9 @@ public class UpdateModule extends ReactContextBaseJavaModule {
|
||||
if (isFirstTime) {
|
||||
updateContext.clearFirstTime();
|
||||
}
|
||||
boolean isRolledBack = updateContext.isRolledBack();
|
||||
constants.put("isRolledBack", isRolledBack);
|
||||
if (isRolledBack) {
|
||||
String rolledBackVersion = updateContext.rolledBackVersion();
|
||||
constants.put("rolledBackVersion", rolledBackVersion);
|
||||
if (rolledBackVersion != null) {
|
||||
updateContext.clearRollbackMark();
|
||||
}
|
||||
constants.put("blockUpdate", updateContext.getBlockUpdate());
|
||||
@@ -134,9 +134,7 @@ public class UpdateModule extends ReactContextBaseJavaModule {
|
||||
private void downloadPatchFromPackage(ReadableMap options, final Promise promise) {
|
||||
String url = options.getString("updateUrl");
|
||||
String hash = options.getString("hash");
|
||||
if (hash == null) {
|
||||
hash = options.getString("hashName");
|
||||
}
|
||||
|
||||
updateContext.downloadPatchFromApk(url, hash, new UpdateContext.DownloadFileListener() {
|
||||
@Override
|
||||
public void onDownloadCompleted(DownloadTaskParams params) {
|
||||
@@ -154,13 +152,9 @@ public class UpdateModule extends ReactContextBaseJavaModule {
|
||||
private void downloadPatchFromPpk(ReadableMap options, final Promise promise) {
|
||||
String url = options.getString("updateUrl");
|
||||
String hash = options.getString("hash");
|
||||
if (hash == null) {
|
||||
hash = options.getString("hashName");
|
||||
}
|
||||
|
||||
String originHash = options.getString("originHash");
|
||||
if (originHash == null) {
|
||||
originHash = options.getString(("originHashName"));
|
||||
}
|
||||
|
||||
updateContext.downloadPatchFromPpk(url, hash, originHash, new UpdateContext.DownloadFileListener() {
|
||||
@Override
|
||||
public void onDownloadCompleted(DownloadTaskParams params) {
|
||||
@@ -176,8 +170,7 @@ public class UpdateModule extends ReactContextBaseJavaModule {
|
||||
|
||||
@ReactMethod
|
||||
public void reloadUpdate(ReadableMap options) {
|
||||
final String hash = options.getString("hash") == null ?
|
||||
options.getString("hashName") : options.getString("hash");
|
||||
final String hash = options.getString("hash");
|
||||
|
||||
UiThreadUtil.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
@@ -218,8 +211,7 @@ public class UpdateModule extends ReactContextBaseJavaModule {
|
||||
|
||||
@ReactMethod
|
||||
public void setNeedUpdate(ReadableMap options) {
|
||||
final String hash = options.getString("hash") == null ?
|
||||
options.getString("hashName") : options.getString("hash");
|
||||
final String hash = options.getString("hash");
|
||||
|
||||
UiThreadUtil.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
|
@@ -120,6 +120,7 @@ RCT_EXPORT_MODULE(RCTPushy);
|
||||
|
||||
NSDictionary *pushyInfo = [defaults dictionaryForKey:keyPushyInfo];
|
||||
NSString *lastVersion = pushyInfo[paramLastVersion];
|
||||
NSString *curVersion = pushyInfo[paramCurrentVersion];
|
||||
NSString *curPackageVersion = [RCTPushy packageVersion];
|
||||
if (lastVersion.length) {
|
||||
// roll back to last version
|
||||
@@ -133,7 +134,7 @@ RCT_EXPORT_MODULE(RCTPushy);
|
||||
// roll back to bundle
|
||||
[defaults setObject:nil forKey:keyPushyInfo];
|
||||
}
|
||||
[defaults setObject:@(YES) forKey:keyRolledBackMarked];
|
||||
[defaults setObject:curVersion forKey:keyRolledBackMarked];
|
||||
[defaults synchronize];
|
||||
return lastVersion;
|
||||
}
|
||||
@@ -151,7 +152,7 @@ RCT_EXPORT_MODULE(RCTPushy);
|
||||
ret[@"downloadRootDir"] = [RCTPushy downloadDir];
|
||||
ret[@"packageVersion"] = [RCTPushy packageVersion];
|
||||
ret[@"buildTime"] = [RCTPushy buildTime];
|
||||
ret[@"isRolledBack"] = [defaults objectForKey:keyRolledBackMarked];
|
||||
ret[@"rolledBackVersion"] = [defaults objectForKey:keyRolledBackMarked];
|
||||
ret[@"isFirstTime"] = [defaults objectForKey:keyFirstLoadMarked];
|
||||
ret[@"blockUpdate"] = [defaults objectForKey:keyBlockUpdate];
|
||||
ret[@"uuid"] = [defaults objectForKey:keyUuid];
|
||||
@@ -159,12 +160,12 @@ RCT_EXPORT_MODULE(RCTPushy);
|
||||
ret[@"currentVersion"] = [pushyInfo objectForKey:paramCurrentVersion];
|
||||
|
||||
// clear isFirstTimemarked
|
||||
if ([[defaults objectForKey:keyFirstLoadMarked] boolValue]) {
|
||||
if (ret[@"isFirstTime"]) {
|
||||
[defaults setObject:nil forKey:keyFirstLoadMarked];
|
||||
}
|
||||
|
||||
// clear rolledbackmark
|
||||
if ([[defaults objectForKey:keyRolledBackMarked] boolValue]) {
|
||||
if (ret[@"rolledBackVersion"] != nil) {
|
||||
[defaults setObject:nil forKey:keyRolledBackMarked];
|
||||
[self clearInvalidFiles];
|
||||
}
|
||||
@@ -268,9 +269,7 @@ RCT_EXPORT_METHOD(downloadPatchFromPpk:(NSDictionary *)options
|
||||
RCT_EXPORT_METHOD(setNeedUpdate:(NSDictionary *)options)
|
||||
{
|
||||
NSString *hash = options[@"hash"];
|
||||
if (hash.length <= 0) {
|
||||
hash = options[@"hashName"];
|
||||
}
|
||||
|
||||
if (hash.length) {
|
||||
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
||||
NSString *lastVersion = nil;
|
||||
@@ -294,9 +293,7 @@ RCT_EXPORT_METHOD(setNeedUpdate:(NSDictionary *)options)
|
||||
RCT_EXPORT_METHOD(reloadUpdate:(NSDictionary *)options)
|
||||
{
|
||||
NSString *hash = options[@"hash"];
|
||||
if (hash.length <= 0) {
|
||||
hash = options[@"hashName"];
|
||||
}
|
||||
|
||||
if (hash.length) {
|
||||
[self setNeedUpdate:options];
|
||||
|
||||
@@ -359,9 +356,7 @@ RCT_EXPORT_METHOD(markSuccess)
|
||||
{
|
||||
NSString *updateUrl = [RCTConvert NSString:options[@"updateUrl"]];
|
||||
NSString *hash = [RCTConvert NSString:options[@"hash"]];
|
||||
if (hash.length <= 0) {
|
||||
hash = [RCTConvert NSString:options[@"hashName"]];;
|
||||
}
|
||||
|
||||
if (updateUrl.length <= 0 || hash.length <= 0) {
|
||||
callback([self errorWithMessage:ERROR_OPTIONS]);
|
||||
return;
|
||||
|
@@ -76,6 +76,10 @@ export function getCheckUrl(APPKEY, endpoint = currentEndpoint) {
|
||||
return `${endpoint}/checkUpdate/${APPKEY}`;
|
||||
}
|
||||
|
||||
export function getReportUrl(endpoint = currentEndpoint) {
|
||||
return `${endpoint}/report`;
|
||||
}
|
||||
|
||||
export function setCustomEndpoints({ main, backups, backupQueryUrl }) {
|
||||
currentEndpoint = main;
|
||||
backupEndpointsQueryUrl = null;
|
||||
|
96
lib/main.js
96
lib/main.js
@@ -2,6 +2,7 @@ import {
|
||||
tryBackupEndpoints,
|
||||
getCheckUrl,
|
||||
setCustomEndpoints,
|
||||
getReportUrl,
|
||||
} from './endpoint';
|
||||
import {
|
||||
NativeEventEmitter,
|
||||
@@ -25,7 +26,9 @@ export const downloadRootDir = Pushy.downloadRootDir;
|
||||
export const packageVersion = Pushy.packageVersion;
|
||||
export const currentVersion = Pushy.currentVersion;
|
||||
export const isFirstTime = Pushy.isFirstTime;
|
||||
export const isRolledBack = Pushy.isRolledBack;
|
||||
const rolledBackVersion = Pushy.rolledBackVersion;
|
||||
export const isRolledBack = typeof rolledBackVersion === 'string';
|
||||
|
||||
export const buildTime = Pushy.buildTime;
|
||||
let blockUpdate = Pushy.blockUpdate;
|
||||
let uuid = Pushy.uuid;
|
||||
@@ -45,7 +48,7 @@ async function getLocalHashInfo(hash) {
|
||||
}
|
||||
|
||||
export async function getCurrentVersionInfo() {
|
||||
return currentVersion ? await getLocalHashInfo(currentVersion) || {} : {};
|
||||
return currentVersion ? (await getLocalHashInfo(currentVersion)) || {} : {};
|
||||
}
|
||||
|
||||
const eventEmitter = new NativeEventEmitter(Pushy);
|
||||
@@ -59,30 +62,29 @@ function logger(text) {
|
||||
console.log(`Pushy: ${text}`);
|
||||
}
|
||||
|
||||
function report(hash, type) {
|
||||
logger(type);
|
||||
fetch(getReportUrl(), {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Accept: 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
hash,
|
||||
type,
|
||||
cInfo,
|
||||
packageVersion,
|
||||
buildTime,
|
||||
}),
|
||||
}).catch((_e) => {});
|
||||
}
|
||||
|
||||
logger('uuid: ' + uuid);
|
||||
|
||||
/*
|
||||
Return json:
|
||||
Package expired:
|
||||
{
|
||||
expired: true,
|
||||
downloadUrl: 'http://appstore/downloadUrl',
|
||||
if (isRolledBack) {
|
||||
report(rolledBackVersion, 'rollback');
|
||||
}
|
||||
Package is up to date:
|
||||
{
|
||||
upToDate: true,
|
||||
}
|
||||
There is available update:
|
||||
{
|
||||
update: true,
|
||||
name: '1.0.3-rc',
|
||||
hash: 'hash',
|
||||
description: '添加聊天功能\n修复商城页面BUG',
|
||||
metaInfo: '{"silent":true}',
|
||||
pdiffUrl: 'http://update-packages.reactnative.cn/hash',
|
||||
diffUrl: 'http://update-packages.reactnative.cn/hash',
|
||||
}
|
||||
*/
|
||||
|
||||
export const cInfo = {
|
||||
pushy: require('../package.json').version,
|
||||
@@ -173,6 +175,10 @@ export async function downloadUpdate(options, eventListeners) {
|
||||
if (!options.update) {
|
||||
return;
|
||||
}
|
||||
if (rolledBackVersion === options.hash) {
|
||||
logger(`rolledback hash ${rolledBackVersion}, ignored`);
|
||||
return;
|
||||
}
|
||||
if (downloadedHash === options.hash) {
|
||||
logger(`duplicated downloaded hash ${downloadedHash}, ignored`);
|
||||
return;
|
||||
@@ -212,19 +218,30 @@ export async function downloadUpdate(options, eventListeners) {
|
||||
originHash: currentVersion,
|
||||
});
|
||||
} catch (e) {
|
||||
logger(e.message);
|
||||
logger('diff error, try pdiff');
|
||||
logger(`diff error: ${e.message}, try pdiff`);
|
||||
try {
|
||||
await Pushy.downloadPatchFromPackage({
|
||||
updateUrl: options.pdiffUrl,
|
||||
hash: options.hash,
|
||||
});
|
||||
} catch (e) {
|
||||
progressHandler && progressHandler.remove();
|
||||
report(options.hash, 'error');
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
} else if (options.pdiffUrl) {
|
||||
logger('downloading pdiff');
|
||||
try {
|
||||
await Pushy.downloadPatchFromPackage({
|
||||
updateUrl: options.pdiffUrl,
|
||||
hash: options.hash,
|
||||
});
|
||||
} catch (e) {
|
||||
progressHandler && progressHandler.remove();
|
||||
report(options.hash, 'error');
|
||||
throw e;
|
||||
}
|
||||
} else if (options.pdiffUrl) {
|
||||
logger('downloading pdiff');
|
||||
await Pushy.downloadPatchFromPackage({
|
||||
updateUrl: options.pdiffUrl,
|
||||
hash: options.hash,
|
||||
});
|
||||
}
|
||||
setLocalHashInfo(options.hash, {
|
||||
name: options.name,
|
||||
@@ -251,20 +268,23 @@ function assertHash(hash) {
|
||||
return;
|
||||
}
|
||||
readyHash = hash;
|
||||
return true;
|
||||
}
|
||||
|
||||
export function switchVersion(hash) {
|
||||
assertRelease();
|
||||
assertHash(hash);
|
||||
logger('switchVersion: ' + hash);
|
||||
Pushy.reloadUpdate({ hash });
|
||||
if (assertHash(hash)) {
|
||||
logger('switchVersion: ' + hash);
|
||||
Pushy.reloadUpdate({ hash });
|
||||
}
|
||||
}
|
||||
|
||||
export function switchVersionLater(hash) {
|
||||
assertRelease();
|
||||
assertHash(hash);
|
||||
logger('switchVersionLater: ' + hash);
|
||||
Pushy.setNeedUpdate({ hash });
|
||||
if (assertHash(hash)) {
|
||||
logger('switchVersionLater: ' + hash);
|
||||
Pushy.setNeedUpdate({ hash });
|
||||
}
|
||||
}
|
||||
|
||||
let marked = false;
|
||||
@@ -275,8 +295,8 @@ export function markSuccess() {
|
||||
return;
|
||||
}
|
||||
marked = true;
|
||||
logger('markSuccess');
|
||||
Pushy.markSuccess();
|
||||
report(currentVersion, 'success');
|
||||
}
|
||||
|
||||
export async function downloadAndInstallApk({ url, onDownloadProgress }) {
|
||||
|
@@ -36,12 +36,15 @@ export function simpleUpdate(WrappedComponent) {
|
||||
this.checkUpdate();
|
||||
}
|
||||
componentWillUnmount() {
|
||||
this.stateListener.remove();
|
||||
this.stateListener && this.stateListener.remove();
|
||||
}
|
||||
doUpdate = async (info) => {
|
||||
try {
|
||||
const hash = await downloadUpdate(info);
|
||||
this.stateListener.remove();
|
||||
if (!hash) {
|
||||
return;
|
||||
}
|
||||
this.stateListener && this.stateListener.remove();
|
||||
Alert.alert('提示', '下载完毕,是否立即更新?', [
|
||||
{
|
||||
text: '以后再说',
|
||||
@@ -91,7 +94,7 @@ export function simpleUpdate(WrappedComponent) {
|
||||
},
|
||||
},
|
||||
]);
|
||||
} else {
|
||||
} else if (info.update) {
|
||||
Alert.alert(
|
||||
'提示',
|
||||
'检查到新的版本' + info.name + ',是否下载?\n' + info.description,
|
||||
|
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "react-native-update",
|
||||
"version": "7.0.0",
|
||||
"version": "7.3.8",
|
||||
"description": "react-native hot update",
|
||||
"main": "lib/index.js",
|
||||
"scripts": {
|
||||
@@ -28,6 +28,6 @@
|
||||
},
|
||||
"homepage": "https://github.com/reactnativecn/react-native-pushy#readme",
|
||||
"dependencies": {
|
||||
"nanoid": "^3.1.28"
|
||||
"nanoid": "^3.1.30"
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user