mirror of
https://github.com/zoriya/react-native-background-downloader.git
synced 2026-05-24 15:08:08 +00:00
refactor js
This commit is contained in:
@@ -9,31 +9,27 @@ let headers = {}
|
||||
RNBackgroundDownloaderEmitter.addListener('downloadProgress', events => {
|
||||
for (const event of events) {
|
||||
const task = tasksMap.get(event.id)
|
||||
if (task)
|
||||
task._onProgress(event.percent, event.written, event.total)
|
||||
task?.onProgress(event.percent, event.written, event.total)
|
||||
}
|
||||
})
|
||||
|
||||
RNBackgroundDownloaderEmitter.addListener('downloadComplete', ({ id, location }) => {
|
||||
const task = tasksMap.get(id)
|
||||
if (task)
|
||||
task._onDone({ location })
|
||||
task?.onDone({ location })
|
||||
|
||||
tasksMap.delete(id)
|
||||
})
|
||||
|
||||
RNBackgroundDownloaderEmitter.addListener('downloadFailed', event => {
|
||||
const task = tasksMap.get(event.id)
|
||||
if (task)
|
||||
task._onError(event.error, event.errorcode)
|
||||
task?.onError(event.error, event.errorcode)
|
||||
|
||||
tasksMap.delete(event.id)
|
||||
})
|
||||
|
||||
RNBackgroundDownloaderEmitter.addListener('downloadBegin', ({ id, expectedBytes, headers }) => {
|
||||
const task = tasksMap.get(id)
|
||||
if (task)
|
||||
task._onBegin({ expectedBytes, headers })
|
||||
task?.onBegin({ expectedBytes, headers })
|
||||
})
|
||||
|
||||
export function setHeaders (h = {}) {
|
||||
|
||||
+29
-29
@@ -2,9 +2,12 @@ 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}`)
|
||||
const type = typeof handler
|
||||
|
||||
if (type !== 'function')
|
||||
throw new TypeError(`[RNBackgroundDownloader] expected argument to be a function, got: ${type}`)
|
||||
}
|
||||
|
||||
export default class DownloadTask {
|
||||
state = 'PENDING'
|
||||
percent = 0
|
||||
@@ -18,65 +21,60 @@ export default class DownloadTask {
|
||||
this.bytesWritten = taskInfo.bytesWritten ?? 0
|
||||
this.totalBytes = taskInfo.totalBytes ?? 0
|
||||
|
||||
if (this.#parseable(taskInfo.metadata))
|
||||
this.metadata = JSON.parse(taskInfo.metadata)
|
||||
this.metadata = this.tryParseJson(taskInfo.metadata)
|
||||
|
||||
if (originalTask) {
|
||||
this._beginHandler = originalTask._beginHandler
|
||||
this._progressHandler = originalTask._progressHandler
|
||||
this._doneHandler = originalTask._doneHandler
|
||||
this._errorHandler = originalTask._errorHandler
|
||||
this.beginHandler = originalTask.beginHandler
|
||||
this.progressHandler = originalTask.progressHandler
|
||||
this.doneHandler = originalTask.doneHandler
|
||||
this.errorHandler = originalTask.errorHandler
|
||||
}
|
||||
}
|
||||
|
||||
begin (handler) {
|
||||
validateHandler(handler)
|
||||
this._beginHandler = handler
|
||||
this.beginHandler = handler
|
||||
return this
|
||||
}
|
||||
|
||||
progress (handler) {
|
||||
validateHandler(handler)
|
||||
this._progressHandler = handler
|
||||
this.progressHandler = handler
|
||||
return this
|
||||
}
|
||||
|
||||
done (handler) {
|
||||
validateHandler(handler)
|
||||
this._doneHandler = handler
|
||||
this.doneHandler = handler
|
||||
return this
|
||||
}
|
||||
|
||||
error (handler) {
|
||||
validateHandler(handler)
|
||||
this._errorHandler = handler
|
||||
this.errorHandler = handler
|
||||
return this
|
||||
}
|
||||
|
||||
_onBegin ({ expectedBytes, headers }) {
|
||||
onBegin ({ expectedBytes, headers }) {
|
||||
this.state = 'DOWNLOADING'
|
||||
if (this._beginHandler)
|
||||
this._beginHandler({ expectedBytes, headers })
|
||||
this?.beginHandler({ expectedBytes, headers })
|
||||
}
|
||||
|
||||
_onProgress (percent, bytesWritten, totalBytes) {
|
||||
onProgress (percent, bytesWritten, totalBytes) {
|
||||
this.percent = percent
|
||||
this.bytesWritten = bytesWritten
|
||||
this.totalBytes = totalBytes
|
||||
if (this._progressHandler)
|
||||
this._progressHandler(percent, bytesWritten, totalBytes)
|
||||
this?.progressHandler(percent, bytesWritten, totalBytes)
|
||||
}
|
||||
|
||||
_onDone ({ location }) {
|
||||
onDone ({ location }) {
|
||||
this.state = 'DONE'
|
||||
if (this._doneHandler)
|
||||
this._doneHandler({ location })
|
||||
this?.doneHandler({ location })
|
||||
}
|
||||
|
||||
_onError (error, errorCode) {
|
||||
onError (error, errorCode) {
|
||||
this.state = 'FAILED'
|
||||
if (this._errorHandler)
|
||||
this._errorHandler(error, errorCode)
|
||||
this?.errorHandler(error, errorCode)
|
||||
}
|
||||
|
||||
pause () {
|
||||
@@ -94,12 +92,14 @@ export default class DownloadTask {
|
||||
RNBackgroundDownloader.stopTask(this.id)
|
||||
}
|
||||
|
||||
#parseable = (element) => {
|
||||
tryParseJson (element) {
|
||||
try {
|
||||
JSON.parse(element)
|
||||
return true
|
||||
} catch (err) {
|
||||
return false
|
||||
if (element)
|
||||
element = JSON.parse(element)
|
||||
return element
|
||||
} catch (e) {
|
||||
console.warn('DownloadTask tryParseJson', e)
|
||||
return null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "react-native-background-downloader",
|
||||
"version": "2.5.0",
|
||||
"version": "2.5.1",
|
||||
"description": "A library for React-Native to help you download large files on iOS and Android both in the foreground and most importantly in the background.",
|
||||
"keywords": [
|
||||
"react-native",
|
||||
|
||||
Reference in New Issue
Block a user