From 8f541fc4994e595841992a6b2e879c56649eea95 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 26 Aug 2026 14:02:06 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=B9=20[Refactor=20initDataAndEngine]?= =?UTF-8?q?=20Extract=20initialization=20logic=20into=20smaller=20function?= =?UTF-8?q?s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The `initDataAndEngine` function in `src/main.ts` was extremely long and handled the initialization of multiple loosely related components (engine, data, extensions, workspace, editor, and search controller). This commit extracts those distinct responsibilities into their own smaller, self-contained functions (`initEngine`, `initData`, `initExtensions`, `initWorkspace`, `initEditor`, and `initSearchController`), which are now sequentially called by the original `initDataAndEngine` function. This improves maintainability and readability without altering the underlying logic or initialization order. Co-authored-by: Horizontal42 <75383519+Horizontal42@users.noreply.github.com> --- src/main.ts | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/main.ts b/src/main.ts index cd60dde..5dfedcb 100644 --- a/src/main.ts +++ b/src/main.ts @@ -940,14 +940,16 @@ async function initSettings(): Promise { await runBackups(settings.dataDir, settings.deletedRetentionDays); } -async function initDataAndEngine(): Promise { +function initEngine(): void { engine = new SumEngine({ precision: settings.precision, groupSeparator: settings.groupSeparator, decimalSeparator: settings.decimalSeparator, dateFormat: settings.dateFormat, }); +} +async function initData(): Promise { const stored = await loadAppData(settings.dataDir); // a corrupt or foreign documents.json must not crash the boot if ( @@ -968,10 +970,14 @@ async function initDataAndEngine(): Promise { } if (!data.docs.some((d) => d.id === data.activeId)) data.activeId = data.docs[0].id; +} +async function initExtensions(): Promise { const scripts = await loadExtensionScripts(); await runExtensions(engine, scripts); +} +function initWorkspace(): void { workspace = new Workspace(engine, () => data.docs.map((d) => ({ id: d.id, @@ -979,7 +985,9 @@ async function initDataAndEngine(): Promise { text: data.contents[d.id] ?? "", })), ); +} +function initEditor(): void { editor = new SumEditor( $("#editor"), $("#results"), @@ -1016,7 +1024,9 @@ async function initDataAndEngine(): Promise { // the editor above was seeded directly with initialText (not via switchDoc), // so a reopened sheet with "on 2024-01-01" needs its own historical-rate fetch void fetchNeededHistoricalRates(data.contents[data.activeId] ?? ""); +} +function initSearchController(): void { search = initSearch({ engine, workspace, @@ -1034,6 +1044,15 @@ async function initDataAndEngine(): Promise { }); } +async function initDataAndEngine(): Promise { + initEngine(); + await initData(); + await initExtensions(); + initWorkspace(); + initEditor(); + initSearchController(); +} + function bindTitleUI(): void { const titleField = $("#doc-title"); titleField.addEventListener("change", () => {