Skip to content

Commit c673ee0

Browse files
authored
fix: don't re-download completed downloads on next init() (#38)
1 parent 3ef589b commit c673ee0

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,9 @@ export default class DownloadQueue {
272272
return;
273273
}
274274
await this.reconcileFinishStateWithFile(spec);
275-
return this.start(spec);
275+
if (!spec.finished && spec.createTime > 0) {
276+
this.start(spec);
277+
}
276278
})
277279
);
278280

test/index.spec.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,48 @@ describe("DownloadQueue", () => {
564564
);
565565
});
566566

567+
it("doesn't start downloads for 'finished' specs", async () => {
568+
const queue = new DownloadQueue();
569+
570+
(download as jest.Mock).mockReturnValue(task);
571+
(exists as jest.Mock).mockImplementation(() => true);
572+
573+
await kvfs.write("/mydomain/foo", {
574+
id: "foo",
575+
url: "http://foo.com/a.mp3",
576+
path: `${RNFS.DocumentDirectoryPath}/DownloadQueue/mydomain/foo`,
577+
createTime: Date.now() - 1000,
578+
finished: true,
579+
});
580+
await queue.init({ domain: "mydomain" });
581+
582+
// This test protects against a regression. We used to restart downloads
583+
// even for finished specs.
584+
expect(task.resume).not.toHaveBeenCalled();
585+
expect(download).not.toHaveBeenCalled();
586+
});
587+
588+
it("doesn't start downloads for lazy-deleted specs", async () => {
589+
const queue = new DownloadQueue();
590+
591+
(download as jest.Mock).mockReturnValue(task);
592+
(exists as jest.Mock).mockImplementation(() => true);
593+
594+
await kvfs.write("/mydomain/foo", {
595+
id: "foo",
596+
url: "http://foo.com/a.mp3",
597+
path: `${RNFS.DocumentDirectoryPath}/DownloadQueue/mydomain/foo`,
598+
createTime: -(Date.now() + 1000), // Delete one second into future
599+
finished: false, // Force the issue by simulating a half-downloaded file
600+
});
601+
await queue.init({ domain: "mydomain" });
602+
603+
// This test protects against a regression. We used to restart downloads
604+
// even for finished specs.
605+
expect(task.resume).not.toHaveBeenCalled();
606+
expect(download).not.toHaveBeenCalled();
607+
});
608+
567609
it("enforces netInfo callbacks when activeNetworkTypes is passed", async () => {
568610
const queue = new DownloadQueue();
569611

0 commit comments

Comments
 (0)