Skip to content

Commit

Permalink
fix: optimize setActiveNetworkTypes to not do anything when types hav…
Browse files Browse the repository at this point in the history
…en't changed (#31)
  • Loading branch information
fivecar authored Jan 25, 2023
1 parent 409c875 commit 5623cde
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,13 @@ export default class DownloadQueue {
async setActiveNetworkTypes(types: string[]): Promise<void> {
this.verifyInitialized();

if (
this.activeNetworkTypes.length === types.length &&
this.activeNetworkTypes.every(type => types.includes(type))
) {
return;
}

this.activeNetworkTypes = types;
if (this.netInfoFetchState) {
const state = await this.netInfoFetchState();
Expand Down
20 changes: 20 additions & 0 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1558,6 +1558,26 @@ describe("DownloadQueue", () => {
await expect(queue.setActiveNetworkTypes(["wifi"])).rejects.toThrow();
});


it("should do nothing when setting same active network types", async () => {
const queue = new DownloadQueue();

await queue.init({
domain: "mydomain",
activeNetworkTypes: ["wifi", "ethernet"],
netInfoAddEventListener: addEventListener,
netInfoFetchState: fetch,
});
expect(addEventListener).toHaveBeenCalledTimes(1);
expect(fetch).toHaveBeenCalledTimes(1);

await queue.setActiveNetworkTypes(["ethernet", "wifi"]);
expect(fetch).toHaveBeenCalledTimes(1); // no change

await queue.setActiveNetworkTypes(["ethernet", "wifi", "cellular"]);
expect(fetch).toHaveBeenCalledTimes(2);
});

it("should update activeNetworkTypes to current conditions", async () => {
const queue = new DownloadQueue();
const state = createNetState(true);
Expand Down

0 comments on commit 5623cde

Please sign in to comment.