Skip to content

Commit 1ad90b0

Browse files
authored
fix: send begin/done events for revived lazy deletes (#16)
1 parent 47c6924 commit 1ad90b0

File tree

3 files changed

+63
-16
lines changed

3 files changed

+63
-16
lines changed

.vscode/settings.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,8 @@
22
"editor.formatOnSave": true,
33
"[typescript]": {
44
"editor.defaultFormatter": "esbenp.prettier-vscode"
5-
}
5+
},
6+
"cSpell.words": [
7+
"kvfs"
8+
]
69
}

src/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,14 @@ export default class DownloadQueue {
310310
]);
311311
if (!curSpec.finished || !fileExists) {
312312
this.start(curSpec);
313+
} else {
314+
// If we already have the file, and you're reviving it from deletion,
315+
// send "begin" and "done" notifications so that most clients can
316+
// treat it the same as a fresh download.
317+
const fileSpec = await RNFS.stat(curSpec.path);
318+
319+
this.handlers?.onBegin?.(curSpec.url, fileSpec.size);
320+
this.handlers?.onDone?.(curSpec.url, curSpec.path);
313321
}
314322
}
315323
return;

test/index.spec.ts

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
ErrorHandler,
1717
ProgressHandler,
1818
} from "react-native-background-downloader";
19-
import RNFS, { exists, readdir, unlink } from "react-native-fs";
19+
import RNFS, { exists, readdir, stat, unlink } from "react-native-fs";
2020
import DownloadQueue, { DownloadQueueHandlers } from "../src";
2121

2222
jest.mock("@react-native-async-storage/async-storage", () => {
@@ -170,6 +170,7 @@ describe("DownloadQueue", () => {
170170
(exists as jest.Mock).mockReturnValue(false);
171171
(readdir as jest.Mock).mockReturnValue([]);
172172
(unlink as jest.Mock).mockImplementation(() => Promise.resolve());
173+
(stat as jest.Mock).mockReturnValue({ size: 8675309 });
173174

174175
(download as jest.Mock).mockReturnValue(task);
175176
(checkForExistingDownloads as jest.Mock).mockReturnValue([]);
@@ -873,6 +874,47 @@ describe("DownloadQueue", () => {
873874
await queue.addUrl("http://foo.com");
874875
expect(download).toHaveBeenCalledTimes(2);
875876
});
877+
878+
it("should send notifications for revived lazy-deletions", async () => {
879+
const queue = new DownloadQueue();
880+
const handlers: DownloadQueueHandlers = {
881+
onBegin: jest.fn(),
882+
onDone: jest.fn(),
883+
};
884+
885+
Object.assign(task, {
886+
id: "foo",
887+
begin: jest.fn(handler => {
888+
task._begin = handler;
889+
return task;
890+
}),
891+
done: jest.fn(handler => {
892+
task._done = handler;
893+
return task;
894+
}),
895+
});
896+
897+
(exists as jest.Mock).mockResolvedValue(true);
898+
899+
await queue.init({ domain: "mydomain", handlers });
900+
await queue.addUrl("http://foo.com");
901+
902+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
903+
task._begin!("http://foo.com");
904+
expect(handlers.onBegin).toHaveBeenCalledTimes(1);
905+
906+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
907+
await task._done!();
908+
expect(handlers.onDone).toHaveBeenCalledTimes(1);
909+
910+
await queue.removeUrl("http://foo.com", 0);
911+
await queue.addUrl("http://foo.com");
912+
913+
// When we re-add a lazy-deleted download that was already downloaded,
914+
// we expect to get notifications for begin/done again.
915+
expect(handlers.onBegin).toHaveBeenCalledTimes(2);
916+
expect(handlers.onDone).toHaveBeenCalledTimes(2);
917+
});
876918
});
877919

878920
describe("Pause / resume", () => {
@@ -1057,8 +1099,6 @@ describe("DownloadQueue", () => {
10571099
expect(task.pause).toHaveBeenCalledTimes(1); // no change!
10581100
});
10591101

1060-
1061-
10621102
it("should unsubscribe when terminated", async () => {
10631103
const queue = new DownloadQueue();
10641104
const unsubscriber = jest.fn();
@@ -1426,27 +1466,23 @@ describe("DownloadQueue", () => {
14261466
onError: jest.fn(),
14271467
};
14281468
const queue = new DownloadQueue();
1429-
let beginner: BeginHandler;
1430-
let progresser: ProgressHandler;
1431-
let doner: DoneHandler;
1432-
let errorer: ErrorHandler;
14331469

14341470
Object.assign(task, {
14351471
id: "foo",
14361472
begin: jest.fn(handler => {
1437-
beginner = handler;
1473+
task._begin = handler;
14381474
return task;
14391475
}),
14401476
progress: jest.fn(handler => {
1441-
progresser = handler;
1477+
task._progress = handler;
14421478
return task;
14431479
}),
14441480
done: jest.fn(handler => {
1445-
doner = handler;
1481+
task._done = handler;
14461482
return task;
14471483
}),
14481484
error: jest.fn(handler => {
1449-
errorer = handler;
1485+
task._error = handler;
14501486
return task;
14511487
}),
14521488
});
@@ -1455,13 +1491,13 @@ describe("DownloadQueue", () => {
14551491
await queue.addUrl("http://foo.com");
14561492

14571493
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1458-
beginner!({ expectedBytes: 300, headers: {} });
1494+
task._begin!({ expectedBytes: 300, headers: {} });
14591495
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1460-
progresser!(0.5, 500, 1000);
1496+
task._progress!(0.5, 500, 1000);
14611497
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1462-
await doner!();
1498+
await task._done!();
14631499
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1464-
errorer!("foo", 500);
1500+
task._error!("foo", 500);
14651501

14661502
expect(handlers.onBegin).toHaveBeenCalledTimes(1);
14671503
expect(handlers.onProgress).toHaveBeenCalledTimes(1);

0 commit comments

Comments
 (0)