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