perf: optimize app startup and background lifecycles

This commit is contained in:
inman
2026-08-19 00:56:15 +08:00
parent 31332f6d76
commit 927e13349b
121 changed files with 4359 additions and 1400 deletions

View File

@@ -55,6 +55,13 @@ export interface WorksSquareSessionPersistence {
save(session: WorksSquareSessionInput | null): Promise<void>;
}
export interface WorksSquareSecureStorage {
isEncryptionAvailable(): boolean;
getSelectedStorageBackend(): string;
encryptString(plainText: string): Buffer;
decryptString(encrypted: Buffer): string;
}
export type WorksSquareSessionChangeReason = 'changed' | 'terminal';
export type WorksSquareSessionListener = (
session: WorksSquareSessionSnapshot | null,
@@ -279,11 +286,10 @@ function replaceCredentialSession(
return { generation, persisted, previousSession };
}
async function createElectronSessionPersistence(): Promise<WorksSquareSessionPersistence | null> {
const [{ default: Store }, { safeStorage }] = await Promise.all([
import('electron-store'),
import('electron'),
]);
async function createElectronSessionPersistence(
safeStorage: WorksSquareSecureStorage,
): Promise<WorksSquareSessionPersistence | null> {
const { default: Store } = await import('electron-store');
const usesUnprotectedLinuxBackend = process.platform === 'linux'
&& safeStorage.getSelectedStorageBackend() === 'basic_text';
if (!safeStorage.isEncryptionAvailable() || usesUnprotectedLinuxBackend) {
@@ -347,16 +353,20 @@ async function createElectronSessionPersistence(): Promise<WorksSquareSessionPer
export async function initializeWorksSquareSession(
options: {
persistence?: WorksSquareSessionPersistence;
secureStorage?: WorksSquareSecureStorage | null;
nowMs?: number;
} = {},
): Promise<WorksSquareSessionSnapshot | null> {
persistenceQueue = Promise.resolve();
credentialPersistenceBarrier = null;
startupRuntimeCleanupRequired = false;
sessionPersistenceFactory = options.persistence ? null : createElectronSessionPersistence;
const secureStorage = options.secureStorage ?? null;
sessionPersistenceFactory = options.persistence || !secureStorage
? null
: () => createElectronSessionPersistence(secureStorage);
try {
sessionPersistence = options.persistence
?? await sessionPersistenceFactory!();
?? (sessionPersistenceFactory ? await sessionPersistenceFactory() : null);
} catch (error) {
logger.warn('[works-square-session] Failed to initialize secure session persistence', error);
sessionPersistence = null;