Skip to content

Commit

Permalink
fix: remove legacy rushFirst patch (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
uncenter authored Jul 11, 2024
1 parent 2c30063 commit 39b5b42
Showing 1 changed file with 2 additions and 25 deletions.
27 changes: 2 additions & 25 deletions src/entries/content/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,10 @@ export default defineContentScript({
runAt: 'document_start',

main() {
// Replacing all icons synchronously prevents visual "blinks" but can
// cause missing icons/rendering delay in very large folders.
// Replacing asynchronously instead fixes problems in large folders, but introduces "blinks".
// Here we compromise, rushing the first n replacements to prevent blinks that will likely be "above the fold"
// and delaying the replacement of subsequent rows.
let executions = 0;
let timerID: NodeJS.Timeout;
const rushFirst = (rushBatch: number, callback: () => void) => {
if (executions <= rushBatch) {
callback(); // Immediately run to prevent visual "blink".
setTimeout(callback, 20); // Run again later to catch any icons that are missed in large repositories.
executions += 1;
} else {
setTimeout(callback, 0); // Run without blocking to prevent delayed rendering of large folders too much.
clearTimeout(timerID);
timerID = setTimeout(() => {
executions = 0;
}, 1000); // Reset execution tracker.
}
};

// Monitor DOM elements that match a CSS selector.
observe(SELECTORS.row, {
add(row) {
const callback = async () =>
await replaceIconInRow(row as HTMLElement);
rushFirst(90, callback);
async add(row) {
await replaceIconInRow(row as HTMLElement);
},
});

Expand Down

0 comments on commit 39b5b42

Please sign in to comment.