refactor js

This commit is contained in:
Kesha Antonov
2023-01-29 20:50:01 +03:00
parent 8e7dd68041
commit 92e44e64e3
3 changed files with 34 additions and 38 deletions
+4 -8
View File
@@ -9,31 +9,27 @@ let headers = {}
RNBackgroundDownloaderEmitter.addListener('downloadProgress', events => { RNBackgroundDownloaderEmitter.addListener('downloadProgress', events => {
for (const event of events) { for (const event of events) {
const task = tasksMap.get(event.id) 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 }) => { RNBackgroundDownloaderEmitter.addListener('downloadComplete', ({ id, location }) => {
const task = tasksMap.get(id) const task = tasksMap.get(id)
if (task) task?.onDone({ location })
task._onDone({ location })
tasksMap.delete(id) tasksMap.delete(id)
}) })
RNBackgroundDownloaderEmitter.addListener('downloadFailed', event => { RNBackgroundDownloaderEmitter.addListener('downloadFailed', event => {
const task = tasksMap.get(event.id) const task = tasksMap.get(event.id)
if (task) task?.onError(event.error, event.errorcode)
task._onError(event.error, event.errorcode)
tasksMap.delete(event.id) tasksMap.delete(event.id)
}) })
RNBackgroundDownloaderEmitter.addListener('downloadBegin', ({ id, expectedBytes, headers }) => { RNBackgroundDownloaderEmitter.addListener('downloadBegin', ({ id, expectedBytes, headers }) => {
const task = tasksMap.get(id) const task = tasksMap.get(id)
if (task) task?.onBegin({ expectedBytes, headers })
task._onBegin({ expectedBytes, headers })
}) })
export function setHeaders (h = {}) { export function setHeaders (h = {}) {
+29 -29
View File
@@ -2,9 +2,12 @@ import { NativeModules } from 'react-native'
const { RNBackgroundDownloader } = NativeModules const { RNBackgroundDownloader } = NativeModules
function validateHandler (handler) { function validateHandler (handler) {
if (!(typeof handler === 'function')) const type = typeof handler
throw new TypeError(`[RNBackgroundDownloader] expected argument to be a function, got: ${typeof handler}`)
if (type !== 'function')
throw new TypeError(`[RNBackgroundDownloader] expected argument to be a function, got: ${type}`)
} }
export default class DownloadTask { export default class DownloadTask {
state = 'PENDING' state = 'PENDING'
percent = 0 percent = 0
@@ -18,65 +21,60 @@ export default class DownloadTask {
this.bytesWritten = taskInfo.bytesWritten ?? 0 this.bytesWritten = taskInfo.bytesWritten ?? 0
this.totalBytes = taskInfo.totalBytes ?? 0 this.totalBytes = taskInfo.totalBytes ?? 0
if (this.#parseable(taskInfo.metadata)) this.metadata = this.tryParseJson(taskInfo.metadata)
this.metadata = JSON.parse(taskInfo.metadata)
if (originalTask) { if (originalTask) {
this._beginHandler = originalTask._beginHandler this.beginHandler = originalTask.beginHandler
this._progressHandler = originalTask._progressHandler this.progressHandler = originalTask.progressHandler
this._doneHandler = originalTask._doneHandler this.doneHandler = originalTask.doneHandler
this._errorHandler = originalTask._errorHandler this.errorHandler = originalTask.errorHandler
} }
} }
begin (handler) { begin (handler) {
validateHandler(handler) validateHandler(handler)
this._beginHandler = handler this.beginHandler = handler
return this return this
} }
progress (handler) { progress (handler) {
validateHandler(handler) validateHandler(handler)
this._progressHandler = handler this.progressHandler = handler
return this return this
} }
done (handler) { done (handler) {
validateHandler(handler) validateHandler(handler)
this._doneHandler = handler this.doneHandler = handler
return this return this
} }
error (handler) { error (handler) {
validateHandler(handler) validateHandler(handler)
this._errorHandler = handler this.errorHandler = handler
return this return this
} }
_onBegin ({ expectedBytes, headers }) { onBegin ({ expectedBytes, headers }) {
this.state = 'DOWNLOADING' 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.percent = percent
this.bytesWritten = bytesWritten this.bytesWritten = bytesWritten
this.totalBytes = totalBytes this.totalBytes = totalBytes
if (this._progressHandler) this?.progressHandler(percent, bytesWritten, totalBytes)
this._progressHandler(percent, bytesWritten, totalBytes)
} }
_onDone ({ location }) { onDone ({ location }) {
this.state = 'DONE' this.state = 'DONE'
if (this._doneHandler) this?.doneHandler({ location })
this._doneHandler({ location })
} }
_onError (error, errorCode) { onError (error, errorCode) {
this.state = 'FAILED' this.state = 'FAILED'
if (this._errorHandler) this?.errorHandler(error, errorCode)
this._errorHandler(error, errorCode)
} }
pause () { pause () {
@@ -94,12 +92,14 @@ export default class DownloadTask {
RNBackgroundDownloader.stopTask(this.id) RNBackgroundDownloader.stopTask(this.id)
} }
#parseable = (element) => { tryParseJson (element) {
try { try {
JSON.parse(element) if (element)
return true element = JSON.parse(element)
} catch (err) { return element
return false } catch (e) {
console.warn('DownloadTask tryParseJson', e)
return null
} }
} }
} }
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "react-native-background-downloader", "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.", "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": [ "keywords": [
"react-native", "react-native",