From e80d16acb4db45bd0196fa609e319ffdaa5cad38 Mon Sep 17 00:00:00 2001 From: YuShifan <894402575bt@gmail.com> Date: Sat, 31 Jul 2021 14:54:58 +0800 Subject: [PATCH 1/2] refactor(settings): refactor settings service --- src/background.ts | 52 ++++++++++++++----------- src/database/services/SettingService.ts | 12 ++++++ 2 files changed, 42 insertions(+), 22 deletions(-) diff --git a/src/background.ts b/src/background.ts index 68f06be94..62976f71e 100644 --- a/src/background.ts +++ b/src/background.ts @@ -30,10 +30,6 @@ let win: BrowserWindow | null let menu: Menu | null -const windowSize = db.get('windowSize') - -const theme = db.get('settings.currentTheme') - // Scheme must be registered before the app is ready protocol.registerSchemesAsPrivileged([{ scheme: 'app', privileges: { secure: true, standard: true } }]) @@ -85,6 +81,10 @@ function beforeAppQuit() { app.quit() } +let windowSize: WindowSizeModel +let theme: Theme +let autoCheckUpdate: boolean + async function beforeWindowReady() { await ConnectionInit({ doMigrations: true, @@ -92,6 +92,15 @@ async function beforeWindowReady() { } as initOptionModel) const { settingService } = useServices() await settingService.setSetting() + const setting = await settingService.getSetting() + if (setting) { + windowSize = { + height: setting.height, + width: setting.width, + } + theme = setting.currentTheme + autoCheckUpdate = setting.autoCheck + } } function createWindow() { @@ -135,29 +144,11 @@ function createWindow() { }) } -// Quit when all windows are closed. -app.on('window-all-closed', () => { - // On macOS it is common for applications and their menu bar - // to stay active until the user quits explicitly with Cmd + Q - if (!isMac) { - beforeAppQuit() - } -}) - -app.on('activate', () => { - // On macOS it's common to re-create a window in the app when the - // dock icon is clicked and there are no other windows open. - if (win === null) { - createWindow() - } -}) - // This method will be called when Electron has finished // initialization and is ready to create browser windows. // Some APIs can only be used after this event occurs. app.on('ready', async () => { await beforeWindowReady() - const autoCheckUpdate = db.get('settings.autoCheck') if (isDevelopment && !process.env.IS_TEST) { // Install Vue Devtools try { @@ -173,6 +164,23 @@ app.on('ready', async () => { } }) +// Quit when all windows are closed. +app.on('window-all-closed', () => { + // On macOS it is common for applications and their menu bar + // to stay active until the user quits explicitly with Cmd + Q + if (!isMac) { + beforeAppQuit() + } +}) + +app.on('activate', () => { + // On macOS it's common to re-create a window in the app when the + // dock icon is clicked and there are no other windows open. + if (win === null) { + createWindow() + } +}) + // Disabled create new window app.on('web-contents-created', (event, contents) => { // tslint:disable-next-line:no-shadowed-variable diff --git a/src/database/services/SettingService.ts b/src/database/services/SettingService.ts index 805625bff..ba1c1e73e 100644 --- a/src/database/services/SettingService.ts +++ b/src/database/services/SettingService.ts @@ -11,6 +11,18 @@ export default class SettingService { ) {} public async setSetting() { + const data = await this.collectionRepository.find() + if (data.length) { + return + } return await this.collectionRepository.insert({}) } + + public async getSetting() { + const data = await this.collectionRepository.find() + if (!data.length) { + return + } + return data[0] + } } From 70fc98276035e94e42b04f7db4ae287607e1fad8 Mon Sep 17 00:00:00 2001 From: YuShifan <894402575bt@gmail.com> Date: Sat, 31 Jul 2021 17:37:07 +0800 Subject: [PATCH 2/2] fix(main): fix await run db migrations --- src/background.ts | 46 +++++++++++++---------------------- src/database/useConnection.ts | 5 ++-- src/main.ts | 10 ++++---- 3 files changed, 25 insertions(+), 36 deletions(-) diff --git a/src/background.ts b/src/background.ts index 62976f71e..03afd1f71 100644 --- a/src/background.ts +++ b/src/background.ts @@ -1,11 +1,10 @@ -'use strict' - +import 'reflect-metadata' // Required by TypoORM. +;('use strict') import { app, protocol, BrowserWindow, ipcMain, shell, Menu } from 'electron' import { createProtocol } from 'vue-cli-plugin-electron-builder/lib' import installExtension, { VUEJS_DEVTOOLS } from 'electron-devtools-installer' import { updateConnectionMessage } from '@/api/connection' import { quitAndRenameLogger } from './utils/logger' -import db from './database/index' import updateChecker from './main/updateChecker' import getMenuTemplate from './main/getMenuTemplate' import saveFile from './main/saveFile' @@ -14,13 +13,14 @@ import newWindow from './main/newWindow' import useConnection, { initOptionModel } from '@/database/useConnection' import useServices from '@/database/useServices' -interface WindowSizeModel { - width: number - height: number -} - declare const __static: string +let theme: Theme = 'light' +let autoCheckUpdate: boolean = true +const windowSize = { + width: 1025, + height: 749, +} const isDevelopment: boolean = process.env.NODE_ENV !== 'production' const isMac: boolean = process.platform === 'darwin' @@ -81,29 +81,18 @@ function beforeAppQuit() { app.quit() } -let windowSize: WindowSizeModel -let theme: Theme -let autoCheckUpdate: boolean - -async function beforeWindowReady() { - await ConnectionInit({ - doMigrations: true, - undoMigrations: false, - } as initOptionModel) +async function createWindow() { + // Init tables and connect to local database. + await ConnectionInit({ doMigrations: true } as initOptionModel) const { settingService } = useServices() await settingService.setSetting() const setting = await settingService.getSetting() if (setting) { - windowSize = { - height: setting.height, - width: setting.width, - } theme = setting.currentTheme autoCheckUpdate = setting.autoCheck + windowSize.height = setting.height + windowSize.width = setting.width } -} - -function createWindow() { // Create the browser window. win = new BrowserWindow({ ...windowSize, @@ -142,13 +131,16 @@ function createWindow() { win = null beforeAppQuit() }) + handleIpcMessages() + if (autoCheckUpdate) { + updateChecker() + } } // This method will be called when Electron has finished // initialization and is ready to create browser windows. // Some APIs can only be used after this event occurs. app.on('ready', async () => { - await beforeWindowReady() if (isDevelopment && !process.env.IS_TEST) { // Install Vue Devtools try { @@ -158,10 +150,6 @@ app.on('ready', async () => { } } createWindow() - handleIpcMessages() - if (autoCheckUpdate) { - updateChecker() - } }) // Quit when all windows are closed. diff --git a/src/database/useConnection.ts b/src/database/useConnection.ts index ce2c3ea97..464b93979 100644 --- a/src/database/useConnection.ts +++ b/src/database/useConnection.ts @@ -12,10 +12,11 @@ const useConnection = () => { useContainer(Container) sqlConnection = await createConnection(ORMConfig) if (initOption.doMigrations) { - sqlConnection.runMigrations() + await sqlConnection.runMigrations() } else if (initOption.undoMigrations) { - sqlConnection.undoLastMigration() + await sqlConnection.undoLastMigration() } + return sqlConnection } async function ConnectionDestory() { if (sqlConnection) { diff --git a/src/main.ts b/src/main.ts index 99040d106..f20b4d5e2 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,24 +1,24 @@ import 'reflect-metadata' // Required by TypoORM. import Vue from 'vue' +import VueI18n from 'vue-i18n' +import VueClipboard from 'vue-clipboard2' +import path from 'path' +import log4js from 'log4js' import 'element-ui/lib/theme-chalk/index.css' import ElementLocale from 'element-ui/lib/locale' + import App from './App.vue' import router from './router/index' import store from './store/index' -import VueI18n from 'vue-i18n' -import VueClipboard from 'vue-clipboard2' import Lang from './lang' import element from './utils/element' import VueLog4js from './plugins/logPlugin/index' -import log4js from 'log4js' import { getOrCreateLogDir } from './utils/logger' import logConfig from './plugins/logPlugin/logConfig.json' -import path from 'path' import useConnection, { initOptionModel } from './database/useConnection' const { ConnectionInit } = useConnection() - // Init typeORM connection before Vue APP start, after this DI services are available. ConnectionInit({ doMigrations: false, undoMigrations: false } as initOptionModel).then(() => { const LOG_DIR = getOrCreateLogDir()