mirror of
https://github.com/zoriya/react-native-background-downloader.git
synced 2026-06-03 02:23:36 +00:00
improve typescript
This commit is contained in:
Vendored
+11
-4
@@ -13,11 +13,18 @@ type SetHeaders = (h: DownloadHeaders) => void;
|
||||
|
||||
export interface TaskInfoObject {
|
||||
id: string;
|
||||
percent: number;
|
||||
bytesWritten: number;
|
||||
totalBytes: number;
|
||||
metadata: object | string;
|
||||
|
||||
percent?: number;
|
||||
bytesWritten?: number;
|
||||
totalBytes?: number;
|
||||
|
||||
beginHandler?: Function;
|
||||
progressHandler?: Function;
|
||||
doneHandler?: Function;
|
||||
errorHandler?: Function;
|
||||
}
|
||||
export type TaskInfo = string | TaskInfoObject;
|
||||
export type TaskInfo = TaskInfoObject;
|
||||
|
||||
export interface BeginHandlerObject {
|
||||
expectedBytes: number;
|
||||
|
||||
+23
-9
@@ -1,5 +1,6 @@
|
||||
import { NativeModules, NativeEventEmitter } from 'react-native'
|
||||
import DownloadTask from './lib/downloadTask'
|
||||
import DownloadTask from './lib/DownloadTask'
|
||||
|
||||
const { RNBackgroundDownloader } = NativeModules
|
||||
const RNBackgroundDownloaderEmitter = new NativeEventEmitter(RNBackgroundDownloader)
|
||||
|
||||
@@ -43,7 +44,9 @@ export function checkForExistingDownloads () {
|
||||
return RNBackgroundDownloader.checkForExistingDownloads()
|
||||
.then(foundTasks => {
|
||||
return foundTasks.map(taskInfo => {
|
||||
// SECOND ARGUMENT RE-ASSIGNS EVENT HANDLERS
|
||||
const task = new DownloadTask(taskInfo, tasksMap.get(taskInfo.id))
|
||||
|
||||
if (taskInfo.state === RNBackgroundDownloader.TaskRunning) {
|
||||
task.state = 'DOWNLOADING'
|
||||
} else if (taskInfo.state === RNBackgroundDownloader.TaskSuspended) {
|
||||
@@ -79,22 +82,33 @@ export function completeHandler (jobId) {
|
||||
return RNBackgroundDownloader.completeHandler(jobId)
|
||||
}
|
||||
|
||||
export function download (options) {
|
||||
type DownloadOptions = {
|
||||
id: string,
|
||||
url: string,
|
||||
destination: string,
|
||||
headers?: object,
|
||||
metadata?: object,
|
||||
}
|
||||
|
||||
export function download (options : DownloadOptions) {
|
||||
if (!options.id || !options.url || !options.destination)
|
||||
throw new Error('[RNBackgroundDownloader] id, url and destination are required')
|
||||
|
||||
options.headers = { ...headers, ...(options.headers || {}) }
|
||||
|
||||
options.metadata = JSON.stringify(
|
||||
options.metadata && typeof options.metadata === 'object'
|
||||
? options.metadata
|
||||
: {}
|
||||
)
|
||||
if (!(options.metadata && typeof options.metadata === 'object'))
|
||||
options.metadata = {}
|
||||
|
||||
const task = new DownloadTask({ id: options.id, metadata: options.metadata })
|
||||
const task = new DownloadTask({
|
||||
id: options.id,
|
||||
metadata: options.metadata,
|
||||
})
|
||||
tasksMap.set(options.id, task)
|
||||
|
||||
RNBackgroundDownloader.download(options)
|
||||
RNBackgroundDownloader.download({
|
||||
...options,
|
||||
metadata: JSON.stringify(options.metadata),
|
||||
})
|
||||
|
||||
return task
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
import { NativeModules } from 'react-native'
|
||||
import { TaskInfo } from '..'
|
||||
|
||||
const { RNBackgroundDownloader } = NativeModules
|
||||
|
||||
function validateHandler (handler) {
|
||||
@@ -9,13 +11,15 @@ function validateHandler (handler) {
|
||||
}
|
||||
|
||||
export default class DownloadTask {
|
||||
id = ''
|
||||
state = 'PENDING'
|
||||
metadata = {}
|
||||
|
||||
percent = 0
|
||||
bytesWritten = 0
|
||||
totalBytes = 0
|
||||
metadata = {}
|
||||
|
||||
constructor (taskInfo, originalTask) {
|
||||
constructor (taskInfo: TaskInfo, originalTask?: TaskInfo) {
|
||||
this.id = taskInfo.id
|
||||
this.percent = taskInfo.percent ?? 0
|
||||
this.bytesWritten = taskInfo.bytesWritten ?? 0
|
||||
Reference in New Issue
Block a user