Skip to content
This repository was archived by the owner on May 10, 2024. It is now read-only.

Commit 26e7bcf

Browse files
committed
update debounce
1 parent eefb1b5 commit 26e7bcf

2 files changed

Lines changed: 14 additions & 12 deletions

File tree

code/foreground.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ const debounced_modify_followed_channels_list = create_debounced_function(() =>
105105
(favorites.has(channel_name) ? apply_settings_to_channel(channel, "followed") : remove_applied_settings_from_channel(channel));
106106
}
107107
}
108-
}, 50);
108+
}, 250, false);
109109
const followed_channels_list_mo = new MutationObserver((mutations) => {
110110
debounced_modify_followed_channels_list();
111111
});
@@ -117,12 +117,14 @@ function create_element_from_html_string(html_string) {
117117
return element;
118118
}
119119

120-
function create_debounced_function(fn, timeout) {
121-
let timer = null;
122-
return () => {
123-
(timer ? clearTimeout(timer) : null);
124-
timer = setTimeout(() => {
120+
function create_debounced_function(fn, timeout, exec_leading) {
121+
let timeout_id = null;
122+
return function () {
123+
(exec_leading && !timeout_id ? fn.apply(this, arguments) : null);
124+
(timeout_id ? clearTimeout(timeout_id) : null);
125+
timeout_id = setTimeout(() => {
125126
fn.apply(this, arguments);
127+
timeout_id = null;
126128
}, timeout);
127129
};
128130
}
@@ -337,8 +339,8 @@ function update_channels_lists() {
337339

338340
const show_more_times_clicked = expand_followed_channels_list();
339341

340-
const num_existing_channels = favorite_channels_list.children.length;
341-
let num_replaced_channels = 0;
342+
const existing_channel_count = favorite_channels_list.children.length;
343+
let replaced_channel_count = 0;
342344
for (const channel of followed_channels_list.children) {
343345
const channel_live = (channel.querySelector("span").innerHTML == "Offline" ? false : true);
344346
if (channel_live) {
@@ -347,15 +349,15 @@ function update_channels_lists() {
347349
const channel_clone = channel.cloneNode(true);
348350
configure_channel_clone(channel_clone);
349351

350-
(num_replaced_channels < num_existing_channels ? favorite_channels_list.children[num_replaced_channels++].replaceWith(channel_clone) : favorite_channels_list.append(channel_clone));
352+
(replaced_channel_count < existing_channel_count ? favorite_channels_list.children[replaced_channel_count++].replaceWith(channel_clone) : favorite_channels_list.append(channel_clone));
351353
}
352354
} else {
353355
break;
354356
}
355357
}
356358

357-
const num_leftover_channels = num_existing_channels - num_replaced_channels;
358-
for (let i = 0; i < num_leftover_channels; i++) {
359+
const leftover_channel_count = existing_channel_count - replaced_channel_count;
360+
for (let i = 0; i < leftover_channel_count; i++) {
359361
const last_element = [...(favorite_channels_list.children)].at(-1);
360362
last_element.remove();
361363
}

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"name": "ttv-favorites",
44
"description": "enables favorites on Twitch.tv",
55
"homepage_url": "https://github.com/jc9108/ttv-favorites",
6-
"version": "1.2.10",
6+
"version": "1.2.11",
77
"manifest_version": 3,
88
"icons": {
99
"16": "./images/icon_16.png",

0 commit comments

Comments
 (0)