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

Implement download progress

This commit is contained in:
sunnylqm
2020-09-16 13:01:14 +08:00
parent a4052091e0
commit a966655faf
6 changed files with 1686 additions and 978 deletions

10
lib/index.d.ts vendored
View File

@@ -31,7 +31,11 @@ export type CheckResult =
export function checkUpdate(appkey: string): Promise<CheckResult>;
export function downloadUpdate(
options: UpdateAvailableResult,
info: UpdateAvailableResult,
eventListeners?: {
onDownloadProgress?: (data: ProgressData) => void;
onUnzipProgress?: (data: ProgressData) => void;
},
): Promise<undefined | string>;
export function switchVersion(hash: string): void;
@@ -56,8 +60,8 @@ export function setCustomEndpoints({
backupQueryUrl?: string;
}): void;
interface ProgressEvent {
interface ProgressData {
hashname: string;
received: number;
total: number;
}

View File

@@ -3,7 +3,7 @@ import {
getCheckUrl,
setCustomEndpoints,
} from './endpoint';
import { NativeAppEventEmitter, NativeModules, Platform } from 'react-native';
import { NativeEventEmitter, NativeModules, Platform } from 'react-native';
export { setCustomEndpoints };
const {
version: v,
@@ -32,12 +32,18 @@ if (Platform.OS === 'android' && !Pushy.isUsingBundleUrl) {
);
}
const eventEmitter = new NativeEventEmitter(Pushy);
if (!uuid) {
uuid = uuidv4();
Pushy.setUuid(uuid);
}
console.log('Pushy uuid: ' + uuid);
function logger(text) {
console.log(`Pushy: ${text}`);
}
logger('uuid: ' + uuid);
/*
Return json:
@@ -77,6 +83,10 @@ export async function checkUpdate(APPKEY, isRetry) {
).toLocaleString()}"之后重试。`,
);
}
if (typeof APPKEY !== 'string') {
throw new Error('未检查到合法的APPKEY请查看update.json文件是否正确生成');
}
logger('checking update');
let resp;
try {
resp = await fetch(getCheckUrl(APPKEY), {
@@ -129,41 +139,62 @@ function checkOperation(op) {
});
}
export async function downloadUpdate(options) {
export async function downloadUpdate(options, eventListeners) {
assertRelease();
if (!options.update) {
return;
}
if (eventListeners) {
if (eventListeners.onDownloadProgress) {
const downloadCallback = eventListeners.onDownloadProgress;
eventEmitter.addListener('RCTPushyDownloadProgress', (progressData) => {
if (progressData.hash === options.hash) {
downloadCallback(progressData);
}
});
}
if (eventListeners.onUnzipProgress) {
const unzipCallback = eventListeners.onUnzipProgress;
eventEmitter.addListener('RCTPushyUnzipProgress', (progressData) => {
if (progressData.hash === options.hash) {
unzipCallback(progressData);
}
});
}
}
if (options.diffUrl) {
logger('downloading diff');
await Pushy.downloadPatchFromPpk({
updateUrl: options.diffUrl,
hashName: options.hash,
originHashName: currentVersion,
});
} else if (options.pdiffUrl) {
logger('downloading pdiff');
await Pushy.downloadPatchFromPackage({
updateUrl: options.pdiffUrl,
hashName: options.hash,
});
}
eventEmitter.removeAllListeners('RCTPushyDownloadProgress');
eventEmitter.removeAllListeners('RCTPushyUnzipProgress');
return options.hash;
}
export function switchVersion(hash) {
assertRelease();
logger('switchVersion');
Pushy.reloadUpdate({ hashName: hash });
}
export function switchVersionLater(hash) {
assertRelease();
logger('switchVersionLater');
Pushy.setNeedUpdate({ hashName: hash });
}
export function markSuccess() {
assertRelease();
logger('markSuccess');
Pushy.markSuccess();
}
NativeAppEventEmitter.addListener('RCTPushyDownloadProgress', (params) => {});
NativeAppEventEmitter.addListener('RCTPushyUnzipProgress', (params) => {});