Skip to content

Commit 1df6ba3

Browse files
Copilotsawka
andcommitted
refactor: lazily initialize context menu model singleton
Co-authored-by: sawka <2722291+sawka@users.noreply.github.com>
1 parent e0a29de commit 1df6ba3

13 files changed

Lines changed: 51 additions & 17 deletions

File tree

frontend/app/aipanel/aipanel-contextmenu.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,5 +155,5 @@ export async function handleWaveAIContextMenu(e: React.MouseEvent, showCopy: boo
155155
});
156156
}
157157

158-
ContextMenuModel.showContextMenu(menu, e);
158+
ContextMenuModel.getInstance().showContextMenu(menu, e);
159159
}

frontend/app/app.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ async function handleContextMenu(e: React.MouseEvent<HTMLDivElement>) {
130130
},
131131
});
132132
}
133-
ContextMenuModel.showContextMenu(menu, e);
133+
ContextMenuModel.getInstance().showContextMenu(menu, e);
134134
}
135135

136136
function AppSettingsUpdater() {

frontend/app/block/blockframe-header.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ function handleHeaderContextMenu(
5757
click: () => uxCloseBlock(blockId),
5858
}
5959
);
60-
ContextMenuModel.showContextMenu(menu, e);
60+
ContextMenuModel.getInstance().showContextMenu(menu, e);
6161
}
6262

6363
type HeaderTextElemsProps = {
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { describe, expect, it, vi } from "vitest";
2+
3+
describe("ContextMenuModel", () => {
4+
it("initializes only when getInstance is called", async () => {
5+
const onContextMenuClick = vi.fn();
6+
const getApi = vi.fn(() => ({
7+
onContextMenuClick,
8+
showContextMenu: vi.fn(),
9+
}));
10+
11+
vi.resetModules();
12+
vi.doMock("./global", () => ({
13+
atoms: {},
14+
getApi,
15+
globalStore: { get: vi.fn() },
16+
}));
17+
18+
const { ContextMenuModel } = await import("./contextmenu");
19+
expect(getApi).not.toHaveBeenCalled();
20+
21+
const firstInstance = ContextMenuModel.getInstance();
22+
const secondInstance = ContextMenuModel.getInstance();
23+
24+
expect(firstInstance).toBe(secondInstance);
25+
expect(getApi).toHaveBeenCalledTimes(1);
26+
expect(onContextMenuClick).toHaveBeenCalledTimes(1);
27+
});
28+
});

frontend/app/store/contextmenu.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,21 @@
33

44
import { atoms, getApi, globalStore } from "./global";
55

6-
class ContextMenuModelType {
6+
class ContextMenuModel {
7+
private static instance: ContextMenuModel;
78
handlers: Map<string, () => void> = new Map(); // id -> handler
89

9-
constructor() {
10+
private constructor() {
1011
getApi().onContextMenuClick(this.handleContextMenuClick.bind(this));
1112
}
1213

14+
static getInstance(): ContextMenuModel {
15+
if (ContextMenuModel.instance == null) {
16+
ContextMenuModel.instance = new ContextMenuModel();
17+
}
18+
return ContextMenuModel.instance;
19+
}
20+
1321
handleContextMenuClick(id: string): void {
1422
const handler = this.handlers.get(id);
1523
if (handler) {
@@ -63,6 +71,4 @@ class ContextMenuModelType {
6371
}
6472
}
6573

66-
const ContextMenuModel = new ContextMenuModelType();
67-
68-
export { ContextMenuModel, ContextMenuModelType };
74+
export { ContextMenuModel };

frontend/app/tab/tab.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ const TabInner = forwardRef<HTMLDivElement, TabProps>((props, ref) => {
207207
menu.push({ label: "Backgrounds", type: "submenu", submenu }, { type: "separator" });
208208
}
209209
menu.push({ label: "Close Tab", click: () => onClose(null) });
210-
ContextMenuModel.showContextMenu(menu, e);
210+
ContextMenuModel.getInstance().showContextMenu(menu, e);
211211
},
212212
[handleRenameTab, id, onClose]
213213
);

frontend/app/view/preview/preview-directory.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ function TableBody({
419419
click: () => handleFileDelete(model, finfo.path, false, setErrorMsg),
420420
}
421421
);
422-
ContextMenuModel.showContextMenu(menu, e);
422+
ContextMenuModel.getInstance().showContextMenu(menu, e);
423423
},
424424
[setRefreshVersion, conn]
425425
);
@@ -861,7 +861,7 @@ function DirectoryPreview({ model }: DirectoryPreviewProps) {
861861
];
862862
addOpenMenuItems(menu, conn, finfo);
863863

864-
ContextMenuModel.showContextMenu(menu, e);
864+
ContextMenuModel.getInstance().showContextMenu(menu, e);
865865
},
866866
[setRefreshVersion, conn, newFile, newDirectory, dirPath]
867867
);

frontend/app/view/preview/preview-model.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ export class PreviewModel implements ViewModel {
209209
label: `Go to ${bookmark.label} (${bookmark.path})`,
210210
click: () => this.goHistory(bookmark.path),
211211
}));
212-
ContextMenuModel.showContextMenu(menuItems, e);
212+
ContextMenuModel.getInstance().showContextMenu(menuItems, e);
213213
},
214214
};
215215
}

frontend/app/view/term/term.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ const TerminalView = ({ blockId, model }: ViewComponentProps<TermViewModel>) =>
369369
e.preventDefault();
370370
e.stopPropagation();
371371
const menuItems = model.getContextMenuItems();
372-
ContextMenuModel.showContextMenu(menuItems, e);
372+
ContextMenuModel.getInstance().showContextMenu(menuItems, e);
373373
},
374374
[model]
375375
);

frontend/app/workspace/widgets.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ const Widgets = memo(() => {
401401
},
402402
},
403403
];
404-
ContextMenuModel.showContextMenu(menu, e);
404+
ContextMenuModel.getInstance().showContextMenu(menu, e);
405405
};
406406

407407
return (

0 commit comments

Comments
 (0)