Skip to content

Commit 5b01f36

Browse files
committed
refactor: use debounce avoid potential perf issue #12
1 parent b27f09b commit 5b01f36

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/pages/background/index.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Browser from 'webextension-polyfill';
33
import { getLogseqCopliotConfig } from '../../config';
44
import { removeUrlHash } from '@/utils';
55
import { setExtensionBadge } from './utils';
6+
import { debounce } from 'lodash';
67

78
const logseqClient = new LogseqClient();
89

@@ -31,8 +32,12 @@ const quickCapture = async (data: string) => {
3132
const activeTab = tab[0];
3233
const url = `logseq://x-callback-url/quickCapture?title=${
3334
activeTab.title
34-
}&url=${encodeURIComponent(activeTab.url!)}&content=${encodeURIComponent(data)}`;
35+
}&url=${encodeURIComponent(activeTab.url!)}&content=${encodeURIComponent(
36+
data,
37+
)}`;
3538
Browser.tabs.update(activeTab.id, { url: url });
39+
40+
debounceBadgeSearch(activeTab.url, activeTab.id);
3641
};
3742

3843
Browser.runtime.onInstalled.addListener(() => {
@@ -46,15 +51,15 @@ Browser.runtime.onInstalled.addListener(() => {
4651
Browser.tabs.onActivated.addListener((activeInfo) => {
4752
const promise = new Promise(async () => {
4853
const tab = await Browser.tabs.get(activeInfo.tabId);
49-
await badgeSearch(tab.url, activeInfo.tabId);
54+
await debounceBadgeSearch(tab.url, activeInfo.tabId);
5055
});
5156
promise.catch((err) => console.error(err));
5257
});
5358

5459
Browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
5560
if (tab.active && changeInfo.status === 'complete') {
5661
const promise = new Promise(async () => {
57-
await badgeSearch(tab.url, tabId);
62+
await debounceBadgeSearch(tab.url, tabId);
5863
});
5964
promise.catch((err) => console.error(err));
6065
}
@@ -68,6 +73,8 @@ const badgeSearch = async (url: string | undefined, tabId: number) => {
6873
await setExtensionBadge(resultCount, tabId);
6974
};
7075

76+
const debounceBadgeSearch = debounce(badgeSearch, 200);
77+
7178
Browser.contextMenus.create({
7279
id: 'quick-capture',
7380
title: 'Quick Capture',

0 commit comments

Comments
 (0)