mirror of
https://github.com/zoriya/react-native-background-downloader.git
synced 2025-12-06 06:56:10 +00:00
add method initDownloader
This commit is contained in:
12
README.md
12
README.md
@@ -321,6 +321,18 @@ Resumes a pause download
|
||||
### `stop()`
|
||||
Stops the download for good and removes the file that was written so far
|
||||
|
||||
### `initDownloader(options)`
|
||||
|
||||
Init android downloader with options
|
||||
|
||||
**options**
|
||||
|
||||
An object containing options properties
|
||||
|
||||
| Property | Type | Required | Platforms | Info |
|
||||
| ------------- | ------------------------------------------------ | :------: | :-------: | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `type` | String | | Android | Downloader type: 'parallel' or 'sequential'. Default: 'sequential'. 'parallel' seems to cause lots of ANRs, so be careful |
|
||||
|
||||
## Constants
|
||||
|
||||
### directories
|
||||
|
||||
@@ -19,6 +19,7 @@ NativeModules.RNBackgroundDownloader = {
|
||||
TaskSuspended: 1,
|
||||
TaskCanceling: 2,
|
||||
TaskCompleted: 3,
|
||||
initDownloader: jest.fn(),
|
||||
checkForExistingDownloads: jest.fn().mockImplementation(() => {
|
||||
foundDownloads = [
|
||||
{
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import RNBackgroundDownloader from '../index'
|
||||
import RNBackgroundDownloader, { initDownloader } from '../index'
|
||||
import DownloadTask from '../lib/downloadTask'
|
||||
import { NativeModules, NativeEventEmitter } from 'react-native'
|
||||
|
||||
@@ -129,6 +129,14 @@ test('stop', () => {
|
||||
expect(RNBackgroundDownloaderNative.stopTask).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
test('initDownloader', () => {
|
||||
return RNBackgroundDownloader.initDownloader()
|
||||
.then(res => {
|
||||
expect(RNBackgroundDownloaderNative.initDownloader).toHaveBeenCalled()
|
||||
expect(res).toBe(undefined)
|
||||
})
|
||||
})
|
||||
|
||||
test('checkForExistingDownloads', () => {
|
||||
return RNBackgroundDownloader.checkForExistingDownloads()
|
||||
.then(foundDownloads => {
|
||||
|
||||
@@ -84,22 +84,10 @@ public class RNBackgroundDownloaderModule extends ReactContextBaseJavaModule imp
|
||||
private HashMap<String, WritableMap> progressReports = new HashMap<>();
|
||||
private static Object sharedLock = new Object();
|
||||
|
||||
public RNBackgroundDownloaderModule(ReactApplicationContext reactContext) {
|
||||
public RNBackgroundDownloaderModule(ReactApplicationContext reactContext, ReadableMap options) {
|
||||
super(reactContext);
|
||||
|
||||
OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
|
||||
final Downloader okHttpDownloader = new OkHttpDownloader(okHttpClient,
|
||||
Downloader.FileDownloaderType.PARALLEL);
|
||||
|
||||
loadConfigMap();
|
||||
FetchConfiguration fetchConfiguration = new FetchConfiguration.Builder(this.getReactApplicationContext())
|
||||
.setDownloadConcurrentLimit(4)
|
||||
.setHttpDownloader(okHttpDownloader)
|
||||
.enableRetryOnNetworkGain(true)
|
||||
.setHttpDownloader(new HttpUrlConnectionDownloader(Downloader.FileDownloaderType.PARALLEL))
|
||||
.build();
|
||||
fetch = Fetch.Impl.getInstance(fetchConfiguration);
|
||||
fetch.addListener(this);
|
||||
this.initDownloader();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -155,6 +143,32 @@ public class RNBackgroundDownloaderModule extends ReactContextBaseJavaModule imp
|
||||
return constants;
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void initDownloader(ReadableMap options) {
|
||||
if (fetch) {
|
||||
fetch.close();
|
||||
fetch = null;
|
||||
}
|
||||
|
||||
Downloader downloader = options.getString("androidDownloaderType") == "parallel"
|
||||
? Downloader.FileDownloaderType.PARALLEL
|
||||
: Downloader.FileDownloaderType.SEQUENTIAL;
|
||||
|
||||
OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
|
||||
final Downloader okHttpDownloader = new OkHttpDownloader(okHttpClient,
|
||||
downloader);
|
||||
|
||||
loadConfigMap();
|
||||
FetchConfiguration fetchConfiguration = new FetchConfiguration.Builder(this.getReactApplicationContext())
|
||||
.setDownloadConcurrentLimit(4)
|
||||
.setHttpDownloader(okHttpDownloader)
|
||||
.enableRetryOnNetworkGain(true)
|
||||
.setHttpDownloader(new HttpUrlConnectionDownloader(downloader))
|
||||
.build();
|
||||
fetch = Fetch.Impl.getInstance(fetchConfiguration);
|
||||
fetch.addListener(this);
|
||||
}
|
||||
|
||||
private void removeFromMaps(int requestId) {
|
||||
synchronized(sharedLock) {
|
||||
RNBGDTaskConfig config = requestIdToConfig.get(requestId);
|
||||
|
||||
7
index.d.ts
vendored
7
index.d.ts
vendored
@@ -76,6 +76,11 @@ export interface DownloadTask {
|
||||
export type CheckForExistingDownloads = () => Promise<DownloadTask[]>;
|
||||
export type EnsureDownloadsAreRunning = () => Promise<void>;
|
||||
|
||||
export interface InitDownloaderOptions {
|
||||
type?: 'parallel' | 'sequential' | null;
|
||||
}
|
||||
export type InitDownloader = (options: InitDownloaderOptions) => Promise<void>;
|
||||
|
||||
export interface DownloadOption {
|
||||
id: string;
|
||||
url: string;
|
||||
@@ -104,6 +109,7 @@ export interface Priority {
|
||||
export const setHeaders: SetHeaders;
|
||||
export const checkForExistingDownloads: CheckForExistingDownloads;
|
||||
export const ensureDownloadsAreRunning: EnsureDownloadsAreRunning;
|
||||
export const initDownloader: InitDownloader;
|
||||
export const download: Download;
|
||||
export const completeHandler: CompleteHandler;
|
||||
export const directories: Directories;
|
||||
@@ -112,6 +118,7 @@ export const Priority: Priority;
|
||||
|
||||
export interface RNBackgroundDownloader {
|
||||
setHeaders: SetHeaders;
|
||||
initDownloader: InitDownloader;
|
||||
checkForExistingDownloads: CheckForExistingDownloads;
|
||||
ensureDownloadsAreRunning: EnsureDownloadsAreRunning;
|
||||
download: Download;
|
||||
|
||||
7
index.ts
7
index.ts
@@ -1,4 +1,4 @@
|
||||
import { NativeModules, NativeEventEmitter } from 'react-native'
|
||||
import { NativeModules, NativeEventEmitter, Platform } from 'react-native'
|
||||
import DownloadTask from './lib/DownloadTask'
|
||||
|
||||
const { RNBackgroundDownloader } = NativeModules
|
||||
@@ -40,6 +40,11 @@ export function setHeaders (h = {}) {
|
||||
headers = h
|
||||
}
|
||||
|
||||
export function initDownloader (options = {}) {
|
||||
if (Platform.OS === 'android')
|
||||
RNBackgroundDownloader.initDownloader(options)
|
||||
}
|
||||
|
||||
export function checkForExistingDownloads () {
|
||||
return RNBackgroundDownloader.checkForExistingDownloads()
|
||||
.then(foundTasks => {
|
||||
|
||||
24
package.json
24
package.json
@@ -56,28 +56,28 @@
|
||||
]
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.20.12",
|
||||
"@babel/eslint-parser": "^7.19.1",
|
||||
"@babel/preset-env": "^7.20.2",
|
||||
"@babel/runtime": "^7.20.13",
|
||||
"@babel/core": "^7.22.9",
|
||||
"@babel/eslint-parser": "^7.22.9",
|
||||
"@babel/preset-env": "^7.22.9",
|
||||
"@babel/runtime": "^7.22.6",
|
||||
"@react-native-community/eslint-config": "^3.2.0",
|
||||
"babel-jest": "^29.4.1",
|
||||
"eslint": "8.33.0",
|
||||
"eslint-config-standard": "^17.0.0",
|
||||
"babel-jest": "^29.6.1",
|
||||
"eslint": "8.44.0",
|
||||
"eslint-config-standard": "^17.1.0",
|
||||
"eslint-config-standard-jsx": "^11.0.0",
|
||||
"eslint-plugin-import": "^2.27.5",
|
||||
"eslint-plugin-n": "^15.6.1",
|
||||
"eslint-plugin-n": "^16.0.1",
|
||||
"eslint-plugin-node": "^11.1.0",
|
||||
"eslint-plugin-promise": "^6.1.1",
|
||||
"eslint-plugin-react": "^7.32.2",
|
||||
"eslint-plugin-react-hooks": "^4.6.0",
|
||||
"eslint-plugin-standard": "^5.0.0",
|
||||
"immer": "^9.0.19",
|
||||
"jest": "^29.4.1",
|
||||
"immer": "^10.0.2",
|
||||
"jest": "^29.6.1",
|
||||
"lint-staged": ">=13",
|
||||
"metro-react-native-babel-preset": "^0.74.1",
|
||||
"metro-react-native-babel-preset": "^0.77.0",
|
||||
"react": "18.2.0",
|
||||
"react-native": "0.71.1",
|
||||
"react-native": "0.72.3",
|
||||
"react-native-fs": "^2.20.0",
|
||||
"react-native-vector-icons": "^9.2.0",
|
||||
"react-test-renderer": "18.2.0"
|
||||
|
||||
Reference in New Issue
Block a user