Skip to content

Commit 8368362

Browse files
committed
Add debouncing to saveSync
1 parent 48ef6be commit 8368362

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

src/chrome/background.js

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ const Fetcher = function (cache) { // Pass reload to avoid cached result
118118
if (!http && filePath.includes('ads.txt')) {
119119
http = true;
120120
path = undefined;
121-
getText(host); // HTTPS fetch failed. Trying HTTP connection...
121+
// HTTPS fetch failed. Trying HTTP connection...
122+
getText(host);
122123
} else {
123124
func([www + host, 1, `${err}`]);
124125
}
@@ -132,7 +133,8 @@ const Fetcher = function (cache) { // Pass reload to avoid cached result
132133
path = path || (!http ? 'https:' : 'http:') + '//' + www + host + '/' + filePath;
133134
fetch(path, requestInit)
134135
.then(onlyOK)
135-
.then(onlyPlainText) // Reject promise when non-standard content-type served
136+
// Reject promise when non-standard content-type served
137+
.then(onlyPlainText)
136138
.then(res => res.text())
137139

138140
.then(res => func([www + host, 2, res]))
@@ -171,14 +173,18 @@ const popup = (function () {
171173
);
172174
}
173175

174-
function lateLoadURL(tabId, changeInfo, pTab) { // Capture URL if loaded later than the popup was open
176+
// Capture URL if loaded later than the popup was open
177+
function lateLoadURL(tabId, changeInfo, pTab) {
175178
if (changeInfo.url && pTab.active) {
176-
popup.message('reset'); // Send message new fetch is upcomming
179+
// Send message new fetch is upcomming
180+
popup.message('reset');
177181
const request = Fetcher('default');
178182
if (up) request.getDomain(changeInfo.url);
179183
}
180184
}
181185

186+
let saveTimeout;
187+
182188
return {
183189
up: (sendResponse) => {
184190
if (sendResponse) sendResponse(up);
@@ -202,25 +208,28 @@ const popup = (function () {
202208
}
203209
return data;
204210
},
205-
saveSync: function (data) { // Save specified sellers to sync storage
206-
chrome.storage.local.set(data, function () {
207-
if (data.sellers !== undefined) {
208-
config.sellers = data.sellers;
209-
if (up) sellersUpdate();
210-
}
211-
});
211+
saveSync: function (data) {
212+
if (saveTimeout) clearTimeout(saveTimeout);
213+
214+
saveTimeout = setTimeout(function() {
215+
chrome.storage.local.set(data, function () {
216+
if (data.sellers !== undefined) {
217+
config.sellers = data.sellers;
218+
if (up) sellersUpdate();
219+
}
220+
});
221+
}, 500);
212222
},
213223
message: function (resObj) {
214224
if (up) {
215225
try {
216226
chrome.runtime.sendMessage(resObj, () => {
217227
if (chrome.runtime.lastError) {
218-
// Silently handle the error
219-
console.debug('Message recipient unavailable:', chrome.runtime.lastError);
228+
console.error('Message recipient unavailable:', chrome.runtime.lastError);
220229
}
221230
});
222231
} catch (error) {
223-
console.debug('Error sending message:', error);
232+
console.error('Error sending message:', error);
224233
}
225234
}
226235
},

0 commit comments

Comments
 (0)