Skip to content

Commit 5275e77

Browse files
authored
feat: upgrade to @kesha-antonov/react-native-background-downloader 2.6.9 (#34)
BREAKING CHANGE: This includes implementing its new way of restoring downloads upon app restart. We've moved to requiring "@kesha-antonov/react-native-background-downloader" as opposed to pulling it directly from GitHub.
1 parent 82dfd8f commit 5275e77

File tree

5 files changed

+100
-61
lines changed

5 files changed

+100
-61
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Automatically download files from urls, even in the background, and keep them lo
2020
## Install
2121

2222
First install peer dependencies:
23-
* [react-native-background-downloader](https://github.com/kesha-antonov/react-native-background-downloader#readme). Be sure to follow the sneakily-hidden [extra iOS step for AppDelegate.m](https://github.com/kesha-antonov/react-native-background-downloader#ios---extra-mandatory-step) or else your background tasks will be canceled by the OS.
23+
* [@kesha-antonov/react-native-background-downloader](https://github.com/kesha-antonov/react-native-background-downloader#readme). Be sure to follow the sneakily-hidden [extra iOS step for AppDelegate.m](https://github.com/kesha-antonov/react-native-background-downloader#ios---extra-mandatory-step) or else your background tasks will be canceled by the OS.
2424
* [react-native-fs](https://github.com/itinance/react-native-fs#readme)
2525
* [@react-native-async-storage/async-storage](https://github.com/react-native-async-storage/async-storage#readme)
2626

package-lock.json

Lines changed: 32 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@
6969
"typescript": "^4.2.4"
7070
},
7171
"peerDependencies": {
72-
"@react-native-async-storage/async-storage": "github:react-native-async-storage/async-storage",
73-
"react-native-background-downloader": "github:kesha-antonov/react-native-background-downloader",
72+
"@kesha-antonov/react-native-background-downloader": "^2.6.9",
73+
"@react-native-async-storage/async-storage": "^1.17.11",
7474
"react-native-fs": "^2.20.0"
7575
},
7676
"config": {

src/index.ts

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import AsyncStorage from "@react-native-async-storage/async-storage";
2-
import KeyValueFileSystem from "key-value-file-system";
3-
import { Platform } from "react-native";
41
import {
52
checkForExistingDownloads,
63
completeHandler,
74
download,
85
DownloadTask,
9-
} from "react-native-background-downloader";
6+
ensureDownloadsAreRunning,
7+
} from "@kesha-antonov/react-native-background-downloader";
8+
import AsyncStorage from "@react-native-async-storage/async-storage";
9+
import KeyValueFileSystem from "key-value-file-system";
10+
import { Platform } from "react-native";
1011
import RNFS from "react-native-fs";
1112
import uuid from "react-uuid";
1213

@@ -252,7 +253,17 @@ export default class DownloadQueue {
252253
this.isPausedByUser = !startActive;
253254

254255
// First revive tasks that were working in the background
255-
await Promise.all(existingTasks.map(task => this.reviveTask(task)));
256+
if (existingTasks.length > 0) {
257+
await Promise.all(existingTasks.map(task => this.reviveTask(task)));
258+
await ensureDownloadsAreRunning();
259+
if (!this.active) {
260+
// ensureDownloadsAreRunning forces all un-stopped downloads to start.
261+
// So we'll need to stop them again if !active.
262+
existingTasks
263+
.filter(task => task.state === "DOWNLOADING")
264+
.forEach(task => void task.pause());
265+
}
266+
}
256267

257268
// Now start downloads for specs that haven't finished
258269
const specsToDownload = this.specs.filter(
@@ -796,15 +807,9 @@ export default class DownloadQueue {
796807
// Since we're already downloading, make sure the client at least
797808
// gets a notification that it's started.
798809
this.handlers?.onBegin?.(spec.url, task.totalBytes);
799-
if (!this.active) {
800-
task.pause();
801-
}
802810
break;
803811
case "PAUSED":
804812
this.handlers?.onBegin?.(spec.url, task.totalBytes);
805-
if (this.active) {
806-
task.resume(); // Assuming checkForExistingDownloads() hasn't already
807-
}
808813
break;
809814
case "DONE":
810815
this.handlers?.onBegin?.(spec.url, task.totalBytes);
@@ -828,7 +833,14 @@ export default class DownloadQueue {
828833
}
829834

830835
if (shouldAddTask) {
836+
// Downloader docs say to pause tasks before reattaching our handlers
837+
task.pause();
831838
this.addTask(spec.url, task);
839+
} else {
840+
// According to downloader docs, we must either stop or pause revived
841+
// tasks because ensureTasksRunning() will unpause any download we
842+
// didn't explicitly stop (!)
843+
task.stop();
832844
}
833845
} else {
834846
if (["DOWNLOADING", "PAUSED"].includes(task.state)) {
@@ -920,4 +932,3 @@ function roundToNextMinute(timestamp: number) {
920932
function basePath() {
921933
return `${RNFS.DocumentDirectoryPath}/DownloadQueue`;
922934
}
923-

0 commit comments

Comments
 (0)