Skip to content
This repository was archived by the owner on Jun 24, 2025. It is now read-only.

Commit 99ca701

Browse files
committed
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.
1 parent bc5a1de commit 99ca701

File tree

3 files changed

+19
-15
lines changed

3 files changed

+19
-15
lines changed

src/services/options_init.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ interface NotSyncedOpts {
1616
syncProxy?: string;
1717
}
1818

19-
async function initNotSyncedOptions(initialized: boolean, opts: NotSyncedOpts = {}) {
19+
async function initNotSyncedOptions(initialized: boolean, theme: string, opts: NotSyncedOpts = {}) {
2020
optionService.createOption('openNoteContexts', JSON.stringify([
2121
{
2222
notePath: 'root',
@@ -32,15 +32,7 @@ async function initNotSyncedOptions(initialized: boolean, opts: NotSyncedOpts =
3232
optionService.createOption('initialized', initialized ? 'true' : 'false', false);
3333

3434
optionService.createOption('lastSyncedPull', '0', false);
35-
optionService.createOption('lastSyncedPush', '0', false);
36-
37-
let theme = 'dark'; // default based on the poll in https://github.com/zadam/trilium/issues/2516
38-
39-
if (utils.isElectron()) {
40-
const {nativeTheme} = await import("electron");
41-
42-
theme = nativeTheme.shouldUseDarkColors ? 'dark' : 'light';
43-
}
35+
optionService.createOption('lastSyncedPush', '0', false);
4436

4537
optionService.createOption('theme', theme, false);
4638

src/services/setup.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ async function setupSyncFromSyncServer(syncServerHost: string, syncProxy: string
8787
}
8888
}
8989

90-
sqlInit.createDatabaseForSync(resp.options, syncServerHost, syncProxy);
91-
90+
await sqlInit.createDatabaseForSync(resp.options, syncServerHost, syncProxy);
91+
9292
triggerSync();
9393

9494
return { result: 'success' };

src/services/sql_init.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ async function createInitialDatabase() {
5858

5959
const schema = fs.readFileSync(`${resourceDir.DB_INIT_DIR}/schema.sql`, "utf-8");
6060
const demoFile = fs.readFileSync(`${resourceDir.DB_INIT_DIR}/demo.zip`);
61+
const defaultTheme = await getDefaultTheme();
6162

6263
let rootNote!: BNote;
6364

@@ -87,7 +88,7 @@ async function createInitialDatabase() {
8788
}).save();
8889

8990
optionsInitService.initDocumentOptions();
90-
optionsInitService.initNotSyncedOptions(true, {});
91+
optionsInitService.initNotSyncedOptions(true, defaultTheme, {});
9192
optionsInitService.initStartupOptions();
9293
password.resetPassword();
9394
});
@@ -118,19 +119,20 @@ async function createInitialDatabase() {
118119
initDbConnection();
119120
}
120121

121-
function createDatabaseForSync(options: OptionRow[], syncServerHost = '', syncProxy = '') {
122+
async function createDatabaseForSync(options: OptionRow[], syncServerHost = '', syncProxy = '') {
122123
log.info("Creating database for sync");
123124

124125
if (isDbInitialized()) {
125126
throw new Error("DB is already initialized");
126127
}
127128

129+
const defaultTheme = await getDefaultTheme();
128130
const schema = fs.readFileSync(`${resourceDir.DB_INIT_DIR}/schema.sql`, "utf8");
129131

130132
sql.transactional(() => {
131133
sql.executeScript(schema);
132134

133-
optionsInitService.initNotSyncedOptions(false, { syncServerHost, syncProxy });
135+
optionsInitService.initNotSyncedOptions(false, defaultTheme, { syncServerHost, syncProxy });
134136

135137
// document options required for sync to kick off
136138
for (const opt of options) {
@@ -141,6 +143,16 @@ function createDatabaseForSync(options: OptionRow[], syncServerHost = '', syncPr
141143
log.info("Schema and not synced options generated.");
142144
}
143145

146+
async function getDefaultTheme() {
147+
if (utils.isElectron()) {
148+
const {nativeTheme} = await import("electron");
149+
return nativeTheme.shouldUseDarkColors ? 'dark' : 'light';
150+
} else {
151+
// default based on the poll in https://github.com/zadam/trilium/issues/2516
152+
return "dark";
153+
}
154+
}
155+
144156
function setDbAsInitialized() {
145157
if (!isDbInitialized()) {
146158
optionService.setOption('initialized', 'true');

0 commit comments

Comments
 (0)