mirror of
https://gitcode.com/gh_mirrors/re/react-native-pushy.git
synced 2025-09-17 18:36:10 +08:00
Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
ebb5defb10 | ||
![]() |
a509ff8e30 | ||
![]() |
59fcba15ee | ||
![]() |
c6f9bb20a1 | ||
![]() |
9e6c7ea769 | ||
![]() |
f461c8ddd2 |
7
.gitignore
vendored
7
.gitignore
vendored
@@ -1,7 +1,6 @@
|
|||||||
/.idea
|
.vscode
|
||||||
/node_modules
|
android/build
|
||||||
/android/build
|
android/obj
|
||||||
/android/obj
|
|
||||||
*.iml
|
*.iml
|
||||||
|
|
||||||
# OSX
|
# OSX
|
||||||
|
19
.npmignore
19
.npmignore
@@ -1,11 +1,11 @@
|
|||||||
/.idea
|
.babelrc
|
||||||
/.babelrc
|
.npmignore
|
||||||
/.npmignore
|
.eslintrc
|
||||||
/.eslintrc
|
.nvmrc
|
||||||
/.nvmrc
|
.travis.yml
|
||||||
/.travis.yml
|
Example
|
||||||
/Example
|
android/build
|
||||||
/android/build
|
.vscode
|
||||||
|
|
||||||
# OSX
|
# OSX
|
||||||
#
|
#
|
||||||
@@ -45,4 +45,5 @@ Example
|
|||||||
yarn.lock
|
yarn.lock
|
||||||
android/jni
|
android/jni
|
||||||
|
|
||||||
domains.json
|
domains.json
|
||||||
|
endpoints.json
|
@@ -52,36 +52,34 @@ export default class App extends Component {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
doUpdate = async info => {
|
doUpdate = async info => {
|
||||||
const hash = await downloadUpdate(info);
|
try {
|
||||||
Alert.alert('提示', '下载完毕,是否重启应用?', [
|
const hash = await downloadUpdate(info);
|
||||||
{
|
Alert.alert('提示', '下载完毕,是否重启应用?', [
|
||||||
text: '是',
|
{
|
||||||
onPress: () => {
|
text: '是',
|
||||||
switchVersion(hash);
|
onPress: () => {
|
||||||
|
switchVersion(hash);
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
{text: '否'},
|
||||||
{text: '否'},
|
{
|
||||||
{
|
text: '下次启动时',
|
||||||
text: '下次启动时',
|
onPress: () => {
|
||||||
onPress: () => {
|
switchVersionLater(hash);
|
||||||
switchVersionLater(hash);
|
},
|
||||||
},
|
},
|
||||||
},
|
]);
|
||||||
]);
|
} catch (err) {
|
||||||
|
Alert.alert('更新失败', err.message);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
checkUpdate = async () => {
|
checkUpdate = async () => {
|
||||||
return await this.doUpdate({
|
|
||||||
update: true,
|
|
||||||
pdiffUrl: 'http://localhost:8888/1.pdiff',
|
|
||||||
hash: 'test',
|
|
||||||
});
|
|
||||||
|
|
||||||
let info;
|
let info;
|
||||||
try {
|
try {
|
||||||
info = await checkUpdate(appKey);
|
info = await checkUpdate(appKey);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.warn(err);
|
Alert.alert('更新检查失败', err.message);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (info.expired) {
|
if (info.expired) {
|
||||||
@@ -115,7 +113,7 @@ export default class App extends Component {
|
|||||||
render() {
|
render() {
|
||||||
return (
|
return (
|
||||||
<View style={styles.container}>
|
<View style={styles.container}>
|
||||||
<Text style={styles.welcome}>443欢迎使用热更新服务</Text>
|
<Text style={styles.welcome}>欢迎使用热更新服务</Text>
|
||||||
<Image
|
<Image
|
||||||
resizeMode={'contain'}
|
resizeMode={'contain'}
|
||||||
source={require('./assets/shezhi.png')}
|
source={require('./assets/shezhi.png')}
|
||||||
|
@@ -7,6 +7,8 @@ import android.content.pm.PackageManager;
|
|||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import com.facebook.react.ReactInstanceManager;
|
import com.facebook.react.ReactInstanceManager;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
@@ -68,6 +70,17 @@ public class UpdateContext {
|
|||||||
return context.getString(R.string.pushy_build_time);
|
return context.getString(R.string.pushy_build_time);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getUuid() {
|
||||||
|
return sp.getString("uuid", null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map getBlockUpdate() {
|
||||||
|
return new HashMap<String, Object>() {{
|
||||||
|
put("until", sp.getInt("blockUntil", 0));
|
||||||
|
put("reason", sp.getString("blockReason", null));
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
public boolean getIsUsingBundleUrl() {
|
public boolean getIsUsingBundleUrl() {
|
||||||
return isUsingBundleUrl;
|
return isUsingBundleUrl;
|
||||||
}
|
}
|
||||||
@@ -130,6 +143,19 @@ public class UpdateContext {
|
|||||||
editor.apply();
|
editor.apply();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setUuid(String uuid) {
|
||||||
|
SharedPreferences.Editor editor = sp.edit();
|
||||||
|
editor.putString("uuid", uuid);
|
||||||
|
editor.apply();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBlockUpdate(int until, String reason) {
|
||||||
|
SharedPreferences.Editor editor = sp.edit();
|
||||||
|
editor.putInt("blockUntil", until);
|
||||||
|
editor.putString("blockReason", reason);
|
||||||
|
editor.apply();
|
||||||
|
}
|
||||||
|
|
||||||
public String getCurrentVersion() {
|
public String getCurrentVersion() {
|
||||||
return sp.getString("currentVersion", null);
|
return sp.getString("currentVersion", null);
|
||||||
}
|
}
|
||||||
|
@@ -57,6 +57,8 @@ public class UpdateModule extends ReactContextBaseJavaModule{
|
|||||||
if (isRolledBack) {
|
if (isRolledBack) {
|
||||||
updateContext.clearRollbackMark();
|
updateContext.clearRollbackMark();
|
||||||
}
|
}
|
||||||
|
constants.put("blockUpdate", updateContext.getBlockUpdate());
|
||||||
|
constants.put("uuid", updateContext.getUuid());
|
||||||
return constants;
|
return constants;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -184,6 +186,28 @@ public class UpdateModule extends ReactContextBaseJavaModule{
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ReactMethod
|
||||||
|
public void setBlockUpdate(ReadableMap options) {
|
||||||
|
final int until = options.getInt("until");
|
||||||
|
final String reason = options.getString("reason");
|
||||||
|
UiThreadUtil.runOnUiThread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
updateContext.setBlockUpdate(until, reason);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@ReactMethod
|
||||||
|
public void setUuid(final String uuid) {
|
||||||
|
UiThreadUtil.runOnUiThread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
updateContext.setUuid(uuid);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/* 发送事件*/
|
/* 发送事件*/
|
||||||
public static void sendEvent(String eventName, WritableMap params) {
|
public static void sendEvent(String eventName, WritableMap params) {
|
||||||
((ReactContext) mContext).getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(eventName,
|
((ReactContext) mContext).getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(eventName,
|
||||||
|
@@ -27,6 +27,8 @@ static NSString *const paramLastVersion = @"lastVersion";
|
|||||||
static NSString *const paramCurrentVersion = @"currentVersion";
|
static NSString *const paramCurrentVersion = @"currentVersion";
|
||||||
static NSString *const paramIsFirstTime = @"isFirstTime";
|
static NSString *const paramIsFirstTime = @"isFirstTime";
|
||||||
static NSString *const paramIsFirstLoadOk = @"isFirstLoadOK";
|
static NSString *const paramIsFirstLoadOk = @"isFirstLoadOK";
|
||||||
|
static NSString *const keyBlockUpdate = @"REACTNATIVECN_PUSHY_BLOCKUPDATE";
|
||||||
|
static NSString *const keyUuid = @"REACTNATIVECN_PUSHY_UUID";
|
||||||
static NSString *const keyFirstLoadMarked = @"REACTNATIVECN_PUSHY_FIRSTLOADMARKED_KEY";
|
static NSString *const keyFirstLoadMarked = @"REACTNATIVECN_PUSHY_FIRSTLOADMARKED_KEY";
|
||||||
static NSString *const keyRolledBackMarked = @"REACTNATIVECN_PUSHY_ROLLEDBACKMARKED_KEY";
|
static NSString *const keyRolledBackMarked = @"REACTNATIVECN_PUSHY_ROLLEDBACKMARKED_KEY";
|
||||||
static NSString *const KeyPackageUpdatedMarked = @"REACTNATIVECN_PUSHY_ISPACKAGEUPDATEDMARKED_KEY";
|
static NSString *const KeyPackageUpdatedMarked = @"REACTNATIVECN_PUSHY_ISPACKAGEUPDATEDMARKED_KEY";
|
||||||
@@ -155,6 +157,8 @@ RCT_EXPORT_MODULE(RCTPushy);
|
|||||||
ret[@"buildTime"] = [RCTPushy buildTime];
|
ret[@"buildTime"] = [RCTPushy buildTime];
|
||||||
ret[@"isRolledBack"] = [defaults objectForKey:keyRolledBackMarked];
|
ret[@"isRolledBack"] = [defaults objectForKey:keyRolledBackMarked];
|
||||||
ret[@"isFirstTime"] = [defaults objectForKey:keyFirstLoadMarked];
|
ret[@"isFirstTime"] = [defaults objectForKey:keyFirstLoadMarked];
|
||||||
|
ret[@"blockUpdate"] = [defaults objectForKey:keyBlockUpdate];
|
||||||
|
ret[@"uuid"] = [defaults objectForKey:keyUuid];
|
||||||
NSDictionary *pushyInfo = [defaults dictionaryForKey:keyPushyInfo];
|
NSDictionary *pushyInfo = [defaults dictionaryForKey:keyPushyInfo];
|
||||||
ret[@"currentVersion"] = [pushyInfo objectForKey:paramCurrentVersion];
|
ret[@"currentVersion"] = [pushyInfo objectForKey:paramCurrentVersion];
|
||||||
|
|
||||||
@@ -188,6 +192,23 @@ RCT_EXPORT_MODULE(RCTPushy);
|
|||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
RCT_EXPORT_METHOD(setBlockUpdate:(NSDictionary *)options)
|
||||||
|
{
|
||||||
|
// NSMutableDictionary *blockUpdateInfo = [NSMutableDictionary new];
|
||||||
|
// blockUpdateInfo[@"reason"] = options[@"reason"];
|
||||||
|
// blockUpdateInfo[@"until"] = options[@"until"];
|
||||||
|
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
||||||
|
[defaults setObject:options forKey:keyBlockUpdate];
|
||||||
|
[defaults synchronize];
|
||||||
|
}
|
||||||
|
|
||||||
|
RCT_EXPORT_METHOD(setUuid:(NSString *)uuid)
|
||||||
|
{
|
||||||
|
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
||||||
|
[defaults setObject:uuid forKey:keyUuid];
|
||||||
|
[defaults synchronize];
|
||||||
|
}
|
||||||
|
|
||||||
RCT_EXPORT_METHOD(downloadUpdate:(NSDictionary *)options
|
RCT_EXPORT_METHOD(downloadUpdate:(NSDictionary *)options
|
||||||
resolver:(RCTPromiseResolveBlock)resolve
|
resolver:(RCTPromiseResolveBlock)resolve
|
||||||
rejecter:(RCTPromiseRejectBlock)reject)
|
rejecter:(RCTPromiseRejectBlock)reject)
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
let currentEndpoint = 'https://update.reactnative.cn/api';
|
let currentEndpoint = 'https://update.react-native.cn/api';
|
||||||
|
|
||||||
function ping(url, rejectImmediate) {
|
function ping(url, rejectImmediate) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
51
lib/index.js
51
lib/index.js
@@ -5,6 +5,11 @@ import {
|
|||||||
} from './endpoint';
|
} from './endpoint';
|
||||||
import { NativeAppEventEmitter, NativeModules, Platform } from 'react-native';
|
import { NativeAppEventEmitter, NativeModules, Platform } from 'react-native';
|
||||||
export { setCustomEndpoints };
|
export { setCustomEndpoints };
|
||||||
|
const {
|
||||||
|
version: v,
|
||||||
|
} = require('react-native/Libraries/Core/ReactNativeVersion');
|
||||||
|
const RNVersion = `${v.major}.${v.minor}.${v.patch}`;
|
||||||
|
const uuidv4 = require('uuid/v4');
|
||||||
|
|
||||||
let Pushy = NativeModules.Pushy;
|
let Pushy = NativeModules.Pushy;
|
||||||
|
|
||||||
@@ -18,6 +23,8 @@ export const currentVersion = Pushy.currentVersion;
|
|||||||
export const isFirstTime = Pushy.isFirstTime;
|
export const isFirstTime = Pushy.isFirstTime;
|
||||||
export const isRolledBack = Pushy.isRolledBack;
|
export const isRolledBack = Pushy.isRolledBack;
|
||||||
export const buildTime = Pushy.buildTime;
|
export const buildTime = Pushy.buildTime;
|
||||||
|
let blockUpdate = Pushy.blockUpdate;
|
||||||
|
let uuid = Pushy.uuid;
|
||||||
|
|
||||||
if (Platform.OS === 'android' && !Pushy.isUsingBundleUrl) {
|
if (Platform.OS === 'android' && !Pushy.isUsingBundleUrl) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
@@ -25,9 +32,16 @@ if (Platform.OS === 'android' && !Pushy.isUsingBundleUrl) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!uuid) {
|
||||||
|
uuid = uuidv4();
|
||||||
|
Pushy.setUuid(uuid);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('Pushy uuid: ' + uuid);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Return json:
|
Return json:
|
||||||
Package was expired:
|
Package expired:
|
||||||
{
|
{
|
||||||
expired: true,
|
expired: true,
|
||||||
downloadUrl: 'http://appstore/downloadUrl',
|
downloadUrl: 'http://appstore/downloadUrl',
|
||||||
@@ -56,6 +70,13 @@ function assertRelease() {
|
|||||||
|
|
||||||
export async function checkUpdate(APPKEY, isRetry) {
|
export async function checkUpdate(APPKEY, isRetry) {
|
||||||
assertRelease();
|
assertRelease();
|
||||||
|
if (blockUpdate && blockUpdate.until > Date.now() / 1000) {
|
||||||
|
throw new Error(
|
||||||
|
`热更新已暂停,原因:${blockUpdate.reason}。请在"${new Date(
|
||||||
|
blockUpdate.until * 1000,
|
||||||
|
).toLocaleString()}"之后重试。`,
|
||||||
|
);
|
||||||
|
}
|
||||||
let resp;
|
let resp;
|
||||||
try {
|
try {
|
||||||
resp = await fetch(getCheckUrl(APPKEY), {
|
resp = await fetch(getCheckUrl(APPKEY), {
|
||||||
@@ -68,6 +89,12 @@ export async function checkUpdate(APPKEY, isRetry) {
|
|||||||
packageVersion,
|
packageVersion,
|
||||||
hash: currentVersion,
|
hash: currentVersion,
|
||||||
buildTime,
|
buildTime,
|
||||||
|
cInfo: {
|
||||||
|
pushy: require('../package.json').version,
|
||||||
|
rn: RNVersion,
|
||||||
|
os: Platform.OS + ' ' + Platform.Version,
|
||||||
|
uuid,
|
||||||
|
},
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@@ -77,12 +104,29 @@ export async function checkUpdate(APPKEY, isRetry) {
|
|||||||
await tryBackupEndpoints(APPKEY);
|
await tryBackupEndpoints(APPKEY);
|
||||||
return checkUpdate(APPKEY, true);
|
return checkUpdate(APPKEY, true);
|
||||||
}
|
}
|
||||||
|
const result = await resp.json();
|
||||||
|
checkOperation(result.op);
|
||||||
|
|
||||||
if (resp.status !== 200) {
|
if (resp.status !== 200) {
|
||||||
throw new Error((await resp.json()).message);
|
throw new Error(result.message);
|
||||||
}
|
}
|
||||||
|
|
||||||
return resp.json();
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
function checkOperation(op) {
|
||||||
|
if (!Array.isArray(op)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
op.forEach((action) => {
|
||||||
|
if (action.type === 'block') {
|
||||||
|
blockUpdate = {
|
||||||
|
reason: action.reason,
|
||||||
|
until: (Date.now() + action.duration) / 1000,
|
||||||
|
};
|
||||||
|
Pushy.setBlockUpdate(blockUpdate);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function downloadUpdate(options) {
|
export async function downloadUpdate(options) {
|
||||||
@@ -90,7 +134,6 @@ export async function downloadUpdate(options) {
|
|||||||
if (!options.update) {
|
if (!options.update) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.diffUrl) {
|
if (options.diffUrl) {
|
||||||
await Pushy.downloadPatchFromPpk({
|
await Pushy.downloadPatchFromPpk({
|
||||||
updateUrl: options.diffUrl,
|
updateUrl: options.diffUrl,
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "react-native-update",
|
"name": "react-native-update",
|
||||||
"version": "5.7.0",
|
"version": "5.7.2",
|
||||||
"description": "react-native hot update",
|
"description": "react-native hot update",
|
||||||
"main": "lib/index.js",
|
"main": "lib/index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
@@ -26,5 +26,7 @@
|
|||||||
"react-native": ">=0.27.0"
|
"react-native": ">=0.27.0"
|
||||||
},
|
},
|
||||||
"homepage": "https://github.com/reactnativecn/react-native-pushy#readme",
|
"homepage": "https://github.com/reactnativecn/react-native-pushy#readme",
|
||||||
"dependencies": {}
|
"dependencies": {
|
||||||
|
"uuid": "3"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user