Skip to content

Commit 04ffc5e

Browse files
authored
fix: fixes api calls to underlying downloader (#46)
Because the underlying downloader changed APIs, we needed to handle the fact that task.done() no longer awaits our handler.
1 parent ac5d57f commit 04ffc5e

2 files changed

Lines changed: 101 additions & 71 deletions

File tree

src/index.ts

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -725,38 +725,18 @@ export default class DownloadQueue {
725725
}
726726
this.handlers?.onBegin?.(url, data.expectedBytes);
727727
})
728-
.progress((percent, bytes, total) => {
728+
.progress(({ bytesDownloaded, bytesTotal }) => {
729729
// Bug: https://github.com/kesha-antonov/react-native-background-downloader/issues/23
730730
// See note in begin() above. We can get progress callbacks even without
731731
// begin() (e.g. in the case of resuming a background task upon launch).
732732
if (!this.active) {
733733
task.pause();
734734
}
735-
this.handlers?.onProgress?.(url, percent, bytes, total);
736-
})
737-
.done(async () => {
738-
const spec = this.specs.find(spec => spec.url === url);
739-
740-
if (!spec) {
741-
// This in theory shouldn't ever happen -- basically the downloader
742-
// telling us it's completed the download of a spec we've never heard
743-
// about. But we're being extra careful here not to crash the client
744-
// app if this ever happens.
745-
return;
746-
}
747-
748-
this.removeTask(task.id);
749-
spec.finished = true;
750-
await this.kvfs.write(this.keyFromId(spec.id), spec);
751-
752-
if (Platform.OS === "ios") {
753-
completeHandler(task.id);
754-
}
755-
756-
// Only notify the client once everything has completed successfully and
757-
// our internal state is consistent.
758-
this.handlers?.onDone?.(url, spec.path);
735+
const percent = (bytesDownloaded / bytesTotal) * 100;
736+
this.handlers?.onProgress?.(url, percent, bytesDownloaded, bytesTotal);
759737
})
738+
// eslint-disable-next-line @typescript-eslint/no-misused-promises
739+
.done(async () => await this.doDone(url, task))
760740
.error(error => {
761741
this.removeTask(task.id);
762742
this.handlers?.onError?.(url, error);
@@ -767,6 +747,30 @@ export default class DownloadQueue {
767747
this.tasks.push(task);
768748
}
769749

750+
private async doDone(url: string, task: DownloadTask) {
751+
const spec = this.specs.find(spec => spec.url === url);
752+
753+
if (!spec) {
754+
// This in theory shouldn't ever happen -- basically the downloader
755+
// telling us it's completed the download of a spec we've never heard
756+
// about. But we're being extra careful here not to crash the client
757+
// app if this ever happens.
758+
return;
759+
}
760+
761+
this.removeTask(task.id);
762+
spec.finished = true;
763+
await this.kvfs.write(this.keyFromId(spec.id), spec);
764+
765+
if (Platform.OS === "ios") {
766+
completeHandler(task.id);
767+
}
768+
769+
// Only notify the client once everything has completed successfully and
770+
// our internal state is consistent.
771+
this.handlers?.onDone?.(url, spec.path);
772+
}
773+
770774
private ensureErrorTimerOn() {
771775
if (!this.errorTimer) {
772776
this.errorTimer = setInterval(() => {
@@ -864,10 +868,10 @@ export default class DownloadQueue {
864868
case "DOWNLOADING":
865869
// Since we're already downloading, make sure the client at least
866870
// gets a notification that it's started.
867-
this.handlers?.onBegin?.(spec.url, task.totalBytes);
871+
this.handlers?.onBegin?.(spec.url, task.bytesTotal);
868872
break;
869873
case "PAUSED":
870-
this.handlers?.onBegin?.(spec.url, task.totalBytes);
874+
this.handlers?.onBegin?.(spec.url, task.bytesTotal);
871875
break;
872876
case "DONE":
873877
{
@@ -876,7 +880,7 @@ export default class DownloadQueue {
876880
if (exists) {
877881
spec.finished = true;
878882
await this.kvfs.write(this.keyFromId(spec.id), spec);
879-
this.handlers?.onBegin?.(spec.url, task.totalBytes);
883+
this.handlers?.onBegin?.(spec.url, task.bytesTotal);
880884
this.handlers?.onDone?.(spec.url, spec.path);
881885
shouldAddTask = false;
882886
} else {

0 commit comments

Comments
 (0)