Skip to content

Commit

Permalink
Add SessionManager class
Browse files Browse the repository at this point in the history
Has the following benefits:
* `ses.protocol.handle` only once per Tab
* Session data is cleared after tab is destroyed
* File requests no longer have to redirect
  • Loading branch information
BrandonXLF committed Oct 28, 2023
1 parent 7a2e3d4 commit 8024d3f
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 55 deletions.
69 changes: 69 additions & 0 deletions src/main/SessionManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { session, Session } from 'electron';
import { fileURLToPath } from 'url';
import fs from 'fs/promises';
import convertText from './convertText';
import { getFileType } from '../utils/fileTypes';

export default class SessionManager {
sessions: Record<string, { file: string, mode: string, text: string, ses: Session }> = {};

private textResponse(mode: string, text: string) {
return new Response(convertText(mode, text), {
headers: { 'content-type': 'text/html' }
});
}

private configureIntercept(partition: string, ses: Session) {
ses.protocol.handle('file', async (req) => {
const { file, mode, text } = this.sessions[partition];

let requestFile: string | undefined;

try {
requestFile = fileURLToPath(req.url);
} catch (_) {
// Not a valid file
}

// Intercept the file being edited
if (req.url === `file://${partition}/` || (requestFile !== undefined && requestFile === file))
return this.textResponse(mode, text);

// Convert supported file types
if (requestFile !== undefined && getFileType(requestFile)) {
return this.textResponse(mode, await fs.readFile(requestFile, 'utf8'));
}

// @ts-ignore bypassCustomProtocolHandlers is allowed
return ses.fetch(req, { bypassCustomProtocolHandlers: true })
});
}

private start(partition: string, file: string, mode: string, text: string) {
const ses = session.fromPartition(partition);

this.sessions[partition] = { file, mode, text, ses };
this.configureIntercept(partition, ses);
}

private update(partition: string, file: string, mode: string, text: string) {
this.sessions[partition].file = file;
this.sessions[partition].mode = mode;
this.sessions[partition].text = text;
}

set(partition: string, file: string, mode: string, text: string) {
if (!(partition in this.sessions)) {
this.start(partition, file, mode, text);
return;
}

this.update(partition, file, mode, text);
}

delete(partition: string) {
this.sessions[partition]?.ses.clearStorageData();

delete this.sessions[partition];
}
}
51 changes: 0 additions & 51 deletions src/main/interceptFileProtocol.ts

This file was deleted.

9 changes: 6 additions & 3 deletions src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { app, BrowserWindow, ipcMain, webContents, dialog, nativeTheme, Menu } f
import { getOpenFilters, getSaveFilters } from '../utils/fileTypes';
import { showContextMenu } from './contextMenu';
import { join } from 'path';
import interceptFileProtocol from './interceptFileProtocol';
import SessionManager from './SessionManager';
import showTopMenu from './showTopMenu';
import { randomUUID } from 'crypto';
import handleKeyboardShortcut from './handleKeyboardShortcut';
Expand All @@ -27,7 +27,8 @@ const store = new Store(),
settings = new SettingStore(emitSettingsUpdate, store),
fileHandler = new FileHandler(),
windowFactory = new WindowFactory(store, fileHandler),
updater = new Updater();
updater = new Updater(),
sessionManager = new SessionManager();

Menu.setApplicationMenu(Menu.buildFromTemplate([]));
fileHandler.registerEvents(app);
Expand Down Expand Up @@ -125,7 +126,9 @@ settings.callAndListen('theme', (theme: 'system' | 'light' | 'dark') => {
nativeTheme.themeSource = theme;
});

ipcMain.on('intercept-file', interceptFileProtocol);
ipcMain.on('set-session',(_, ...args: [string, string, string, string]) => sessionManager.set(...args));
ipcMain.on('delete-session', (_, partition: string) => sessionManager.delete(partition));

ipcMain.on('show-menu', showTopMenu);
ipcMain.on('keyboard-input', handleKeyboardShortcut);

Expand Down
3 changes: 2 additions & 1 deletion src/window/Tab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ export default class Tab {

await this.webviewReady;

ipcRenderer.send('intercept-file', this.partition, this.path, this.mode, value);
ipcRenderer.send('set-session', this.partition, this.path, this.mode, value);

try {
await this.webview.loadURL(this.path ? pathToFileURL(this.path).href : `file://${this.partition}/`);
Expand Down Expand Up @@ -377,6 +377,7 @@ export default class Tab {
this.webviewSubContainer.remove();
this.devtools.remove();
this.removeCSSUpdateListener?.();
ipcRenderer.send('delete-session', this.partition);
}

getTabData(): TabData {
Expand Down

0 comments on commit 8024d3f

Please sign in to comment.