From 99ca701a5c8b91050a58b7dc788f4307c8fc31b1 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Mon, 29 Jul 2024 20:41:28 +0300 Subject: [PATCH] electron: Fix initial sync (closes #284) Apparently the issue was caused by options_init which for Electron was attempting to read the theme asynchronously. That's why it didn't cause issues on the server build. --- src/services/options_init.ts | 12 ++---------- src/services/setup.ts | 4 ++-- src/services/sql_init.ts | 18 +++++++++++++++--- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/services/options_init.ts b/src/services/options_init.ts index a50d57296a..b07300150e 100644 --- a/src/services/options_init.ts +++ b/src/services/options_init.ts @@ -16,7 +16,7 @@ interface NotSyncedOpts { syncProxy?: string; } -async function initNotSyncedOptions(initialized: boolean, opts: NotSyncedOpts = {}) { +async function initNotSyncedOptions(initialized: boolean, theme: string, opts: NotSyncedOpts = {}) { optionService.createOption('openNoteContexts', JSON.stringify([ { notePath: 'root', @@ -32,15 +32,7 @@ async function initNotSyncedOptions(initialized: boolean, opts: NotSyncedOpts = optionService.createOption('initialized', initialized ? 'true' : 'false', false); optionService.createOption('lastSyncedPull', '0', false); - optionService.createOption('lastSyncedPush', '0', false); - - let theme = 'dark'; // default based on the poll in https://github.com/zadam/trilium/issues/2516 - - if (utils.isElectron()) { - const {nativeTheme} = await import("electron"); - - theme = nativeTheme.shouldUseDarkColors ? 'dark' : 'light'; - } + optionService.createOption('lastSyncedPush', '0', false); optionService.createOption('theme', theme, false); diff --git a/src/services/setup.ts b/src/services/setup.ts index 0f3d26f58f..b632e8d08b 100644 --- a/src/services/setup.ts +++ b/src/services/setup.ts @@ -87,8 +87,8 @@ async function setupSyncFromSyncServer(syncServerHost: string, syncProxy: string } } - sqlInit.createDatabaseForSync(resp.options, syncServerHost, syncProxy); - + await sqlInit.createDatabaseForSync(resp.options, syncServerHost, syncProxy); + triggerSync(); return { result: 'success' }; diff --git a/src/services/sql_init.ts b/src/services/sql_init.ts index a937504765..7155147462 100644 --- a/src/services/sql_init.ts +++ b/src/services/sql_init.ts @@ -58,6 +58,7 @@ async function createInitialDatabase() { const schema = fs.readFileSync(`${resourceDir.DB_INIT_DIR}/schema.sql`, "utf-8"); const demoFile = fs.readFileSync(`${resourceDir.DB_INIT_DIR}/demo.zip`); + const defaultTheme = await getDefaultTheme(); let rootNote!: BNote; @@ -87,7 +88,7 @@ async function createInitialDatabase() { }).save(); optionsInitService.initDocumentOptions(); - optionsInitService.initNotSyncedOptions(true, {}); + optionsInitService.initNotSyncedOptions(true, defaultTheme, {}); optionsInitService.initStartupOptions(); password.resetPassword(); }); @@ -118,19 +119,20 @@ async function createInitialDatabase() { initDbConnection(); } -function createDatabaseForSync(options: OptionRow[], syncServerHost = '', syncProxy = '') { +async function createDatabaseForSync(options: OptionRow[], syncServerHost = '', syncProxy = '') { log.info("Creating database for sync"); if (isDbInitialized()) { throw new Error("DB is already initialized"); } + const defaultTheme = await getDefaultTheme(); const schema = fs.readFileSync(`${resourceDir.DB_INIT_DIR}/schema.sql`, "utf8"); sql.transactional(() => { sql.executeScript(schema); - optionsInitService.initNotSyncedOptions(false, { syncServerHost, syncProxy }); + optionsInitService.initNotSyncedOptions(false, defaultTheme, { syncServerHost, syncProxy }); // document options required for sync to kick off for (const opt of options) { @@ -141,6 +143,16 @@ function createDatabaseForSync(options: OptionRow[], syncServerHost = '', syncPr log.info("Schema and not synced options generated."); } +async function getDefaultTheme() { + if (utils.isElectron()) { + const {nativeTheme} = await import("electron"); + return nativeTheme.shouldUseDarkColors ? 'dark' : 'light'; + } else { + // default based on the poll in https://github.com/zadam/trilium/issues/2516 + return "dark"; + } +} + function setDbAsInitialized() { if (!isDbInitialized()) { optionService.setOption('initialized', 'true');