-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
114 lines (102 loc) · 3.48 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { NativeModules, NativeEventEmitter } from 'react-native';
const { RNBackgroundDownloader } = NativeModules;
const RNBackgroundDownloaderEmitter = new NativeEventEmitter(RNBackgroundDownloader);
import DownloadTask from './lib/downloadTask';
const tasksMap = new Map();
let headers = {};
RNBackgroundDownloaderEmitter.addListener('downloadProgress', events => {
for (let event of events) {
let task = tasksMap.get(event.id);
if (task) {
task._onProgress(event.percent, event.written, event.total);
}
}
});
RNBackgroundDownloaderEmitter.addListener('downloadComplete', event => {
let task = tasksMap.get(event.id);
if (task) {
task._onDone(event.location);
}
tasksMap.delete(event.id);
});
RNBackgroundDownloaderEmitter.addListener('downloadFailed', event => {
let task = tasksMap.get(event.id);
if (task) {
task._onError(event, event.errorcode);
}
tasksMap.delete(event.id);
});
RNBackgroundDownloaderEmitter.addListener('downloadBegin', event => {
let task = tasksMap.get(event.id);
if (task) {
task._onBegin(event.expectedBytes);
}
});
export function setHeaders(h = {}) {
if (typeof h !== 'object') {
throw new Error('[RNBackgroundDownloader] headers must be an object');
}
headers = h;
}
export function checkForExistingDownloads() {
return RNBackgroundDownloader.checkForExistingDownloads()
.then(foundTasks => {
return foundTasks.map(taskInfo => {
let task = new DownloadTask(taskInfo);
if (taskInfo.state === RNBackgroundDownloader.TaskRunning) {
task.state = 'DOWNLOADING';
} else if (taskInfo.state === RNBackgroundDownloader.TaskSuspended) {
task.state = 'PAUSED';
} else if (taskInfo.state === RNBackgroundDownloader.TaskCanceling) {
task.stop();
return null;
} else if (taskInfo.state === RNBackgroundDownloader.TaskCompleted) {
if (taskInfo.bytesWritten === taskInfo.totalBytes) {
task.state = 'DONE';
} else {
// IOS completed the download but it was not done.
return null;
}
}
tasksMap.set(taskInfo.id, task);
return task;
}).filter(task => task !== null);
});
}
export function download(options) {
if (!options.id || !options.url || !options.destination) {
throw new Error('[RNBackgroundDownloader] id, url and destination are required');
}
if (options.headers && typeof options.headers === 'object') {
options.headers = {
...headers,
...options.headers
};
} else {
options.headers = headers;
}
RNBackgroundDownloader.download(options);
let task = new DownloadTask(options.id);
tasksMap.set(options.id, task);
return task;
}
export const directories = {
documents: RNBackgroundDownloader.documents
};
export const Network = {
WIFI_ONLY: RNBackgroundDownloader.OnlyWifi,
ALL: RNBackgroundDownloader.AllNetworks
};
export const Priority = {
HIGH: RNBackgroundDownloader.PriorityHigh,
MEDIUM: RNBackgroundDownloader.PriorityNormal,
LOW: RNBackgroundDownloader.PriorityLow
};
export default {
download,
checkForExistingDownloads,
setHeaders,
directories,
Network,
Priority
};