@appzung/react-native-code-push
Version:
React Native plugin for the CodePush service
46 lines (44 loc) • 2.16 kB
JavaScript
import { NativeEventEmitter } from 'react-native';
import { LogLevel } from "../enums/LogLevel.enum.js";
import { LocalPackageImplementation } from "./LocalPackageImplementation.js";
import { NativeRNAppZungCodePushModule } from "./NativeRNAppZungCodePushModule.js";
import { log } from "./utils/log.js";
export class RemotePackageImpl {
constructor(remotePackageData, reportStatusDownload) {
Object.assign(this, remotePackageData);
this.download = async downloadProgressCallback => {
if (!this.downloadUrl) {
throw new Error('Cannot download an update without a download url');
}
let downloadProgressSubscription;
if (downloadProgressCallback) {
const codePushEventEmitter = new NativeEventEmitter(NativeRNAppZungCodePushModule);
// Use event subscription to obtain download progress.
downloadProgressSubscription = codePushEventEmitter.addListener('CodePushDownloadProgress', downloadProgressCallback);
}
// Use the downloaded package info. Native code will save the package info
// so that the client knows what the current package version is.
try {
const {
...updatePackageCopy
} = remotePackageData; // In dev mode, React Native deep freezes any object queued over the bridge
const downloadedPackage = await NativeRNAppZungCodePushModule.downloadUpdate(updatePackageCopy, !!downloadProgressCallback);
if (reportStatusDownload) {
const timeoutPromise = new Promise(resolve => setTimeout(resolve, 250));
const reportPromise = reportStatusDownload({
label: this.label
}).catch(error => {
log(LogLevel.ERROR, `Report download status failed: ${error}`);
});
await Promise.race([timeoutPromise, reportPromise]);
}
return new LocalPackageImplementation(downloadedPackage);
} finally {
downloadProgressSubscription && downloadProgressSubscription.remove();
}
};
}
isPending = false; // A remote package could never be in a pending state
}
//# sourceMappingURL=RemotePackageImplementation.js.map
;