Fix download event and remove unzip event
This commit is contained in:
parent
4ba3f25972
commit
f626cc1933
@ -97,6 +97,7 @@ class DownloadTask extends AsyncTask<DownloadTaskParams, long[], Void> {
|
||||
|
||||
long bytesRead = 0;
|
||||
long received = 0;
|
||||
int currentPercentage = 0;
|
||||
while ((bytesRead = source.read(sink.buffer(), DOWNLOAD_CHUNK_SIZE)) != -1) {
|
||||
received += bytesRead;
|
||||
sink.emit();
|
||||
@ -104,11 +105,16 @@ class DownloadTask extends AsyncTask<DownloadTaskParams, long[], Void> {
|
||||
Log.d("RNUpdate", "Progress " + received + "/" + contentLength);
|
||||
}
|
||||
|
||||
publishProgress(new long[]{received, contentLength});
|
||||
int percentage = (int)(received * 100.0 / contentLength + 0.5);
|
||||
if (percentage > currentPercentage) {
|
||||
currentPercentage = percentage;
|
||||
publishProgress(new long[]{received, contentLength});
|
||||
}
|
||||
}
|
||||
if (received != contentLength) {
|
||||
throw new Error("Unexpected eof while reading ppk");
|
||||
throw new Error("Unexpected eof while reading downloaded update");
|
||||
}
|
||||
publishProgress(new long[]{received, contentLength});
|
||||
sink.writeAll(source);
|
||||
sink.close();
|
||||
|
||||
@ -123,7 +129,7 @@ class DownloadTask extends AsyncTask<DownloadTaskParams, long[], Void> {
|
||||
WritableMap params = Arguments.createMap();
|
||||
params.putDouble("received", (values[0][0]));
|
||||
params.putDouble("total", (values[0][1]));
|
||||
params.putString("hashname", this.hash);
|
||||
params.putString("hash", this.hash);
|
||||
sendEvent("RCTPushyDownloadProgress", params);
|
||||
|
||||
}
|
||||
|
@ -43,8 +43,8 @@ static NSString * const ERROR_FILE_OPERATION = @"file operation error";
|
||||
|
||||
// event def
|
||||
static NSString * const EVENT_PROGRESS_DOWNLOAD = @"RCTPushyDownloadProgress";
|
||||
static NSString * const EVENT_PROGRESS_UNZIP = @"RCTPushyUnzipProgress";
|
||||
static NSString * const PARAM_PROGRESS_HASHNAME = @"hashname";
|
||||
// static NSString * const EVENT_PROGRESS_UNZIP = @"RCTPushyUnzipProgress";
|
||||
static NSString * const PARAM_PROGRESS_HASHNAME = @"hash";
|
||||
static NSString * const PARAM_PROGRESS_RECEIVED = @"received";
|
||||
static NSString * const PARAM_PROGRESS_TOTAL = @"total";
|
||||
|
||||
@ -305,7 +305,10 @@ RCT_EXPORT_METHOD(markSuccess)
|
||||
#pragma mark - private
|
||||
- (NSArray<NSString *> *)supportedEvents
|
||||
{
|
||||
return @[EVENT_PROGRESS_DOWNLOAD, EVENT_PROGRESS_UNZIP];
|
||||
return @[
|
||||
EVENT_PROGRESS_DOWNLOAD,
|
||||
// EVENT_PROGRESS_UNZIP
|
||||
];
|
||||
}
|
||||
|
||||
// Will be called when this module's first listener is added.
|
||||
@ -362,14 +365,14 @@ RCT_EXPORT_METHOD(markSuccess)
|
||||
RCTLogInfo(@"RCTPushy -- unzip file %@", zipFilePath);
|
||||
NSString *unzipFilePath = [dir stringByAppendingPathComponent:hashName];
|
||||
[self->_fileManager unzipFileAtPath:zipFilePath toDestination:unzipFilePath progressHandler:^(NSString *entry,long entryNumber, long total) {
|
||||
if (self->hasListeners) {
|
||||
[self sendEventWithName:EVENT_PROGRESS_UNZIP
|
||||
body:@{
|
||||
PARAM_PROGRESS_HASHNAME:hashName,
|
||||
PARAM_PROGRESS_RECEIVED:[NSNumber numberWithLong:entryNumber],
|
||||
PARAM_PROGRESS_TOTAL:[NSNumber numberWithLong:total]
|
||||
}];
|
||||
}
|
||||
// if (self->hasListeners) {
|
||||
// [self sendEventWithName:EVENT_PROGRESS_UNZIP
|
||||
// body:@{
|
||||
// PARAM_PROGRESS_HASHNAME:hashName,
|
||||
// PARAM_PROGRESS_RECEIVED:[NSNumber numberWithLong:entryNumber],
|
||||
// PARAM_PROGRESS_TOTAL:[NSNumber numberWithLong:total]
|
||||
// }];
|
||||
// }
|
||||
|
||||
} completionHandler:^(NSString *path, BOOL succeeded, NSError *error) {
|
||||
dispatch_async(self->_methodQueue, ^{
|
||||
|
1
lib/index.d.ts
vendored
1
lib/index.d.ts
vendored
@ -34,7 +34,6 @@ export function downloadUpdate(
|
||||
info: UpdateAvailableResult,
|
||||
eventListeners?: {
|
||||
onDownloadProgress?: (data: ProgressData) => void;
|
||||
onUnzipProgress?: (data: ProgressData) => void;
|
||||
},
|
||||
): Promise<undefined | string>;
|
||||
|
||||
|
@ -153,14 +153,6 @@ export async function downloadUpdate(options, eventListeners) {
|
||||
}
|
||||
});
|
||||
}
|
||||
if (eventListeners.onUnzipProgress) {
|
||||
const unzipCallback = eventListeners.onUnzipProgress;
|
||||
eventEmitter.addListener('RCTPushyUnzipProgress', (progressData) => {
|
||||
if (progressData.hash === options.hash) {
|
||||
unzipCallback(progressData);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
if (options.diffUrl) {
|
||||
logger('downloading diff');
|
||||
@ -177,7 +169,6 @@ export async function downloadUpdate(options, eventListeners) {
|
||||
});
|
||||
}
|
||||
eventEmitter.removeAllListeners('RCTPushyDownloadProgress');
|
||||
eventEmitter.removeAllListeners('RCTPushyUnzipProgress');
|
||||
return options.hash;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user