Skip to content

Commit ed0ec9b

Browse files
committed
refactor: memoize built-in battery detection at module level
Generated-By: PostHog Code Task-Id: 6b1cbbfe-903b-400d-a76f-45e614c6d0f8
1 parent 3c84ce2 commit ed0ec9b

1 file changed

Lines changed: 4 additions & 6 deletions

File tree

apps/code/src/main/platform-adapters/electron-power-manager.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ const execFileAsync = promisify(execFile);
99

1010
@injectable()
1111
export class ElectronPowerManager implements IPowerManager {
12-
private hasBuiltInBatteryResult: Promise<boolean> | null = null;
13-
1412
public onResume(handler: () => void): () => void {
1513
powerMonitor.on("resume", handler);
1614
return () => powerMonitor.off("resume", handler);
@@ -26,13 +24,13 @@ export class ElectronPowerManager implements IPowerManager {
2624
}
2725

2826
public hasBuiltInBattery(): Promise<boolean> {
29-
if (!this.hasBuiltInBatteryResult) {
30-
this.hasBuiltInBatteryResult = detectBuiltInBattery().catch(() => false);
31-
}
32-
return this.hasBuiltInBatteryResult;
27+
memoizedBuiltInBattery ??= detectBuiltInBattery().catch(() => false);
28+
return memoizedBuiltInBattery;
3329
}
3430
}
3531

32+
let memoizedBuiltInBattery: Promise<boolean> | null = null;
33+
3634
async function detectBuiltInBattery(): Promise<boolean> {
3735
switch (process.platform) {
3836
case "darwin": {

0 commit comments

Comments
 (0)