Skip to content

perf: make platformUtils synchronous via preload - #193

Merged
EtienneLescot merged 4 commits into
release/v1.8.0from
ponytail/sync-platform-utils
Jul 29, 2026
Merged

perf: make platformUtils synchronous via preload#193
EtienneLescot merged 4 commits into
release/v1.8.0from
ponytail/sync-platform-utils

Conversation

@EtienneLescot

@EtienneLescot EtienneLescot commented Jul 28, 2026

Copy link
Copy Markdown
Collaborator

perf: make platformUtils synchronous via preload

src/utils/platformUtils.ts exported 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 reads process.platform synchronously through a one-line preload constant and turns the helpers into ordinary sync functions.

Changes

electron/preload.ts

  • Add a module-level const PLATFORM = process.platform so the renderer gets a synchronous snapshot.
  • Replace the async getPlatform: () => ipcRenderer.invoke("get-platform") with getPlatform: () => PLATFORM.

electron/electron-env.d.ts

  • Update getPlatform return type from Promise<string> to string.

electron/ipc/handlers.ts

  • Remove the now-dead ipcMain.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() returns process.platform directly.
  • isMac, getModifierKey, getShiftKey, formatShortcut all drop async/await.
  • Drop the cachedPlatform module variable and the navigator.platform fallback (in an Electron renderer process.platform is always the right answer).

Caller updates — drop every await:

  • src/hooks/useScreenRecorder.ts:802,914,1089,1179 — four await window.electronAPI.getPlatform() sites; the enclosing async functions stay async for other reasons.
  • src/lib/exporter/gifExporter.ts:141await getPlatform().
  • src/contexts/ShortcutsContext.tsx — replaces the getIsMac().then(setIsMac) chain with a synchronous setIsMac(getIsMac()) inside the existing useEffect. 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:196getPlatform: 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 async helpers and ~50 lines of await/async ceremony removed.

Why this is safe

  • process.platform is the same Node global in the main and renderer processes. Our renderer runs with nodeIntegration: false, contextIsolation: true, and the default sandbox: false, so process.platform is reachable directly from the preload's contextBridge.
  • The platform value cannot change in a process lifetime, so caching-once-and-forever is correct by construction.
  • All consumers of isMac from 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.platform is the canonical source. The native-bridge system.getPlatform (different API) is untouched.

@coderabbitai

coderabbitai Bot commented Jul 28, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 8a943517-46cc-417b-a6b8-c5459b168f52

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch ponytail/sync-platform-utils

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@EtienneLescot
EtienneLescot merged commit 4230b02 into release/v1.8.0 Jul 29, 2026
8 of 9 checks passed
@EtienneLescot
EtienneLescot deleted the ponytail/sync-platform-utils branch July 29, 2026 07:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants