mirror of
https://github.com/stablyai/orca.git
synced 2026-09-30 08:03:12 +00:00
`window.api.platform.get()` runs ~19x/sec while the app is idle. Every call recomputed a payload whose fields are all fixed for the process lifetime (`process.platform`, `process.getSystemVersion()`, `process.arch`, the shell env vars, and the env-derived Linux display server), allocated a fresh object, and crossed the context bridge. Memoize the payload lazily at preload module scope and freeze it, and cache the resolved platform in `getRendererAppPlatform()` so the 32 renderer call sites stop crossing the bridge on every render. The user-agent fallback stays uncached because the web client installs its platform API after boot.
29 lines
1.2 KiB
TypeScript
29 lines
1.2 KiB
TypeScript
import { getLinuxDisplayServer } from '../preload-runtime-support'
|
|
import type { PreloadApi } from '../api-types'
|
|
|
|
type PlatformInfo = ReturnType<PreloadApi['platform']['get']>
|
|
|
|
// Why: the renderer reads this on its render cadence, and every field below is fixed
|
|
// for the process lifetime, so resolve once and hand back the same frozen payload.
|
|
let platformInfo: PlatformInfo | undefined
|
|
|
|
function resolvePlatformInfo(): PlatformInfo {
|
|
return Object.freeze({
|
|
platform: process.platform,
|
|
// Why: sandboxed preload cannot require node:os; Electron exposes the OS
|
|
// version on process.getSystemVersion when available.
|
|
osRelease:
|
|
(process as NodeJS.Process & { getSystemVersion?: () => string }).getSystemVersion?.() ?? '',
|
|
arch: process.arch,
|
|
// Why: these identify the default shell without probing user config files.
|
|
// process.env is available in the sandboxed preload; node:os is not.
|
|
shell: process.env.SHELL?.trim() || process.env.ComSpec?.trim() || '',
|
|
displayServer: getLinuxDisplayServer()
|
|
})
|
|
}
|
|
|
|
export const platformApi = {
|
|
// Why: resolved lazily so preload startup keeps paying nothing for it.
|
|
get: () => (platformInfo ??= resolvePlatformInfo())
|
|
} satisfies PreloadApi['platform']
|