mirror of
https://gitcode.com/gh_mirrors/re/react-native-pushy.git
synced 2025-09-17 17:56:11 +08:00
Compare commits
7 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
23d1fcd4d1 | ||
![]() |
e3a748065a | ||
![]() |
effd7e129d | ||
![]() |
9a00cf7483 | ||
![]() |
d854082495 | ||
![]() |
3073bd99db | ||
![]() |
4436654769 |
@@ -12,7 +12,7 @@
|
||||
"react-native-paper": "^5.13.1",
|
||||
"react-native-safe-area-context": "^5.2.0",
|
||||
"react-native-svg": "^15.11.1",
|
||||
"react-native-update": "^10.23.0",
|
||||
"react-native-update": "^10.25.0",
|
||||
"react-native-vector-icons": "^10.2.0",
|
||||
},
|
||||
"devDependencies": {
|
||||
@@ -1434,7 +1434,7 @@
|
||||
|
||||
"react-native-svg": ["react-native-svg@15.11.1", "", { "dependencies": { "css-select": "^5.1.0", "css-tree": "^1.1.3", "warn-once": "0.1.1" }, "peerDependencies": { "react": "*", "react-native": "*" } }, "sha512-Qmwx/yJKt+AHUr4zjxx/Q69qwKtRfr1+uIfFMQoq3WFRhqU76aL9db1DyvPiY632DAsVGba1pHf92OZPkpjrdQ=="],
|
||||
|
||||
"react-native-update": ["react-native-update@10.23.0", "", { "dependencies": { "nanoid": "^3.3.3", "react-native-url-polyfill": "^2.0.0" }, "peerDependencies": { "react": ">=16.8.0", "react-native": ">=0.59.0" } }, "sha512-gt6kL5Fd8iG46YYwvcuiD6Yod7DSzNZ/Qo5YuiAXrdO/h+3SCJsfDGVmSpanShwAepvvuAUELIJZIRpSrJdIvA=="],
|
||||
"react-native-update": ["react-native-update@10.25.0", "", { "dependencies": { "nanoid": "^3.3.3", "react-native-url-polyfill": "^2.0.0" }, "peerDependencies": { "react": ">=16.8.0", "react-native": ">=0.59.0" } }, "sha512-4EYcYZFMhd/VMk9EwND4N2NOBZJYTksvbLdGTgqwYA1NT4IUmBgTFGeeng8GzazOqh4yP0tlW98zpW+67DEZsg=="],
|
||||
|
||||
"react-native-url-polyfill": ["react-native-url-polyfill@2.0.0", "", { "dependencies": { "whatwg-url-without-unicode": "8.0.0-3" }, "peerDependencies": { "react-native": "*" } }, "sha512-My330Do7/DvKnEvwQc0WdcBnFPploYKp9CYlefDXzIdEaA+PAhDYllkvGeEroEzvc4Kzzj2O4yVdz8v6fjRvhA=="],
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
@@ -23,7 +23,7 @@
|
||||
"react-native-paper": "^5.13.1",
|
||||
"react-native-safe-area-context": "^5.2.0",
|
||||
"react-native-svg": "^15.11.1",
|
||||
"react-native-update": "^10.23.0",
|
||||
"react-native-update": "^10.25.0",
|
||||
"react-native-vector-icons": "^10.2.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
@@ -25,7 +25,7 @@ import {LocalSvg} from 'react-native-svg/css';
|
||||
import TestConsole from './TestConsole';
|
||||
|
||||
import _updateConfig from '../update.json';
|
||||
import {PushyProvider, Pushy, usePushy} from 'react-native-update';
|
||||
import {UpdateProvider, Pushy, Cresc, useUpdate} from 'react-native-update';
|
||||
const {appKey} = _updateConfig[Platform.OS];
|
||||
|
||||
function App() {
|
||||
@@ -40,7 +40,7 @@ function App() {
|
||||
currentHash,
|
||||
parseTestQrCode,
|
||||
progress: {received, total} = {},
|
||||
} = usePushy();
|
||||
} = useUpdate();
|
||||
const [useDefaultAlert, setUseDefaultAlert] = useState(true);
|
||||
const [showTestConsole, setShowTestConsole] = useState(false);
|
||||
const [showUpdateBanner, setShowUpdateBanner] = useState(false);
|
||||
@@ -203,17 +203,24 @@ const styles = StyleSheet.create({
|
||||
image: {},
|
||||
});
|
||||
|
||||
const pushyClient = new Pushy({
|
||||
// use Pushy for China users
|
||||
// const updateClient = new Pushy({
|
||||
// appKey,
|
||||
// debug: true,
|
||||
// });
|
||||
|
||||
// use Cresc for global users
|
||||
const updateClient = new Cresc({
|
||||
appKey,
|
||||
debug: true,
|
||||
});
|
||||
|
||||
export default function Root() {
|
||||
return (
|
||||
<PushyProvider client={pushyClient}>
|
||||
<UpdateProvider client={updateClient}>
|
||||
<PaperProvider>
|
||||
<App />
|
||||
</PaperProvider>
|
||||
</PushyProvider>
|
||||
</UpdateProvider>
|
||||
);
|
||||
}
|
||||
|
@@ -98,23 +98,20 @@ public class UpdateModuleImpl {
|
||||
}
|
||||
});
|
||||
}catch (Exception e){
|
||||
promise.reject("执行报错:" + e.getMessage());
|
||||
promise.reject("downloadPatchFromPpk failed: "+e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static void reloadUpdate(UpdateContext updateContext, ReactApplicationContext mContext, ReadableMap options, Promise promise) {
|
||||
final String hash = options.getString("hash");
|
||||
|
||||
if (hash == null || hash.isEmpty()) {
|
||||
promise.reject("hash不能为空");
|
||||
return;
|
||||
}
|
||||
UiThreadUtil.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
updateContext.switchVersion(hash);
|
||||
final Context application = mContext.getApplicationContext();
|
||||
JSBundleLoader loader = JSBundleLoader.createFileLoader(UpdateContext.getBundleUrl(application));
|
||||
try {
|
||||
updateContext.switchVersion(hash);
|
||||
final Context application = mContext.getApplicationContext();
|
||||
ReactInstanceManager instanceManager = updateContext.getCustomReactInstanceManager();
|
||||
|
||||
if (instanceManager == null) {
|
||||
@@ -122,12 +119,10 @@ public class UpdateModuleImpl {
|
||||
}
|
||||
|
||||
try {
|
||||
JSBundleLoader loader = JSBundleLoader.createFileLoader(UpdateContext.getBundleUrl(application));
|
||||
Field loadField = instanceManager.getClass().getDeclaredField("mBundleLoader");
|
||||
loadField.setAccessible(true);
|
||||
loadField.set(instanceManager, loader);
|
||||
} catch (Throwable err) {
|
||||
promise.reject("pushy:"+err.getMessage());
|
||||
Field jsBundleField = instanceManager.getClass().getDeclaredField("mJSBundleFile");
|
||||
jsBundleField.setAccessible(true);
|
||||
jsBundleField.set(instanceManager, UpdateContext.getBundleUrl(application));
|
||||
@@ -137,31 +132,36 @@ public class UpdateModuleImpl {
|
||||
promise.resolve(true);
|
||||
|
||||
} catch (Throwable err) {
|
||||
promise.reject(err);
|
||||
Log.e("pushy", "switchVersion failed ", err);
|
||||
final Activity currentActivity = mContext.getCurrentActivity();
|
||||
if (currentActivity == null) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
// Try to get getReactDelegate method using reflection
|
||||
java.lang.reflect.Method getReactDelegateMethod =
|
||||
ReactActivity.class.getMethod("getReactDelegate");
|
||||
if (getReactDelegateMethod != null) {
|
||||
ReactDelegate reactDelegate = (ReactDelegate)
|
||||
getReactDelegateMethod.invoke(currentActivity);
|
||||
|
||||
// Try to get reload method using reflection
|
||||
java.lang.reflect.Method reloadMethod =
|
||||
ReactDelegate.class.getMethod("reload");
|
||||
if (reloadMethod != null) {
|
||||
reloadMethod.invoke(reactDelegate);
|
||||
} else {
|
||||
throw new NoSuchMethodException();
|
||||
}
|
||||
} else {
|
||||
throw new NoSuchMethodException();
|
||||
}
|
||||
|
||||
ReactDelegate reactDelegate = (ReactDelegate)
|
||||
getReactDelegateMethod.invoke(currentActivity);
|
||||
|
||||
Field reactHostField = ReactDelegate.class.getDeclaredField("mReactHost");
|
||||
reactHostField.setAccessible(true);
|
||||
Object reactHost = reactHostField.get(reactDelegate);
|
||||
|
||||
// Access the mReactHostDelegate field
|
||||
Field reactHostDelegateField = reactHost.getClass().getDeclaredField("mReactHostDelegate");
|
||||
reactHostDelegateField.setAccessible(true);
|
||||
Object reactHostDelegate = reactHostDelegateField.get(reactHost);
|
||||
|
||||
// Modify the jsBundleLoader field
|
||||
Field jsBundleLoaderField = reactHostDelegate.getClass().getDeclaredField("jsBundleLoader");
|
||||
jsBundleLoaderField.setAccessible(true);
|
||||
jsBundleLoaderField.set(reactHostDelegate, loader);
|
||||
|
||||
// Get the reload method with a String parameter
|
||||
java.lang.reflect.Method reloadMethod = reactHost.getClass().getMethod("reload", String.class);
|
||||
|
||||
// Invoke the reload method with a reason
|
||||
reloadMethod.invoke(reactHost, "react-native-update");
|
||||
} catch (Throwable e) {
|
||||
currentActivity.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
@@ -171,72 +171,54 @@ public class UpdateModuleImpl {
|
||||
});
|
||||
}
|
||||
}
|
||||
promise.resolve(true);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
public static void setNeedUpdate(UpdateContext updateContext, ReadableMap options,Promise promise) {
|
||||
try {
|
||||
final String hash = options.getString("hash");
|
||||
if(hash==null || hash.isEmpty()){
|
||||
promise.reject("hash不能为空");
|
||||
return;
|
||||
}
|
||||
UiThreadUtil.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
updateContext.switchVersion(hash);
|
||||
promise.resolve(true);
|
||||
} catch (Throwable err) {
|
||||
promise.reject("switchVersionLater failed:"+err.getMessage());
|
||||
Log.e("pushy", "switchVersionLater failed", err);
|
||||
}
|
||||
public static void setNeedUpdate(UpdateContext updateContext, ReadableMap options, Promise promise) {
|
||||
final String hash = options.getString("hash");
|
||||
UiThreadUtil.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
updateContext.switchVersion(hash);
|
||||
promise.resolve(true);
|
||||
} catch (Throwable err) {
|
||||
promise.reject("switchVersionLater failed: "+err.getMessage());
|
||||
Log.e("pushy", "switchVersionLater failed", err);
|
||||
}
|
||||
});
|
||||
} catch (Exception e){
|
||||
promise.reject("执行报错:"+e.getMessage());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static void markSuccess(UpdateContext updateContext,Promise promise) {
|
||||
try {
|
||||
UiThreadUtil.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
updateContext.markSuccess();
|
||||
promise.resolve(true);
|
||||
}
|
||||
});
|
||||
} catch (Exception e){
|
||||
promise.reject("执行报错:"+e.getMessage());
|
||||
}
|
||||
public static void markSuccess(UpdateContext updateContext, Promise promise) {
|
||||
UiThreadUtil.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
updateContext.markSuccess();
|
||||
promise.resolve(true);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static void setUuid(UpdateContext updateContext, String uuid, Promise promise) {
|
||||
try {
|
||||
UiThreadUtil.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
updateContext.setKv("uuid", uuid);
|
||||
promise.resolve(true);
|
||||
}
|
||||
});
|
||||
} catch (Exception e){
|
||||
promise.reject("执行报错:"+e.getMessage());
|
||||
}
|
||||
|
||||
UiThreadUtil.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
updateContext.setKv("uuid", uuid);
|
||||
promise.resolve(true);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static boolean check(String json) {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
try {
|
||||
mapper.readValue(json, Map.class);
|
||||
System.out.println("String can be converted to Map");
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
System.out.println("String cannot be converted to Map");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -246,12 +228,12 @@ public class UpdateModuleImpl {
|
||||
UiThreadUtil.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if(!check(info)){
|
||||
updateContext.setKv("hash_" + hash, info);
|
||||
promise.reject("校验报错:json字符串格式错误");
|
||||
}else {
|
||||
if (check(info)) {
|
||||
updateContext.setKv("hash_" + hash, info);
|
||||
promise.resolve(true);
|
||||
} else {
|
||||
updateContext.setKv("hash_" + hash, info);
|
||||
promise.reject("setLocalHashInfo failed: invalid json string");
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -262,7 +244,7 @@ public class UpdateModuleImpl {
|
||||
if (check(value)) {
|
||||
promise.resolve(value);
|
||||
} else {
|
||||
promise.reject("校验报错:json字符串格式错误");
|
||||
promise.reject("getLocalHashInfo failed: invalid json string");
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -78,6 +78,7 @@ RCT_EXPORT_MODULE(RCTPushy);
|
||||
BOOL needClearPushyInfo = ![curPackageVersion isEqualToString:packageVersion];
|
||||
if (needClearPushyInfo) {
|
||||
[defaults setObject:nil forKey:keyPushyInfo];
|
||||
[defaults setObject:nil forKey:keyHashInfo];
|
||||
[defaults setObject:@(YES) forKey:KeyPackageUpdatedMarked];
|
||||
|
||||
// ...need clear files later
|
||||
|
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "react-native-update",
|
||||
"version": "10.24.1",
|
||||
"version": "10.25.1",
|
||||
"description": "react-native hot update",
|
||||
"main": "src/index",
|
||||
"scripts": {
|
||||
|
@@ -1,5 +1,14 @@
|
||||
import { CheckResult, ClientOptions, ProgressData, EventType } from './type';
|
||||
import { emptyObj, joinUrls, log, noop, promiseAny, testUrls } from './utils';
|
||||
import {
|
||||
assertDev,
|
||||
assertWeb,
|
||||
emptyObj,
|
||||
joinUrls,
|
||||
log,
|
||||
noop,
|
||||
promiseAny,
|
||||
testUrls,
|
||||
} from './utils';
|
||||
import { EmitterSubscription, Platform } from 'react-native';
|
||||
import { PermissionsAndroid } from './permissions';
|
||||
import {
|
||||
@@ -17,7 +26,7 @@ import {
|
||||
|
||||
const SERVER_PRESETS = {
|
||||
// cn
|
||||
pushy: {
|
||||
Pushy: {
|
||||
main: 'https://update.react-native.cn/api',
|
||||
backups: ['https://update.reactnative.cn/api'],
|
||||
queryUrls: [
|
||||
@@ -26,7 +35,7 @@ const SERVER_PRESETS = {
|
||||
],
|
||||
},
|
||||
// i18n
|
||||
cresc: {
|
||||
Cresc: {
|
||||
main: 'https://api.cresc.dev',
|
||||
backups: ['https://api.cresc.app'],
|
||||
queryUrls: [
|
||||
@@ -34,11 +43,8 @@ const SERVER_PRESETS = {
|
||||
],
|
||||
},
|
||||
};
|
||||
if (Platform.OS === 'web') {
|
||||
console.warn(
|
||||
'react-native-update does not support hot updates on the web platform and will not perform any operations',
|
||||
);
|
||||
}
|
||||
|
||||
assertWeb();
|
||||
|
||||
const defaultClientOptions: ClientOptions = {
|
||||
appKey: '',
|
||||
@@ -52,11 +58,8 @@ const defaultClientOptions: ClientOptions = {
|
||||
|
||||
// for China users
|
||||
export class Pushy {
|
||||
options: ClientOptions = {
|
||||
...defaultClientOptions,
|
||||
server: SERVER_PRESETS.pushy,
|
||||
};
|
||||
clientType: 'pushy' | 'cresc' = 'pushy';
|
||||
options = defaultClientOptions;
|
||||
clientType: 'Pushy' | 'Cresc' = 'Pushy';
|
||||
lastChecking?: number;
|
||||
lastRespJson?: Promise<any>;
|
||||
|
||||
@@ -85,6 +88,8 @@ export class Pushy {
|
||||
throw new Error('appKey is required');
|
||||
}
|
||||
}
|
||||
this.clientType = new.target.name as 'Pushy' | 'Cresc';
|
||||
this.options.server = SERVER_PRESETS[this.clientType];
|
||||
this.setOptions(options);
|
||||
if (isRolledBack) {
|
||||
this.report({
|
||||
@@ -150,6 +155,15 @@ export class Pushy {
|
||||
}
|
||||
return true;
|
||||
};
|
||||
assertDebug = () => {
|
||||
if (__DEV__ && !this.options.debug) {
|
||||
console.info(
|
||||
'You are currently in the development environment and have not enabled debug mode. The hot update check will not be performed. If you need to debug hot updates in the development environment, please set debug to true in the client.',
|
||||
);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
markSuccess = () => {
|
||||
if (Pushy.marked || __DEV__ || !isFirstTime) {
|
||||
return;
|
||||
@@ -159,10 +173,7 @@ export class Pushy {
|
||||
this.report({ type: 'markSuccess' });
|
||||
};
|
||||
switchVersion = async (hash: string) => {
|
||||
if (__DEV__) {
|
||||
console.warn(
|
||||
'switchVersion() is not supported in development environment; no action taken.',
|
||||
);
|
||||
if (!assertDev('switchVersion()')) {
|
||||
return;
|
||||
}
|
||||
if (Pushy.assertHash(hash) && !Pushy.applyingUpdate) {
|
||||
@@ -173,10 +184,7 @@ export class Pushy {
|
||||
};
|
||||
|
||||
switchVersionLater = async (hash: string) => {
|
||||
if (__DEV__) {
|
||||
console.warn(
|
||||
'switchVersionLater() is not supported in development environment; no action taken.',
|
||||
);
|
||||
if (!assertDev('switchVersionLater()')) {
|
||||
return;
|
||||
}
|
||||
if (Pushy.assertHash(hash)) {
|
||||
@@ -185,14 +193,10 @@ export class Pushy {
|
||||
}
|
||||
};
|
||||
checkUpdate = async (extra?: Record<string, any>) => {
|
||||
if (__DEV__ && !this.options.debug) {
|
||||
console.info(
|
||||
'You are currently in the development environment and have not enabled debug mode. The hot update check will not be performed. If you need to debug hot updates in the development environment, please set debug to true in the client.',
|
||||
);
|
||||
if (!this.assertDebug()) {
|
||||
return;
|
||||
}
|
||||
if (Platform.OS === 'web') {
|
||||
console.warn('web platform does not support hot update check');
|
||||
if (!assertWeb()) {
|
||||
return;
|
||||
}
|
||||
if (
|
||||
@@ -508,10 +512,4 @@ export class Pushy {
|
||||
}
|
||||
|
||||
// for international users
|
||||
export class Cresc extends Pushy {
|
||||
clientType: 'cresc' | 'pushy' = 'cresc';
|
||||
options: ClientOptions = {
|
||||
...defaultClientOptions,
|
||||
server: SERVER_PRESETS.cresc,
|
||||
};
|
||||
}
|
||||
export class Cresc extends Pushy {}
|
||||
|
@@ -27,7 +27,9 @@ export const UpdateProvider = ({
|
||||
client: Pushy | Cresc;
|
||||
children: ReactNode;
|
||||
}) => {
|
||||
client = useRef(client).current;
|
||||
const { options } = client;
|
||||
|
||||
const stateListener = useRef<NativeEventSubscription>();
|
||||
const [updateInfo, setUpdateInfo] = useState<CheckResult>();
|
||||
const updateInfoRef = useRef(updateInfo);
|
||||
@@ -239,10 +241,7 @@ export const UpdateProvider = ({
|
||||
const markSuccess = client.markSuccess;
|
||||
|
||||
useEffect(() => {
|
||||
if (__DEV__ && !options.debug) {
|
||||
console.info(
|
||||
'您当前处于开发环境且未启用debug,不会进行热更检查。如需在开发环境中调试热更,请在client中设置debug为true',
|
||||
);
|
||||
if (!client.assertDebug()) {
|
||||
return;
|
||||
}
|
||||
const { checkStrategy, dismissErrorAfter, autoMarkSuccess } = options;
|
||||
@@ -272,7 +271,7 @@ export const UpdateProvider = ({
|
||||
stateListener.current && stateListener.current.remove();
|
||||
clearTimeout(dismissErrorTimer);
|
||||
};
|
||||
}, [checkUpdate, options, dismissError, markSuccess]);
|
||||
}, [checkUpdate, options, dismissError, markSuccess, client]);
|
||||
|
||||
const parseTestPayload = useCallback(
|
||||
(payload: UpdateTestPayload) => {
|
||||
@@ -326,11 +325,16 @@ export const UpdateProvider = ({
|
||||
};
|
||||
|
||||
Linking.getInitialURL().then(parseLinking);
|
||||
const linkingListener = Linking.addEventListener('url', ({ url }) =>
|
||||
parseLinking(url),
|
||||
);
|
||||
const linkingHandler = ({ url }: { url: string }) => {
|
||||
parseLinking(url);
|
||||
};
|
||||
const linkingListener = Linking.addEventListener('url', linkingHandler);
|
||||
return () => {
|
||||
linkingListener.remove();
|
||||
if (typeof linkingListener.remove === 'function') {
|
||||
linkingListener.remove();
|
||||
} else if ('removeEventListener' in Linking) {
|
||||
(Linking as any).removeEventListener('url', linkingHandler);
|
||||
}
|
||||
};
|
||||
}, [parseTestPayload]);
|
||||
|
||||
|
20
src/utils.ts
20
src/utils.ts
@@ -84,3 +84,23 @@ export const testUrls = async (urls?: string[]) => {
|
||||
log('all ping failed, use first url:', urls[0]);
|
||||
return urls[0];
|
||||
};
|
||||
|
||||
export const assertWeb = () => {
|
||||
if (Platform.OS === 'web') {
|
||||
console.warn(
|
||||
'react-native-update does not support the Web platform and will not perform any operations',
|
||||
);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
export const assertDev = (matter: string) => {
|
||||
if (__DEV__) {
|
||||
console.warn(
|
||||
`${matter} is not supported in development environment; no action taken.`,
|
||||
);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
Reference in New Issue
Block a user