Skip to content

Commit 5623cde

Browse files
authored
fix: optimize setActiveNetworkTypes to not do anything when types haven't changed (#31)
1 parent 409c875 commit 5623cde

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,13 @@ export default class DownloadQueue {
652652
async setActiveNetworkTypes(types: string[]): Promise<void> {
653653
this.verifyInitialized();
654654

655+
if (
656+
this.activeNetworkTypes.length === types.length &&
657+
this.activeNetworkTypes.every(type => types.includes(type))
658+
) {
659+
return;
660+
}
661+
655662
this.activeNetworkTypes = types;
656663
if (this.netInfoFetchState) {
657664
const state = await this.netInfoFetchState();

test/index.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1558,6 +1558,26 @@ describe("DownloadQueue", () => {
15581558
await expect(queue.setActiveNetworkTypes(["wifi"])).rejects.toThrow();
15591559
});
15601560

1561+
1562+
it("should do nothing when setting same active network types", async () => {
1563+
const queue = new DownloadQueue();
1564+
1565+
await queue.init({
1566+
domain: "mydomain",
1567+
activeNetworkTypes: ["wifi", "ethernet"],
1568+
netInfoAddEventListener: addEventListener,
1569+
netInfoFetchState: fetch,
1570+
});
1571+
expect(addEventListener).toHaveBeenCalledTimes(1);
1572+
expect(fetch).toHaveBeenCalledTimes(1);
1573+
1574+
await queue.setActiveNetworkTypes(["ethernet", "wifi"]);
1575+
expect(fetch).toHaveBeenCalledTimes(1); // no change
1576+
1577+
await queue.setActiveNetworkTypes(["ethernet", "wifi", "cellular"]);
1578+
expect(fetch).toHaveBeenCalledTimes(2);
1579+
});
1580+
15611581
it("should update activeNetworkTypes to current conditions", async () => {
15621582
const queue = new DownloadQueue();
15631583
const state = createNetState(true);

0 commit comments

Comments
 (0)