mirror of
https://gitcode.com/gh_mirrors/re/react-native-pushy.git
synced 2025-11-01 13:53:11 +08:00
Implement download and install apk
This commit is contained in:
10
lib/index.d.ts
vendored
10
lib/index.d.ts
vendored
@@ -43,6 +43,14 @@ export function switchVersionLater(hash: string): void;
|
||||
|
||||
export function markSuccess(): void;
|
||||
|
||||
export async function downloadAndInstallApk({
|
||||
url,
|
||||
onDownloadProgress,
|
||||
}: {
|
||||
url: string;
|
||||
onDownloadProgress?: (data: ProgressData) => void;
|
||||
}): void;
|
||||
|
||||
/**
|
||||
* @param {string} main - The main api endpoint
|
||||
* @param {string[]} [backups] - The back up endpoints.
|
||||
@@ -55,7 +63,7 @@ export function setCustomEndpoints({
|
||||
backupQueryUrl,
|
||||
}: {
|
||||
main: string;
|
||||
backUps?: string[];
|
||||
backups?: string[];
|
||||
backupQueryUrl?: string;
|
||||
}): void;
|
||||
|
||||
|
||||
24
lib/index.js
24
lib/index.js
@@ -143,10 +143,11 @@ export async function downloadUpdate(options, eventListeners) {
|
||||
if (!options.update) {
|
||||
return;
|
||||
}
|
||||
let progressHandler;
|
||||
if (eventListeners) {
|
||||
if (eventListeners.onDownloadProgress) {
|
||||
const downloadCallback = eventListeners.onDownloadProgress;
|
||||
eventEmitter.addListener('RCTPushyDownloadProgress', (progressData) => {
|
||||
progressHandler = eventEmitter.addListener('RCTPushyDownloadProgress', (progressData) => {
|
||||
if (progressData.hash === options.hash) {
|
||||
downloadCallback(progressData);
|
||||
}
|
||||
@@ -167,7 +168,7 @@ export async function downloadUpdate(options, eventListeners) {
|
||||
hash: options.hash,
|
||||
});
|
||||
}
|
||||
eventEmitter.removeAllListeners('RCTPushyDownloadProgress');
|
||||
progressHandler && progressHandler.remove();
|
||||
return options.hash;
|
||||
}
|
||||
|
||||
@@ -188,3 +189,22 @@ export function markSuccess() {
|
||||
logger('markSuccess');
|
||||
Pushy.markSuccess();
|
||||
}
|
||||
|
||||
export async function downloadAndInstallApk({ url, onDownloadProgress }) {
|
||||
logger('downloadAndInstallApk');
|
||||
let hash = Date.now().toString();
|
||||
let progressHandler;
|
||||
if (onDownloadProgress) {
|
||||
progressHandler = eventEmitter.addListener('RCTPushyDownloadProgress', (progressData) => {
|
||||
if (progressData.hash === hash) {
|
||||
onDownloadProgress(progressData);
|
||||
}
|
||||
});
|
||||
}
|
||||
await Pushy.downloadAndInstallApk({
|
||||
url,
|
||||
target: 'update.apk',
|
||||
hash,
|
||||
});
|
||||
progressHandler && progressHandler.remove();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user