Sync issue fix, added error code, handled enque failure

Sync issue fix, added error code, handled enque failure
This commit is contained in:
sridhar
2019-10-12 10:51:52 +05:30
parent b7452e84a5
commit 9fef9d38c7
6 changed files with 337 additions and 243 deletions
+70 -70
View File
@@ -2,93 +2,93 @@ import { NativeModules } from 'react-native';
const { RNBackgroundDownloader } = NativeModules;
function validateHandler(handler) {
if (!(typeof handler === 'function')) {
throw new TypeError(`[RNBackgroundDownloader] expected argument to be a function, got: ${typeof handler}`);
}
if (!(typeof handler === 'function')) {
throw new TypeError(`[RNBackgroundDownloader] expected argument to be a function, got: ${typeof handler}`);
}
}
export default class DownloadTask {
state = 'PENDING'
percent = 0
bytesWritten = 0
totalBytes = 0
state = 'PENDING'
percent = 0
bytesWritten = 0
totalBytes = 0
constructor(taskInfo) {
if (typeof taskInfo === 'string') {
this.id = taskInfo;
} else {
this.id = taskInfo.id;
this.percent = taskInfo.percent;
this.bytesWritten = taskInfo.bytesWritten;
this.totalBytes = taskInfo.totalBytes;
}
constructor(taskInfo) {
if (typeof taskInfo === 'string') {
this.id = taskInfo;
} else {
this.id = taskInfo.id;
this.percent = taskInfo.percent;
this.bytesWritten = taskInfo.bytesWritten;
this.totalBytes = taskInfo.totalBytes;
}
}
begin(handler) {
validateHandler(handler);
this._beginHandler = handler;
return this;
}
begin(handler) {
validateHandler(handler);
this._beginHandler = handler;
return this;
}
progress(handler) {
validateHandler(handler);
this._progressHandler = handler;
return this;
}
progress(handler) {
validateHandler(handler);
this._progressHandler = handler;
return this;
}
done(handler) {
validateHandler(handler);
this._doneHandler = handler;
return this;
}
done(handler) {
validateHandler(handler);
this._doneHandler = handler;
return this;
}
error(handler) {
validateHandler(handler);
this._errorHandler = handler;
return this;
}
error(handler) {
validateHandler(handler);
this._errorHandler = handler;
return this;
}
_onBegin(expectedBytes) {
this.state = 'DOWNLOADING';
if (this._beginHandler) {
this._beginHandler(expectedBytes);
}
_onBegin(expectedBytes) {
this.state = 'DOWNLOADING';
if (this._beginHandler) {
this._beginHandler(expectedBytes);
}
}
_onProgress(percent, bytesWritten, totalBytes) {
this.percent = percent;
this.bytesWritten = bytesWritten;
this.totalBytes = totalBytes;
if (this._progressHandler) {
this._progressHandler(percent, bytesWritten, totalBytes);
}
_onProgress(percent, bytesWritten, totalBytes) {
this.percent = percent;
this.bytesWritten = bytesWritten;
this.totalBytes = totalBytes;
if (this._progressHandler) {
this._progressHandler(percent, bytesWritten, totalBytes);
}
}
_onDone() {
this.state = 'DONE';
if (this._doneHandler) {
this._doneHandler();
}
_onDone() {
this.state = 'DONE';
if (this._doneHandler) {
this._doneHandler();
}
}
_onError(error) {
this.state = 'FAILED';
if (this._errorHandler) {
this._errorHandler(error);
}
_onError(error, errorCode) {
this.state = 'FAILED';
if (this._errorHandler) {
this._errorHandler(error, errorCode);
}
}
pause() {
this.state = 'PAUSED';
RNBackgroundDownloader.pauseTask(this.id);
}
pause() {
this.state = 'PAUSED';
RNBackgroundDownloader.pauseTask(this.id);
}
resume() {
this.state = 'DOWNLOADING';
RNBackgroundDownloader.resumeTask(this.id);
}
resume() {
this.state = 'DOWNLOADING';
RNBackgroundDownloader.resumeTask(this.id);
}
stop() {
this.state = 'STOPPED';
RNBackgroundDownloader.stopTask(this.id);
}
stop() {
this.state = 'STOPPED';
RNBackgroundDownloader.stopTask(this.id);
}
}