Skip to content

Commit 6795a0d

Browse files
committed
sample instead of debouncing
1 parent d5776bb commit 6795a0d

File tree

3 files changed

+69
-5
lines changed

3 files changed

+69
-5
lines changed

injected/integration-test/favicon.spec.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,48 @@ test('favicon + monitor', async ({ page }, testInfo) => {
4242
});
4343
});
4444

45+
test('favicon + monitor (many updates)', async ({ page }, testInfo) => {
46+
const favicon = ResultsCollector.create(page, testInfo.project.use);
47+
await page.clock.install();
48+
await favicon.load(HTML, CONFIG);
49+
50+
// ensure first favicon item was sent
51+
await favicon.waitForMessage('faviconFound', 1);
52+
53+
// now update it
54+
await page.getByRole('button', { name: 'Set many overrides' }).click();
55+
await page.clock.fastForward(20);
56+
57+
const messages = await favicon.outgoingMessages();
58+
expect(messages).toHaveLength(1);
59+
60+
await page.clock.fastForward(60);
61+
await page.clock.fastForward(100);
62+
63+
{
64+
const messages = await favicon.outgoingMessages();
65+
expect(messages).toHaveLength(3);
66+
}
67+
68+
{
69+
const messages = await favicon.outgoingMessages();
70+
expect(messages.map((x) => /** @type {{params: any}} */ (x.payload).params)).toStrictEqual([
71+
{
72+
favicons: [{ href: './favicon.png', rel: 'shortcut icon' }],
73+
documentUrl: 'http://localhost:3220/favicon/index.html',
74+
},
75+
{
76+
favicons: [{ href: './new_favicon.png?count=0', rel: 'shortcut icon' }],
77+
documentUrl: 'http://localhost:3220/favicon/index.html',
78+
},
79+
{
80+
favicons: [{ href: './new_favicon.png?count=1', rel: 'shortcut icon' }],
81+
documentUrl: 'http://localhost:3220/favicon/index.html',
82+
},
83+
]);
84+
}
85+
});
86+
4587
test('favicon + monitor disabled', async ({ page }, testInfo) => {
4688
const CONFIG = './integration-test/test-pages/favicon/config/favicon-monitor-disabled.json';
4789
const favicon = ResultsCollector.create(page, testInfo.project.use);

injected/integration-test/test-pages/favicon/index.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,21 @@
88
</head>
99
<body>
1010
<button onclick="setOverride()">Set override</button>
11+
<button onclick="setManyOverrides()">Set many overrides</button>
1112
<script>
1213
function setOverride() {
1314
document.querySelector("link[rel='shortcut icon']").href = "./new_favicon.png";
1415
}
16+
async function setManyOverrides() {
17+
const elem = document.querySelector("link[rel='shortcut icon']");
18+
for (let i = 0; i < 100; i++) {
19+
await new Promise(resolve => {
20+
setTimeout(resolve, 40);
21+
})
22+
const path = `./new_favicon.png?count=${i}`
23+
elem.href = path
24+
}
25+
}
1526
</script>
1627
</body>
1728
</html>

injected/src/features/favicon.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import ContentFeature from '../content-feature.js';
22

33
export class Favicon extends ContentFeature {
4-
/** @type {undefined|number} */
5-
#debounce = undefined;
64
init() {
75
window.addEventListener('DOMContentLoaded', () => {
86
// send once, immediately
@@ -17,15 +15,28 @@ export class Favicon extends ContentFeature {
1715
// if there was an explicit opt-out, do nothing
1816
if (this.getFeatureSetting('monitor') === false) return;
1917

18+
let trailing;
19+
let lastEmitTime = performance.now();
20+
const interval = 50;
21+
2022
// otherwise, monitor and send updates
2123
monitor(() => {
22-
clearTimeout(this.#debounce);
23-
const id = setTimeout(() => this.send(), 100);
24-
this.#debounce = /** @type {any} */ (id);
24+
clearTimeout(trailing);
25+
const currentTime = performance.now();
26+
const delta = currentTime - lastEmitTime;
27+
if (delta >= interval) {
28+
this.send();
29+
} else {
30+
trailing = setTimeout(() => {
31+
this.send();
32+
}, 50);
33+
}
34+
lastEmitTime = currentTime;
2535
});
2636
}
2737

2838
send() {
39+
console.log('📤 sending');
2940
const favicons = getFaviconList();
3041
this.notify('faviconFound', { favicons, documentUrl: document.URL });
3142
}

0 commit comments

Comments
 (0)