Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,12 +266,27 @@ export default class DownloadQueue {
}

// Now start downloads for specs that haven't finished
const specsToDownload = this.specs.filter(
spec => !existingTasks.some(task => task.id === spec.id) && !spec.finished
await Promise.all(
this.specs.map(async spec => {
if (existingTasks.some(task => task.id === spec.id)) {
return;
}
if (spec.finished) {
// Once in a while we think the spec is finished but the file isn't
// on disk. This can happen when XCode installs a new build, and
// sometimes through TestFlight.
const exists = await RNFS.exists(spec.path);

if (exists) {
return;
}

spec.finished = false; // We're not really finished, it seems.
await this.kvfs.write(this.keyFromId(spec.id), spec);
}
return this.start(spec);
})
);
if (specsToDownload.length) {
specsToDownload.forEach(spec => this.start(spec));
}

// Delete any files that don't have a spec
const orphanedFiles = dirFilenames.map(splitFilenameFromExtension).filter(
Expand Down
25 changes: 24 additions & 1 deletion test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ describe("DownloadQueue", () => {
expect(unlink).toHaveBeenCalledTimes(1);
});

it("starts downloads for specs without tasks or files", async () => {
it("starts downloads for unfinished specs without tasks", async () => {
const queue = new DownloadQueue();

(download as jest.Mock).mockReturnValue(task);
Expand All @@ -513,6 +513,29 @@ describe("DownloadQueue", () => {
);
});

// This can happen in cases of TestFlight / XCode installs, somehow, where
// the disk no longer holds files, and yet your specs load just fine (as
// "finished").
it("starts downloads for 'finished' specs without tasks and files", async () => {
const queue = new DownloadQueue();

(download as jest.Mock).mockReturnValue(task);

await kvfs.write("/mydomain/foo", {
id: "foo",
url: "http://foo.com/a.mp3",
path: `${RNFS.DocumentDirectoryPath}/DownloadQueue/mydomain/foo`,
createTime: Date.now() - 1000,
finished: true,
});
await queue.init({ domain: "mydomain" });
expect(task.resume).not.toHaveBeenCalled();

expect(download).toHaveBeenCalledWith(
expect.objectContaining({ id: "foo" })
);
});

it("enforces netInfo callbacks when activeNetworkTypes is passed", async () => {
const queue = new DownloadQueue();

Expand Down