Skip to content

Commit 42d3761

Browse files
author
leoding86
committed
optimize save file overwriting logic
1 parent 5f080c4 commit 42d3761

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

src/background/services/DownloadService.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ class DownloadService extends AbstractService {
1717

1818
openingDownloadManager = false;
1919

20-
cachedDownloadIdFilenameMap = {};
20+
/**
21+
* @type {Map<string, string>}
22+
*/
23+
cachedDownloadIdFilenameMap = new Map();
2124

2225
constructor() {
2326
super();
@@ -27,11 +30,15 @@ class DownloadService extends AbstractService {
2730
conflictAction: "uniquify",
2831
};
2932

30-
if (this.cachedDownloadIdFilenameMap[downloadItem.id]) {
31-
filenameSuggestion.filename = this.cachedDownloadIdFilenameMap[downloadItem.id];
32-
delete this.cachedDownloadIdFilenameMap[downloadItem.id];
33+
if (this.cachedDownloadIdFilenameMap.has(downloadItem.url)) {
34+
filenameSuggestion.filename = this.cachedDownloadIdFilenameMap.get(downloadItem.url);
35+
delete this.cachedDownloadIdFilenameMap.delete(downloadItem.url);
36+
} else {
37+
filenameSuggestion.filename = downloadItem.filename;
3338
}
3439

40+
console.log(downloadItem, filenameSuggestion);
41+
3542
suggest(filenameSuggestion);
3643
});
3744
}
@@ -115,13 +122,13 @@ class DownloadService extends AbstractService {
115122
}
116123
}
117124

118-
cacheDownloadIdFilename(downloadId, filename) {
119-
this.cachedDownloadIdFilenameMap[downloadId] = filename;
125+
cacheDownloadIdFilename(url, filename) {
126+
this.cachedDownloadIdFilenameMap.set(url, filename);
120127
}
121128

122129
async saveFile({ url, filename }) {
130+
this.cacheDownloadIdFilename(url, filename);
123131
const downloadId = await FileSystem.getDefault().saveFile({ url, filename });
124-
this.cacheDownloadIdFilename(downloadId, filename);
125132
return downloadId;
126133
}
127134
}

0 commit comments

Comments
 (0)