perf: make platformUtils synchronous via preload - #193
Merged
Conversation
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
perf: make platformUtils synchronous via preload
src/utils/platformUtils.tsexported five async helpers (getPlatform,isMac,getModifierKey,getShiftKey,formatShortcut) that all cached a value that never changes in a process lifetime. Every call paid an IPC round-trip to the main process. This refactor readsprocess.platformsynchronously through a one-line preload constant and turns the helpers into ordinary sync functions.Changes
electron/preload.tsconst PLATFORM = process.platformso the renderer gets a synchronous snapshot.getPlatform: () => ipcRenderer.invoke("get-platform")withgetPlatform: () => PLATFORM.electron/electron-env.d.tsgetPlatformreturn type fromPromise<string>tostring.electron/ipc/handlers.tsipcMain.handle("get-platform", ...)handler. Verified it had no other callers in main; the only renderer-side path was through the preload.src/utils/platformUtils.ts(rewrite, 65 → 35 lines)getPlatform()returnsprocess.platformdirectly.isMac,getModifierKey,getShiftKey,formatShortcutall dropasync/await.cachedPlatformmodule variable and thenavigator.platformfallback (in an Electron rendererprocess.platformis always the right answer).Caller updates — drop every
await:src/hooks/useScreenRecorder.ts:802,914,1089,1179— fourawait window.electronAPI.getPlatform()sites; the enclosing async functions stay async for other reasons.src/lib/exporter/gifExporter.ts:141—await getPlatform().src/contexts/ShortcutsContext.tsx— replaces thegetIsMac().then(setIsMac)chain with a synchronoussetIsMac(getIsMac())inside the existinguseEffect. State + effect are kept (so SSR/hydration still gets a stable initial render), but the value lands on the first effect tick instead of after a microtask.src/components/launch/LaunchWindow.test.tsx:196—getPlatform: vi.fn(async () => "darwin")→vi.fn(() => "darwin"). (The native-bridge mock at line 108 is left async — that one is a different API and still returns a Promise.)Net: 8 files, +32/-62. Five
asynchelpers and ~50 lines ofawait/asyncceremony removed.Why this is safe
process.platformis the same Node global in the main and renderer processes. Our renderer runs withnodeIntegration: false,contextIsolation: true, and the defaultsandbox: false, soprocess.platformis reachable directly from the preload's contextBridge.isMacfrom the context (NewEditorShell.tsx,ShortcutsConfigDialog.tsx) already consume it as a boolean — no caller changes needed.Testing
npx tsc --noEmit— passes (exit 0).npm run test— 1144/1144 tests pass.npm run lint— same 7 pre-existing warnings, no new ones.npm run format— no changes needed.Manual smoke test on a real platform: not required — the helpers' return values are identical to the previous IPC-backed implementation, and
process.platformis the canonical source. The native-bridgesystem.getPlatform(different API) is untouched.