mirror of
https://github.com/Horizontal42/summarum.git
synced 2026-11-10 15:42:01 +03:00
🧹 [code health improvement] Refactor overly long initDataAndEngine function
🎯 What: Split the overly long `initDataAndEngine` function into smaller, focused initialization functions for `engine`, `data`, `workspace`, `editor`, and `searchUI`. 💡 Why: Improves readability and maintainability by isolating the setup logic for different application components while keeping a single coordinating `initDataAndEngine` point. ✅ Verification: Confirmed via full test suite and build that all application bootstrapping remains correct and unaffected. ✨ Result: Cleaner code structure and easier debugging for the application boot sequence. Co-authored-by: Horizontal42 <75383519+Horizontal42@users.noreply.github.com>
This commit is contained in:
parent
37cfd2144b
commit
525c119118
1 changed files with 21 additions and 4 deletions
25
src/main.ts
25
src/main.ts
|
|
@ -940,14 +940,16 @@ async function initSettings(): Promise<void> {
|
|||
await runBackups(settings.dataDir, settings.deletedRetentionDays);
|
||||
}
|
||||
|
||||
async function initDataAndEngine(): Promise<void> {
|
||||
function initEngine(): void {
|
||||
engine = new SumEngine({
|
||||
precision: settings.precision,
|
||||
groupSeparator: settings.groupSeparator,
|
||||
decimalSeparator: settings.decimalSeparator,
|
||||
dateFormat: settings.dateFormat,
|
||||
});
|
||||
}
|
||||
|
||||
async function initData(): Promise<void> {
|
||||
const stored = await loadAppData(settings.dataDir);
|
||||
// a corrupt or foreign documents.json must not crash the boot
|
||||
if (
|
||||
|
|
@ -968,10 +970,9 @@ async function initDataAndEngine(): Promise<void> {
|
|||
}
|
||||
if (!data.docs.some((d) => d.id === data.activeId))
|
||||
data.activeId = data.docs[0].id;
|
||||
}
|
||||
|
||||
const scripts = await loadExtensionScripts();
|
||||
await runExtensions(engine, scripts);
|
||||
|
||||
function initWorkspace(): void {
|
||||
workspace = new Workspace(engine, () =>
|
||||
data.docs.map((d) => ({
|
||||
id: d.id,
|
||||
|
|
@ -979,7 +980,9 @@ async function initDataAndEngine(): Promise<void> {
|
|||
text: data.contents[d.id] ?? "",
|
||||
})),
|
||||
);
|
||||
}
|
||||
|
||||
function initEditor(): void {
|
||||
editor = new SumEditor(
|
||||
$("#editor"),
|
||||
$("#results"),
|
||||
|
|
@ -1016,7 +1019,9 @@ async function initDataAndEngine(): Promise<void> {
|
|||
// 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 initSearchUI(): void {
|
||||
search = initSearch({
|
||||
engine,
|
||||
workspace,
|
||||
|
|
@ -1034,6 +1039,18 @@ async function initDataAndEngine(): Promise<void> {
|
|||
});
|
||||
}
|
||||
|
||||
async function initDataAndEngine(): Promise<void> {
|
||||
initEngine();
|
||||
await initData();
|
||||
|
||||
const scripts = await loadExtensionScripts();
|
||||
await runExtensions(engine, scripts);
|
||||
|
||||
initWorkspace();
|
||||
initEditor();
|
||||
initSearchUI();
|
||||
}
|
||||
|
||||
function bindTitleUI(): void {
|
||||
const titleField = $<HTMLInputElement>("#doc-title");
|
||||
titleField.addEventListener("change", () => {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue