1
0
mirror of https://gitcode.com/gh_mirrors/re/react-native-pushy.git synced 2025-09-16 10:21:37 +08:00
Code Issues Packages Projects Releases Wiki Activity GitHub Gitee
This commit is contained in:
sunnylqm
2024-11-26 00:53:11 +08:00
parent 2192000d53
commit d4f21a39f5
3 changed files with 47 additions and 32 deletions

View File

@@ -1,6 +1,6 @@
{
"name": "react-native-update",
"version": "10.16.0",
"version": "10.17.0",
"description": "react-native hot update",
"main": "src/index",
"scripts": {
@@ -74,5 +74,6 @@
"react-native": "0.73",
"ts-jest": "^29.2.5",
"typescript": "^5.6.3"
}
},
"packageManager": "yarn@1.22.21+sha1.1959a18351b811cdeedbd484a8f86c3cc3bbaf72"
}

View File

@@ -43,11 +43,13 @@ export class Pushy {
lastChecking?: number;
lastRespJson?: Promise<any>;
progressHandlers: Record<string, EmitterSubscription> = {};
downloadedHash?: string;
static progressHandlers: Record<string, EmitterSubscription> = {};
static downloadedHash?: string;
marked = false;
applyingUpdate = false;
static apkStatus: 'downloading' | 'downloaded' | null = null;
static marked = false;
static applyingUpdate = false;
version = cInfo.pushy;
loggerPromise = (() => {
let resolve: (value?: unknown) => void = () => {};
@@ -121,21 +123,21 @@ export class Pushy {
getCheckUrl = (endpoint: string = this.options.server!.main) => {
return `${endpoint}/checkUpdate/${this.options.appKey}`;
};
assertHash = (hash: string) => {
if (!this.downloadedHash) {
static assertHash = (hash: string) => {
if (!Pushy.downloadedHash) {
return;
}
if (hash !== this.downloadedHash) {
log(`use downloaded hash ${this.downloadedHash} first`);
if (hash !== Pushy.downloadedHash) {
log(`use downloaded hash ${Pushy.downloadedHash} first`);
return;
}
return true;
};
markSuccess = () => {
if (this.marked || __DEV__ || !isFirstTime) {
if (Pushy.marked || __DEV__ || !isFirstTime) {
return;
}
this.marked = true;
Pushy.marked = true;
PushyModule.markSuccess();
this.report({ type: 'markSuccess' });
};
@@ -146,9 +148,9 @@ export class Pushy {
);
return;
}
if (this.assertHash(hash) && !this.applyingUpdate) {
if (Pushy.assertHash(hash) && !Pushy.applyingUpdate) {
log('switchVersion: ' + hash);
this.applyingUpdate = true;
Pushy.applyingUpdate = true;
return PushyModule.reloadUpdate({ hash });
}
};
@@ -160,7 +162,7 @@ export class Pushy {
);
return;
}
if (this.assertHash(hash)) {
if (Pushy.assertHash(hash)) {
log('switchVersionLater: ' + hash);
return PushyModule.setNeedUpdate({ hash });
}
@@ -314,15 +316,15 @@ export class Pushy {
log(`rolledback hash ${rolledBackVersion}, ignored`);
return;
}
if (this.downloadedHash === hash) {
log(`duplicated downloaded hash ${this.downloadedHash}, ignored`);
return this.downloadedHash;
if (Pushy.downloadedHash === hash) {
log(`duplicated downloaded hash ${Pushy.downloadedHash}, ignored`);
return Pushy.downloadedHash;
}
if (this.progressHandlers[hash]) {
if (Pushy.progressHandlers[hash]) {
return;
}
if (onDownloadProgress) {
this.progressHandlers[hash] = pushyNativeEventEmitter.addListener(
Pushy.progressHandlers[hash] = pushyNativeEventEmitter.addListener(
'RCTPushyDownloadProgress',
progressData => {
if (progressData.hash === hash) {
@@ -389,9 +391,9 @@ export class Pushy {
}
}
}
if (this.progressHandlers[hash]) {
this.progressHandlers[hash].remove();
delete this.progressHandlers[hash];
if (Pushy.progressHandlers[hash]) {
Pushy.progressHandlers[hash].remove();
delete Pushy.progressHandlers[hash];
}
if (__DEV__) {
return hash;
@@ -417,7 +419,7 @@ export class Pushy {
description,
metaInfo,
});
this.downloadedHash = hash;
Pushy.downloadedHash = hash;
return hash;
};
downloadAndInstallApk = async (
@@ -427,7 +429,14 @@ export class Pushy {
if (Platform.OS !== 'android') {
return;
}
this.report({ type: 'downloadingApk' });
if (Pushy.apkStatus === 'downloading') {
return;
}
if (Pushy.apkStatus === 'downloaded') {
this.report({ type: 'errorInstallApk' });
this.throwIfEnabled(new Error('errorInstallApk'));
return;
}
if (Platform.Version <= 23) {
try {
const granted = await PermissionsAndroid.request(
@@ -444,12 +453,14 @@ export class Pushy {
return;
}
}
Pushy.apkStatus = 'downloading';
this.report({ type: 'downloadingApk' });
const progressKey = 'downloadingApk';
if (onDownloadProgress) {
if (this.progressHandlers[progressKey]) {
this.progressHandlers[progressKey].remove();
if (Pushy.progressHandlers[progressKey]) {
Pushy.progressHandlers[progressKey].remove();
}
this.progressHandlers[progressKey] = pushyNativeEventEmitter.addListener(
Pushy.progressHandlers[progressKey] = pushyNativeEventEmitter.addListener(
'RCTPushyDownloadProgress',
(progressData: ProgressData) => {
if (progressData.hash === progressKey) {
@@ -463,12 +474,14 @@ export class Pushy {
target: 'update.apk',
hash: progressKey,
}).catch(() => {
Pushy.apkStatus = null;
this.report({ type: 'errorDownloadAndInstallApk' });
this.throwIfEnabled(new Error('errorDownloadAndInstallApk'));
});
if (this.progressHandlers[progressKey]) {
this.progressHandlers[progressKey].remove();
delete this.progressHandlers[progressKey];
Pushy.apkStatus = 'downloaded';
if (Pushy.progressHandlers[progressKey]) {
Pushy.progressHandlers[progressKey].remove();
delete Pushy.progressHandlers[progressKey];
}
};
}

View File

@@ -38,7 +38,8 @@ export type EventType =
| 'downloadingApk'
| 'rejectStoragePermission'
| 'errorStoragePermission'
| 'errorDownloadAndInstallApk';
| 'errorDownloadAndInstallApk'
| 'errorInstallApk';
export interface EventData {
currentVersion: string;