Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(background/tabEvents): memoize setIcon call #598

Merged
merged 2 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/background/services/tabEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,15 @@ export class TabEvents {
await this.setIconAndTooltip(path, title, tabId);
};

// TODO: memoize this call
private setIconAndTooltip = async (
path: (typeof ICONS)[keyof typeof ICONS],
title: string,
tabId?: TabId,
tabId: TabId,
) => {
await this.browser.action.setIcon({ path, tabId });
if (this.tabState.getIcon(tabId) !== path) {
this.tabState.setIcon(tabId, path);
await this.browser.action.setIcon({ path, tabId });
}
await this.browser.action.setTitle({ title, tabId });
};

Expand Down
11 changes: 11 additions & 0 deletions src/background/services/tabState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export class TabState {

private state = new Map<TabId, Map<string, State>>();
private sessions = new Map<TabId, Map<SessionId, PaymentSession>>();
private currentIcon = new Map<TabId, Record<number, string>>();

constructor({ logger }: Cradle) {
Object.assign(this, {
Expand Down Expand Up @@ -118,6 +119,14 @@ export class TabState {
return [...this.sessions.values()].flatMap((s) => [...s.values()]);
}

getIcon(tabId: TabId) {
return this.currentIcon.get(tabId);
}

setIcon(tabId: TabId, icon: Record<number, string>) {
this.currentIcon.set(tabId, icon);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: we're only storing the reference here, not the full data.

}

getAllTabs(): TabId[] {
return [...this.sessions.keys()];
}
Expand All @@ -128,6 +137,8 @@ export class TabState {
}

clearSessionsByTabId(tabId: TabId) {
this.currentIcon.delete(tabId);

const sessions = this.getSessions(tabId);
if (!sessions.size) return;

Expand Down