mirror of
https://github.com/Horizontal42/summarum.git
synced 2026-11-10 15:42:01 +03:00
🧹 [Refactor initDataAndEngine] Extract initialization logic into smaller functions
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>
This commit is contained in:
parent
37cfd2144b
commit
8f541fc499
1 changed files with 20 additions and 1 deletions
21
src/main.ts
21
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,14 @@ async function initDataAndEngine(): Promise<void> {
|
|||
}
|
||||
if (!data.docs.some((d) => d.id === data.activeId))
|
||||
data.activeId = data.docs[0].id;
|
||||
}
|
||||
|
||||
async function initExtensions(): Promise<void> {
|
||||
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<void> {
|
|||
text: data.contents[d.id] ?? "",
|
||||
})),
|
||||
);
|
||||
}
|
||||
|
||||
function initEditor(): void {
|
||||
editor = new SumEditor(
|
||||
$("#editor"),
|
||||
$("#results"),
|
||||
|
|
@ -1016,7 +1024,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 initSearchController(): void {
|
||||
search = initSearch({
|
||||
engine,
|
||||
workspace,
|
||||
|
|
@ -1034,6 +1044,15 @@ async function initDataAndEngine(): Promise<void> {
|
|||
});
|
||||
}
|
||||
|
||||
async function initDataAndEngine(): Promise<void> {
|
||||
initEngine();
|
||||
await initData();
|
||||
await initExtensions();
|
||||
initWorkspace();
|
||||
initEditor();
|
||||
initSearchController();
|
||||
}
|
||||
|
||||
function bindTitleUI(): void {
|
||||
const titleField = $<HTMLInputElement>("#doc-title");
|
||||
titleField.addEventListener("change", () => {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue