Skip to content

Commit

Permalink
electron: Fix initial sync (closes #284)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
eliandoran committed Jul 29, 2024
1 parent bc5a1de commit 99ca701
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
12 changes: 2 additions & 10 deletions src/services/options_init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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);

Expand Down
4 changes: 2 additions & 2 deletions src/services/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' };
Expand Down
18 changes: 15 additions & 3 deletions src/services/sql_init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -87,7 +88,7 @@ async function createInitialDatabase() {
}).save();

optionsInitService.initDocumentOptions();
optionsInitService.initNotSyncedOptions(true, {});
optionsInitService.initNotSyncedOptions(true, defaultTheme, {});
optionsInitService.initStartupOptions();
password.resetPassword();
});
Expand Down Expand Up @@ -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) {
Expand All @@ -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');
Expand Down

0 comments on commit 99ca701

Please sign in to comment.