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.
This commit is contained in:
Elian Doran 2024-07-29 20:41:28 +03:00
parent bc5a1de9b7
commit 99ca701a5c
3 changed files with 19 additions and 15 deletions

View File

@ -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);

View File

@ -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' };

View File

@ -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');