1
0
mirror of https://gitcode.com/gh_mirrors/re/react-native-pushy.git synced 2025-09-17 20:26:09 +08:00
Code Issues Packages Projects Releases Wiki Activity GitHub Gitee

Compare commits

..

11 Commits

Author SHA1 Message Date
sunnylqm
b66b4dea06 v10.5.2 2024-04-23 00:08:53 +08:00
sunnylqm
ad9b0778ba v10.5.1 2024-04-22 23:15:34 +08:00
sunnylqm
3e60cfd80f v10.5.0 2024-04-22 23:11:07 +08:00
sunnylqm
782b0e5227 v10.4.2 2024-04-22 09:57:53 +08:00
sunnylqm
d1ecfb3f93 v10.4.1 2024-04-18 22:50:20 +08:00
sunnylqm
571c2819b9 v10.4.0 2024-04-18 22:41:00 +08:00
sunnylqm
1f75688b92 Refactor assertRelease function 2024-04-18 22:39:03 +08:00
sunnylqm
de332c1796 Update PushyOptions strategy to allow null value 2024-04-02 21:21:15 +08:00
sunnylqm
80e0451983 v10.3.1 2024-03-21 22:11:38 +08:00
sunnylqm
b512ae18b7 do not show alert if no downloadurl 2024-03-21 22:11:09 +08:00
sunnylqm
fe75a2ca9e Remove blockUpdate method from UpdateContext and UpdateModule 2024-03-21 13:03:32 +08:00
12 changed files with 105 additions and 76 deletions

View File

@@ -69,13 +69,6 @@ public class UpdateContext {
return context.getString(R.string.pushy_build_time);
}
public Map getBlockUpdate() {
return new HashMap<String, Object>() {{
put("until", sp.getInt("blockUntil", 0));
put("reason", sp.getString("blockReason", null));
}};
}
public boolean getIsUsingBundleUrl() {
return isUsingBundleUrl;
}

View File

@@ -45,7 +45,6 @@ public class UpdateModule extends NativePushySpec {
if (rolledBackVersion != null) {
updateContext.clearRollbackMark();
}
constants.put("blockUpdate", updateContext.getBlockUpdate());
constants.put("uuid", updateContext.getKv("uuid"));
return constants;
}

View File

@@ -59,7 +59,6 @@ public class UpdateModule extends ReactContextBaseJavaModule {
if (rolledBackVersion != null) {
updateContext.clearRollbackMark();
}
constants.put("blockUpdate", updateContext.getBlockUpdate());
constants.put("uuid", updateContext.getKv("uuid"));
return constants;
}

View File

@@ -16,7 +16,6 @@ static NSString *const paramLastVersion = @"lastVersion";
static NSString *const paramCurrentVersion = @"currentVersion";
static NSString *const paramIsFirstTime = @"isFirstTime";
static NSString *const paramIsFirstLoadOk = @"isFirstLoadOK";
static NSString *const keyBlockUpdate = @"REACTNATIVECN_PUSHY_BLOCKUPDATE";
static NSString *const keyUuid = @"REACTNATIVECN_PUSHY_UUID";
static NSString *const keyHashInfo = @"REACTNATIVECN_PUSHY_HASH_";
static NSString *const keyFirstLoadMarked = @"REACTNATIVECN_PUSHY_FIRSTLOADMARKED_KEY";
@@ -157,7 +156,6 @@ RCT_EXPORT_MODULE(RCTPushy);
ret[@"buildTime"] = [RCTPushy buildTime];
ret[@"rolledBackVersion"] = [defaults objectForKey:keyRolledBackMarked];
ret[@"isFirstTime"] = [defaults objectForKey:keyFirstLoadMarked];
ret[@"blockUpdate"] = [defaults objectForKey:keyBlockUpdate];
ret[@"uuid"] = [defaults objectForKey:keyUuid];
NSDictionary *pushyInfo = [defaults dictionaryForKey:keyPushyInfo];
ret[@"currentVersion"] = [pushyInfo objectForKey:paramCurrentVersion];

View File

@@ -1,8 +1,8 @@
{
"name": "react-native-update",
"version": "10.3.0",
"version": "10.5.2",
"description": "react-native hot update",
"main": "src/index.ts",
"main": "src/index",
"scripts": {
"prepack": "yarn submodule && yarn lint",
"lint": "eslint \"src/*.@(ts|tsx|js|jsx)\" && tsc --noEmit",

View File

@@ -1,10 +1,7 @@
import { CheckResult, PushyOptions, ProgressData, EventType } from './type';
import { assertRelease, log, testUrls } from './utils';
import {
EmitterSubscription,
PermissionsAndroid,
Platform,
} from 'react-native';
import { log, testUrls } from './utils';
import { EmitterSubscription, Platform } from 'react-native';
import type { PermissionsAndroidStatic } from 'react-native';
import {
PushyModule,
buildTime,
@@ -27,6 +24,10 @@ const defaultServer = {
const empty = {};
const noop = () => {};
if (Platform.OS === 'web') {
console.warn('react-native-update 不支持 web 端热更,不会执行操作');
}
export class Pushy {
options: PushyOptions = {
appKey: '',
@@ -35,6 +36,7 @@ export class Pushy {
useAlert: true,
strategy: 'both',
logger: noop,
debug: false,
};
lastChecking?: number;
@@ -112,8 +114,7 @@ export class Pushy {
return true;
};
markSuccess = () => {
assertRelease();
if (this.marked) {
if (this.marked || __DEV__) {
return;
}
this.marked = true;
@@ -121,7 +122,12 @@ export class Pushy {
this.report({ type: 'markSuccess' });
};
switchVersion = (hash: string) => {
assertRelease();
if (__DEV__) {
console.warn(
'您调用了switchVersion方法但是当前是开发环境不会进行任何操作。',
);
return;
}
if (this.assertHash(hash) && !this.applyingUpdate) {
log('switchVersion: ' + hash);
this.applyingUpdate = true;
@@ -130,14 +136,28 @@ export class Pushy {
};
switchVersionLater = (hash: string) => {
assertRelease();
if (__DEV__) {
console.warn(
'您调用了switchVersionLater方法但是当前是开发环境不会进行任何操作。',
);
return;
}
if (this.assertHash(hash)) {
log('switchVersionLater: ' + hash);
PushyModule.setNeedUpdate({ hash });
}
};
checkUpdate = async () => {
assertRelease();
if (__DEV__ && !this.options.debug) {
console.info(
'您当前处于开发环境且未启用 debug不会进行热更检查。如需在开发环境中调试热更请在 client 中设置 debug 为 true',
);
return;
}
if (Platform.OS === 'web') {
console.warn('web 端不支持热更新检查');
return;
}
const now = Date.now();
if (
this.lastRespJson &&
@@ -148,18 +168,22 @@ export class Pushy {
}
this.lastChecking = now;
this.report({ type: 'checking' });
const fetchBody = {
packageVersion,
hash: currentVersion,
buildTime,
cInfo,
};
if (__DEV__) {
delete fetchBody.buildTime;
}
const fetchPayload = {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
packageVersion,
hash: currentVersion,
buildTime,
cInfo,
}),
body: JSON.stringify(fetchBody),
};
let resp;
try {
@@ -225,7 +249,6 @@ export class Pushy {
info: CheckResult,
onDownloadProgress?: (data: ProgressData) => void,
) => {
assertRelease();
const {
hash,
diffUrl: _diffUrl,
@@ -275,7 +298,11 @@ export class Pushy {
});
succeeded = true;
} catch (e: any) {
log(`diff error: ${e.message}, try pdiff`);
if (__DEV__) {
succeeded = true;
} else {
log(`diff error: ${e.message}, try pdiff`);
}
}
}
const pdiffUrl = (await testUrls(pdiffUrls)) || _pdiffUrl;
@@ -288,7 +315,11 @@ export class Pushy {
});
succeeded = true;
} catch (e: any) {
log(`pdiff error: ${e.message}, try full patch`);
if (__DEV__) {
succeeded = true;
} else {
log(`pdiff error: ${e.message}, try full patch`);
}
}
}
const updateUrl = (await testUrls(updateUrls)) || _updateUrl;
@@ -301,13 +332,20 @@ export class Pushy {
});
succeeded = true;
} catch (e: any) {
log(`full patch error: ${e.message}`);
if (__DEV__) {
succeeded = true;
} else {
log(`full patch error: ${e.message}`);
}
}
}
if (this.progressHandlers[hash]) {
this.progressHandlers[hash].remove();
delete this.progressHandlers[hash];
}
if (__DEV__) {
return hash;
}
if (!succeeded) {
return this.report({
type: 'errorUpdate',
@@ -333,6 +371,8 @@ export class Pushy {
this.report({ type: 'downloadingApk' });
if (Platform.Version <= 23) {
try {
const PermissionsAndroid =
require('react-native/Libraries/PermissionsAndroid/PermissionsAndroid') as PermissionsAndroidStatic;
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
);

View File

@@ -1,13 +0,0 @@
const noop = () => {};
export class Pushy {
constructor() {
console.warn(
'react-native-update is not supported and will do nothing on web.',
);
return new Proxy(this, {
get() {
return noop;
},
});
}
}

View File

@@ -8,9 +8,23 @@ const isTurboModuleEnabled =
// @ts-expect-error
global.__turboModuleProxy != null;
export const PushyModule = isTurboModuleEnabled
? require('./NativePushy').default
: NativeModules.Pushy;
const noop = () => {};
class EmptyModule {
constructor() {
return new Proxy(this, {
get() {
return noop;
},
});
}
}
export const PushyModule =
Platform.OS === 'web'
? new EmptyModule()
: isTurboModuleEnabled
? require('./NativePushy').default
: NativeModules.Pushy;
if (!PushyModule) {
throw new Error('react-native-update模块无法加载请对照安装文档检查配置。');

View File

@@ -124,20 +124,20 @@ export const PushyProvider = ({
setUpdateInfo(info);
if (info.expired) {
const { downloadUrl } = info;
showAlert('提示', '您的应用版本已更新,点击更新下载安装新版本', [
{
text: '更新',
onPress: () => {
if (downloadUrl) {
if (downloadUrl) {
showAlert('提示', '您的应用版本已更新,点击更新下载安装新版本', [
{
text: '更新',
onPress: () => {
if (Platform.OS === 'android' && downloadUrl.endsWith('.apk')) {
downloadAndInstallApk(downloadUrl);
} else {
Linking.openURL(downloadUrl);
}
}
},
},
},
]);
]);
}
} else if (info.update) {
showAlert(
'提示',
@@ -159,8 +159,10 @@ export const PushyProvider = ({
const markSuccess = client.markSuccess;
useEffect(() => {
if (__DEV__) {
console.info('检测到在DEV环境不会进行热更新检查');
if (__DEV__ && !options.debug) {
console.info(
'您当前处于开发环境且未启用debug不会进行热更检查。如需在开发环境中调试热更请在client中设置debug为true',
);
return;
}
const { strategy, dismissErrorAfter, autoMarkSuccess } = options;

View File

@@ -1,2 +0,0 @@
import { Fragment } from 'react';
export const PushyProvider = Fragment;

View File

@@ -70,7 +70,8 @@ export interface PushyOptions {
server?: PushyServerConfig;
logger?: UpdateEventsLogger;
useAlert?: boolean;
strategy?: 'onAppStart' | 'onAppResume' | 'both';
strategy?: 'onAppStart' | 'onAppResume' | 'both' | null;
autoMarkSuccess?: boolean;
dismissErrorAfter?: number;
debug?: boolean;
}

View File

@@ -1,20 +1,18 @@
import { Platform } from 'react-native';
export function log(...args: any[]) {
console.log('pushy: ', ...args);
}
export function assertRelease() {
if (__DEV__) {
throw new Error('react-native-update 只能在 RELEASE 版本中运行.');
}
}
const ping = async (url: string) =>
Promise.race([
fetch(url, {
method: 'HEAD',
}).then(({ status }) => status === 200),
new Promise<false>(r => setTimeout(() => r(false), 2000)),
]);
const ping =
Platform.OS === 'web'
? () => Promise.resolve(true)
: async (url: string) =>
Promise.race([
fetch(url, {
method: 'HEAD',
}).then(({ status }) => status === 200),
new Promise<false>(r => setTimeout(() => r(false), 2000)),
]);
const canUseGoogle = ping('https://www.google.com');