mirror of
https://github.com/zoriya/react-native-background-downloader.git
synced 2025-12-06 06:56:10 +00:00
77 lines
1.8 KiB
JavaScript
77 lines
1.8 KiB
JavaScript
import { NativeModules } from 'react-native';
|
|
const { RNBackgroundDownload } = NativeModules;
|
|
|
|
export default class DownloadTask {
|
|
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;
|
|
}
|
|
}
|
|
|
|
begin(handler) {
|
|
this._beginHandler = handler;
|
|
return this;
|
|
}
|
|
|
|
progress(handler) {
|
|
this._progressHandler = handler;
|
|
return this;
|
|
}
|
|
|
|
done(handler) {
|
|
this._doneHandler = handler;
|
|
return this;
|
|
}
|
|
|
|
error(handler) {
|
|
this._errorHandler = handler;
|
|
return this;
|
|
}
|
|
|
|
_onBegin(expectedBytes) {
|
|
this.state = 'DOWNLOADING';
|
|
this._beginHandler && this._beginHandler(expectedBytes);
|
|
}
|
|
|
|
_onProgress(percent, bytesWritten, totalBytes) {
|
|
this.percent = percent;
|
|
this.bytesWritten = bytesWritten;
|
|
this.totalBytes = totalBytes;
|
|
this._progressHandler && this._progressHandler(percent, bytesWritten, totalBytes);
|
|
}
|
|
|
|
_onDone() {
|
|
this.state = 'DONE';
|
|
this._doneHandler && this._doneHandler();
|
|
}
|
|
|
|
_onError(error) {
|
|
this.state = 'FAILED';
|
|
this._errorHandler && this._errorHandler(error);
|
|
}
|
|
|
|
pause() {
|
|
this.state = 'PAUSED';
|
|
RNBackgroundDownload.puaseTask(this.id);
|
|
}
|
|
|
|
resume() {
|
|
this.state = 'DOWNLOADING';
|
|
RNBackgroundDownload.resumeTask(this.id);
|
|
}
|
|
|
|
stop() {
|
|
this.state = 'STOPPED';
|
|
RNBackgroundDownload.stopTask(this.id);
|
|
}
|
|
} |