The VS Code of video editing, built for agents.
TypeScript
Refactor file input handling in Electron to improve performance and caching
konstantin-paulus7328b5d553bb
diff --git a/apps/desktop/src/main.ts b/apps/desktop/src/main.tsindex 8a4f65a9..80ca2ec1 100644--- a/apps/desktop/src/main.ts+++ b/apps/desktop/src/main.ts@@ -45,26 +45,19 @@ function deliverAuthUrl(url: string) {async function setFileInputFiles(selector: string, absolutePath: string) {if (!mainWindow) throw new Error("No main window");const wc = mainWindow.webContents;- const wasAttached = wc.debugger.isAttached();- if (!wasAttached) wc.debugger.attach("1.3");- try {- const { root } = await wc.debugger.sendCommand("DOM.getDocument");- const { nodeId } = await wc.debugger.sendCommand("DOM.querySelector", {- nodeId: root.nodeId,- selector,- });- if (!nodeId) throw new Error(`Selector not found: ${selector}`);- await wc.debugger.sendCommand("DOM.setFileInputFiles", {- files: [absolutePath],- nodeId,- });- } finally {- if (!wasAttached) {- try {- wc.debugger.detach();- } catch { /** ignore */ }- }- }+ // Stay attached between calls — attach/detach dominates the cost of a+ // transfer, and file materialization happens in bursts.+ if (!wc.debugger.isAttached()) wc.debugger.attach("1.3");+ const { root } = await wc.debugger.sendCommand("DOM.getDocument");+ const { nodeId } = await wc.debugger.sendCommand("DOM.querySelector", {+ nodeId: root.nodeId,+ selector,+ });+ if (!nodeId) throw new Error(`Selector not found: ${selector}`);+ await wc.debugger.sendCommand("DOM.setFileInputFiles", {+ files: [absolutePath],+ nodeId,+ });}function createWindow(show = true) {diff --git a/apps/web/src/lib/electron-file-handle.ts b/apps/web/src/lib/electron-file-handle.tsindex 0895d17b..af73fc0e 100644--- a/apps/web/src/lib/electron-file-handle.ts+++ b/apps/web/src/lib/electron-file-handle.ts@@ -65,17 +65,27 @@ export class ElectronFileHandle {readonly name: string;readonly path: string;+ private filePromise: Promise<File> | null = null;+constructor(path: string, name?: string) {this.path = path;this.name = name ?? basename(path);}async getFile(): Promise<File> {- // Serialize CDP round-trips: every call shares the same hidden input, and- // overlapping setFiles() calls would race on `input.files[0]`.- const next = inFlight.then(() => materializeFile(this.path));- inFlight = next.catch(() => {});- return next;+ if (!this.filePromise) {+ // Serialize CDP round-trips: every call shares the same hidden input,+ // and overlapping setFiles() calls would race on `input.files[0]`.+ const next = inFlight.then(() => materializeFile(this.path));+ inFlight = next.catch(() => {});+ this.filePromise = next;+ next.catch(this.invalidate.bind(this));+ }+ return this.filePromise;+ }++ invalidate() {+ this.filePromise = null;}// FileSystemFileHandle shape — ElectronFileHandle is always "granted" because