mirror of
https://gitcode.com/gh_mirrors/re/react-native-pushy.git
synced 2025-09-16 10:21:37 +08:00
do not use static properties
This commit is contained in:
125
src/client.ts
125
src/client.ts
@@ -61,6 +61,31 @@ const defaultClientOptions: ClientOptions = {
|
|||||||
throwError: false,
|
throwError: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const sharedState: {
|
||||||
|
progressHandlers: Record<string, EmitterSubscription>;
|
||||||
|
downloadedHash?: string;
|
||||||
|
apkStatus: 'downloading' | 'downloaded' | null;
|
||||||
|
marked: boolean;
|
||||||
|
applyingUpdate: boolean;
|
||||||
|
} = {
|
||||||
|
progressHandlers: {},
|
||||||
|
downloadedHash: undefined,
|
||||||
|
apkStatus: null,
|
||||||
|
marked: false,
|
||||||
|
applyingUpdate: false,
|
||||||
|
};
|
||||||
|
|
||||||
|
const assertHash = (hash: string) => {
|
||||||
|
if (!sharedState.downloadedHash) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (hash !== sharedState.downloadedHash) {
|
||||||
|
log(`use downloaded hash ${sharedState.downloadedHash} first`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
// for China users
|
// for China users
|
||||||
export class Pushy {
|
export class Pushy {
|
||||||
options = defaultClientOptions;
|
options = defaultClientOptions;
|
||||||
@@ -68,13 +93,6 @@ export class Pushy {
|
|||||||
lastChecking?: number;
|
lastChecking?: number;
|
||||||
lastRespJson?: Promise<any>;
|
lastRespJson?: Promise<any>;
|
||||||
|
|
||||||
static progressHandlers: Record<string, EmitterSubscription> = {};
|
|
||||||
static downloadedHash?: string;
|
|
||||||
|
|
||||||
static apkStatus: 'downloading' | 'downloaded' | null = null;
|
|
||||||
|
|
||||||
static marked = false;
|
|
||||||
static applyingUpdate = false;
|
|
||||||
version = cInfo.rnu;
|
version = cInfo.rnu;
|
||||||
loggerPromise = (() => {
|
loggerPromise = (() => {
|
||||||
let resolve: (value?: unknown) => void = () => {};
|
let resolve: (value?: unknown) => void = () => {};
|
||||||
@@ -152,17 +170,6 @@ export class Pushy {
|
|||||||
getCheckUrl = (endpoint: string = this.options.server!.main) => {
|
getCheckUrl = (endpoint: string = this.options.server!.main) => {
|
||||||
return `${endpoint}/checkUpdate/${this.options.appKey}`;
|
return `${endpoint}/checkUpdate/${this.options.appKey}`;
|
||||||
};
|
};
|
||||||
static assertHash = (hash: string) => {
|
|
||||||
if (!Pushy.downloadedHash) {
|
|
||||||
log('no downloaded hash');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (hash !== Pushy.downloadedHash) {
|
|
||||||
log(`use downloaded hash ${Pushy.downloadedHash} first`);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
};
|
|
||||||
assertDebug = () => {
|
assertDebug = () => {
|
||||||
if (__DEV__ && !this.options.debug) {
|
if (__DEV__ && !this.options.debug) {
|
||||||
console.info(
|
console.info(
|
||||||
@@ -173,10 +180,10 @@ export class Pushy {
|
|||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
markSuccess = () => {
|
markSuccess = () => {
|
||||||
if (Pushy.marked || __DEV__ || !isFirstTime) {
|
if (sharedState.marked || __DEV__ || !isFirstTime) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Pushy.marked = true;
|
sharedState.marked = true;
|
||||||
PushyModule.markSuccess();
|
PushyModule.markSuccess();
|
||||||
this.report({ type: 'markSuccess' });
|
this.report({ type: 'markSuccess' });
|
||||||
};
|
};
|
||||||
@@ -184,9 +191,9 @@ export class Pushy {
|
|||||||
if (!assertDev('switchVersion()')) {
|
if (!assertDev('switchVersion()')) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (Pushy.assertHash(hash) && !Pushy.applyingUpdate) {
|
if (assertHash(hash) && !sharedState.applyingUpdate) {
|
||||||
log('switchVersion: ' + hash);
|
log('switchVersion: ' + hash);
|
||||||
Pushy.applyingUpdate = true;
|
sharedState.applyingUpdate = true;
|
||||||
return PushyModule.reloadUpdate({ hash });
|
return PushyModule.reloadUpdate({ hash });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -195,7 +202,7 @@ export class Pushy {
|
|||||||
if (!assertDev('switchVersionLater()')) {
|
if (!assertDev('switchVersionLater()')) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (Pushy.assertHash(hash)) {
|
if (assertHash(hash)) {
|
||||||
log('switchVersionLater: ' + hash);
|
log('switchVersionLater: ' + hash);
|
||||||
return PushyModule.setNeedUpdate({ hash });
|
return PushyModule.setNeedUpdate({ hash });
|
||||||
}
|
}
|
||||||
@@ -350,18 +357,18 @@ export class Pushy {
|
|||||||
log(`rolledback hash ${rolledBackVersion}, ignored`);
|
log(`rolledback hash ${rolledBackVersion}, ignored`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (Pushy.downloadedHash === hash) {
|
if (sharedState.downloadedHash === hash) {
|
||||||
log(`duplicated downloaded hash ${Pushy.downloadedHash}, ignored`);
|
log(`duplicated downloaded hash ${sharedState.downloadedHash}, ignored`);
|
||||||
return Pushy.downloadedHash;
|
return sharedState.downloadedHash;
|
||||||
}
|
}
|
||||||
if (Pushy.progressHandlers[hash]) {
|
if (sharedState.progressHandlers[hash]) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const patchStartTime = Date.now();
|
const patchStartTime = Date.now();
|
||||||
if (onDownloadProgress) {
|
if (onDownloadProgress) {
|
||||||
// @ts-expect-error harmony not in existing platforms
|
// @ts-expect-error harmony not in existing platforms
|
||||||
if (Platform.OS === 'harmony') {
|
if (Platform.OS === 'harmony') {
|
||||||
Pushy.progressHandlers[hash] = DeviceEventEmitter.addListener(
|
sharedState.progressHandlers[hash] = DeviceEventEmitter.addListener(
|
||||||
'RCTPushyDownloadProgress',
|
'RCTPushyDownloadProgress',
|
||||||
progressData => {
|
progressData => {
|
||||||
if (progressData.hash === hash) {
|
if (progressData.hash === hash) {
|
||||||
@@ -370,14 +377,15 @@ export class Pushy {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
Pushy.progressHandlers[hash] = pushyNativeEventEmitter.addListener(
|
sharedState.progressHandlers[hash] =
|
||||||
'RCTPushyDownloadProgress',
|
pushyNativeEventEmitter.addListener(
|
||||||
progressData => {
|
'RCTPushyDownloadProgress',
|
||||||
if (progressData.hash === hash) {
|
progressData => {
|
||||||
onDownloadProgress(progressData);
|
if (progressData.hash === hash) {
|
||||||
}
|
onDownloadProgress(progressData);
|
||||||
},
|
}
|
||||||
);
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let succeeded = '';
|
let succeeded = '';
|
||||||
@@ -445,9 +453,9 @@ export class Pushy {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (Pushy.progressHandlers[hash]) {
|
if (sharedState.progressHandlers[hash]) {
|
||||||
Pushy.progressHandlers[hash].remove();
|
sharedState.progressHandlers[hash].remove();
|
||||||
delete Pushy.progressHandlers[hash];
|
delete sharedState.progressHandlers[hash];
|
||||||
}
|
}
|
||||||
if (__DEV__) {
|
if (__DEV__) {
|
||||||
return hash;
|
return hash;
|
||||||
@@ -483,7 +491,7 @@ export class Pushy {
|
|||||||
description,
|
description,
|
||||||
metaInfo,
|
metaInfo,
|
||||||
});
|
});
|
||||||
Pushy.downloadedHash = hash;
|
sharedState.downloadedHash = hash;
|
||||||
return hash;
|
return hash;
|
||||||
};
|
};
|
||||||
downloadAndInstallApk = async (
|
downloadAndInstallApk = async (
|
||||||
@@ -493,10 +501,10 @@ export class Pushy {
|
|||||||
if (Platform.OS !== 'android') {
|
if (Platform.OS !== 'android') {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (Pushy.apkStatus === 'downloading') {
|
if (sharedState.apkStatus === 'downloading') {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (Pushy.apkStatus === 'downloaded') {
|
if (sharedState.apkStatus === 'downloaded') {
|
||||||
this.report({ type: 'errorInstallApk' });
|
this.report({ type: 'errorInstallApk' });
|
||||||
this.throwIfEnabled(new Error('errorInstallApk'));
|
this.throwIfEnabled(new Error('errorInstallApk'));
|
||||||
return;
|
return;
|
||||||
@@ -517,35 +525,36 @@ export class Pushy {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Pushy.apkStatus = 'downloading';
|
sharedState.apkStatus = 'downloading';
|
||||||
this.report({ type: 'downloadingApk' });
|
this.report({ type: 'downloadingApk' });
|
||||||
const progressKey = 'downloadingApk';
|
const progressKey = 'downloadingApk';
|
||||||
if (onDownloadProgress) {
|
if (onDownloadProgress) {
|
||||||
if (Pushy.progressHandlers[progressKey]) {
|
if (sharedState.progressHandlers[progressKey]) {
|
||||||
Pushy.progressHandlers[progressKey].remove();
|
sharedState.progressHandlers[progressKey].remove();
|
||||||
}
|
}
|
||||||
Pushy.progressHandlers[progressKey] = pushyNativeEventEmitter.addListener(
|
sharedState.progressHandlers[progressKey] =
|
||||||
'RCTPushyDownloadProgress',
|
pushyNativeEventEmitter.addListener(
|
||||||
(progressData: ProgressData) => {
|
'RCTPushyDownloadProgress',
|
||||||
if (progressData.hash === progressKey) {
|
(progressData: ProgressData) => {
|
||||||
onDownloadProgress(progressData);
|
if (progressData.hash === progressKey) {
|
||||||
}
|
onDownloadProgress(progressData);
|
||||||
},
|
}
|
||||||
);
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
await PushyModule.downloadAndInstallApk({
|
await PushyModule.downloadAndInstallApk({
|
||||||
url,
|
url,
|
||||||
target: 'update.apk',
|
target: 'update.apk',
|
||||||
hash: progressKey,
|
hash: progressKey,
|
||||||
}).catch(() => {
|
}).catch(() => {
|
||||||
Pushy.apkStatus = null;
|
sharedState.apkStatus = null;
|
||||||
this.report({ type: 'errorDownloadAndInstallApk' });
|
this.report({ type: 'errorDownloadAndInstallApk' });
|
||||||
this.throwIfEnabled(new Error('errorDownloadAndInstallApk'));
|
this.throwIfEnabled(new Error('errorDownloadAndInstallApk'));
|
||||||
});
|
});
|
||||||
Pushy.apkStatus = 'downloaded';
|
sharedState.apkStatus = 'downloaded';
|
||||||
if (Pushy.progressHandlers[progressKey]) {
|
if (sharedState.progressHandlers[progressKey]) {
|
||||||
Pushy.progressHandlers[progressKey].remove();
|
sharedState.progressHandlers[progressKey].remove();
|
||||||
delete Pushy.progressHandlers[progressKey];
|
delete sharedState.progressHandlers[progressKey];
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
restartApp = async () => {
|
restartApp = async () => {
|
||||||
|
@@ -62,7 +62,7 @@ const ping =
|
|||||||
if (!pingFinished) {
|
if (!pingFinished) {
|
||||||
log('ping timeout', url);
|
log('ping timeout', url);
|
||||||
}
|
}
|
||||||
}, 2000),
|
}, 5000),
|
||||||
),
|
),
|
||||||
]);
|
]);
|
||||||
};
|
};
|
||||||
@@ -81,6 +81,7 @@ export const testUrls = async (urls?: string[]) => {
|
|||||||
try {
|
try {
|
||||||
const ret = await promiseAny(urls.map(ping));
|
const ret = await promiseAny(urls.map(ping));
|
||||||
if (ret) {
|
if (ret) {
|
||||||
|
log('ping success, use url:', ret);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
} catch {}
|
} catch {}
|
||||||
|
Reference in New Issue
Block a user