1
0
mirror of https://gitcode.com/gh_mirrors/re/react-native-pushy.git synced 2025-11-01 13:53:11 +08:00
Code Issues Packages Projects Releases Wiki Activity GitHub Gitee

chore: some tweak

This commit is contained in:
sunnylqm
2023-02-20 13:03:12 +08:00
parent 55879198e8
commit d6c268f533
7 changed files with 68 additions and 57 deletions

View File

@@ -4,38 +4,46 @@
*/
'use strict';
import type {TurboModule} from 'react-native/Libraries/TurboModule/RCTExport';
import {TurboModuleRegistry} from 'react-native';
import type { TurboModule } from 'react-native/Libraries/TurboModule/RCTExport';
import { TurboModuleRegistry } from 'react-native';
export interface Spec extends TurboModule {
getConstants: () => {
downloadRootDir: string;
packageVersion: string;
currentVersion: string;
isFirstTime: boolean;
rolledBackVersion: string;
buildTime: string;
blockUpdate: Object;
uuid: string;
isUsingBundleUrl: boolean;
getConstants: () => {
downloadRootDir: string,
packageVersion: string,
currentVersion: string,
isFirstTime: boolean,
rolledBackVersion: string,
buildTime: string,
blockUpdate: Object,
uuid: string,
isUsingBundleUrl: boolean,
};
setLocalHashInfo(hash: string, info:string): void;
getLocalHashInfo(hash: string):Promise<string>;
setLocalHashInfo(hash: string, info: string): void;
getLocalHashInfo(hash: string): Promise<string>;
setUuid(uuid: string): void;
setBlockUpdate(options: {reason: string,until: number}): void;
downloadPatchFromPpk(options: { updateUrl:string,
setBlockUpdate(options: { reason: string, until: number }): void;
downloadPatchFromPpk(options: {
updateUrl: string,
hash: string,
originHash: string}):Promise<void>;
downloadPatchFromPackage(options: { updateUrl:string,
hash: string}): Promise<void>;
downloadFullUpdate(options: { updateUrl:string,
hash: string}): Promise<void>;
reloadUpdate(options: { hash: string}): void;
setNeedUpdate(options: { hash: string}): void;
originHash: string,
}): Promise<void>;
downloadPatchFromPackage(options: {
updateUrl: string,
hash: string,
}): Promise<void>;
downloadFullUpdate(options: {
updateUrl: string,
hash: string,
}): Promise<void>;
reloadUpdate(options: { hash: string }): void;
setNeedUpdate(options: { hash: string }): void;
markSuccess(): void;
downloadAndInstallApk(options: { url: string,
target : string,
hash: string}): Promise<void>;
downloadAndInstallApk(options: {
url: string,
target: string,
hash: string,
}): Promise<void>;
}
export default (TurboModuleRegistry.get<Spec>('Pushy'): ?Spec);
export default (TurboModuleRegistry.get<Spec>('Pushy'): ?Spec);

View File

@@ -16,14 +16,17 @@ const {
} = require('react-native/Libraries/Core/ReactNativeVersion');
const RNVersion = `${v.major}.${v.minor}.${v.patch}`;
const isTurboModuleEnabled = global.__turboModuleProxy != null;
const Pushy = isTurboModuleEnabled ?
require("./NativeUpdate").default :
NativeModules.Pushy;
if (!Pushy) {
export const PushyModule = isTurboModuleEnabled
? require('./NativeUpdate').default
: NativeModules.Pushy;
if (!PushyModule) {
throw new Error('react-native-update模块无法加载请对照安装文档检查配置。');
}
const PushyConstants = isTurboModuleEnabled ? Pushy.getConstants(): Pushy;
const PushyConstants = isTurboModuleEnabled
? PushyModule.getConstants()
: PushyModule;
export const downloadRootDir = PushyConstants.downloadRootDir;
export const packageVersion = PushyConstants.packageVersion;
@@ -43,22 +46,22 @@ if (Platform.OS === 'android' && !PushyConstants.isUsingBundleUrl) {
}
function setLocalHashInfo(hash, info) {
Pushy.setLocalHashInfo(hash, JSON.stringify(info));
PushyModule.setLocalHashInfo(hash, JSON.stringify(info));
}
async function getLocalHashInfo(hash) {
return JSON.parse(await Pushy.getLocalHashInfo(hash));
return JSON.parse(await PushyModule.getLocalHashInfo(hash));
}
export async function getCurrentVersionInfo() {
return currentVersion ? (await getLocalHashInfo(currentVersion)) || {} : {};
}
const eventEmitter = new NativeEventEmitter(Pushy);
const eventEmitter = new NativeEventEmitter(PushyModule);
if (!uuid) {
uuid = require('nanoid/non-secure').nanoid();
Pushy.setUuid(uuid);
PushyModule.setUuid(uuid);
}
function logger(text) {
@@ -166,7 +169,7 @@ function checkOperation(op) {
reason: action.reason,
until: Math.round((Date.now() + action.duration) / 1000),
};
Pushy.setBlockUpdate(blockUpdate);
PushyModule.setBlockUpdate(blockUpdate);
}
});
}
@@ -212,7 +215,7 @@ export async function downloadUpdate(options, eventListeners) {
if (options.diffUrl) {
logger('downloading diff');
try {
await Pushy.downloadPatchFromPpk({
await PushyModule.downloadPatchFromPpk({
updateUrl: options.diffUrl,
hash: options.hash,
originHash: currentVersion,
@@ -225,7 +228,7 @@ export async function downloadUpdate(options, eventListeners) {
if (!succeeded && options.pdiffUrl) {
logger('downloading pdiff');
try {
await Pushy.downloadPatchFromPackage({
await PushyModule.downloadPatchFromPackage({
updateUrl: options.pdiffUrl,
hash: options.hash,
});
@@ -237,7 +240,7 @@ export async function downloadUpdate(options, eventListeners) {
if (!succeeded && options.updateUrl) {
logger('downloading full patch');
try {
await Pushy.downloadFullUpdate({
await PushyModule.downloadFullUpdate({
updateUrl: options.updateUrl,
hash: options.hash,
});
@@ -276,7 +279,7 @@ export function switchVersion(hash) {
assertRelease();
if (assertHash(hash)) {
logger('switchVersion: ' + hash);
Pushy.reloadUpdate({ hash });
PushyModule.reloadUpdate({ hash });
}
}
@@ -284,7 +287,7 @@ export function switchVersionLater(hash) {
assertRelease();
if (assertHash(hash)) {
logger('switchVersionLater: ' + hash);
Pushy.setNeedUpdate({ hash });
PushyModule.setNeedUpdate({ hash });
}
}
@@ -296,7 +299,7 @@ export function markSuccess() {
return;
}
marked = true;
Pushy.markSuccess();
PushyModule.markSuccess();
report(currentVersion, 'success');
}
@@ -326,7 +329,7 @@ export async function downloadAndInstallApk({ url, onDownloadProgress }) {
},
);
}
await Pushy.downloadAndInstallApk({
await PushyModule.downloadAndInstallApk({
url,
target: 'update.apk',
hash,