52 lines
1.2 KiB
TypeScript
52 lines
1.2 KiB
TypeScript
|
|
import { WebContentsView } from "electron/main";
|
||
|
|
import type { WebContentsView as ElectronWebContentsView } from "electron/main";
|
||
|
|
|
||
|
|
export interface HotViewHandle {
|
||
|
|
accountId: string;
|
||
|
|
view: ElectronWebContentsView;
|
||
|
|
activatedAt: number;
|
||
|
|
}
|
||
|
|
|
||
|
|
const hotViews = new Map<string, HotViewHandle>();
|
||
|
|
|
||
|
|
export function acquireHotView(accountId: string): HotViewHandle {
|
||
|
|
const existing = hotViews.get(accountId);
|
||
|
|
if (existing) {
|
||
|
|
existing.activatedAt = Date.now();
|
||
|
|
return existing;
|
||
|
|
}
|
||
|
|
|
||
|
|
const handle: HotViewHandle = {
|
||
|
|
accountId,
|
||
|
|
view: new WebContentsView({
|
||
|
|
webPreferences: {
|
||
|
|
sandbox: true,
|
||
|
|
contextIsolation: true,
|
||
|
|
nodeIntegration: false,
|
||
|
|
},
|
||
|
|
}),
|
||
|
|
activatedAt: Date.now(),
|
||
|
|
};
|
||
|
|
hotViews.set(accountId, handle);
|
||
|
|
return handle;
|
||
|
|
}
|
||
|
|
|
||
|
|
export function demoteView(accountId: string): void {
|
||
|
|
const handle = hotViews.get(accountId);
|
||
|
|
if (!handle) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!handle.view.webContents.isDestroyed()) {
|
||
|
|
handle.view.webContents.close();
|
||
|
|
}
|
||
|
|
hotViews.delete(accountId);
|
||
|
|
}
|
||
|
|
|
||
|
|
export function listHotViews(): Array<Pick<HotViewHandle, "accountId" | "activatedAt">> {
|
||
|
|
return [...hotViews.values()].map(({ accountId, activatedAt }) => ({
|
||
|
|
accountId,
|
||
|
|
activatedAt,
|
||
|
|
}));
|
||
|
|
}
|