Skip to content

Commit 733dae4

Browse files
committed
Refactored the electron backend.
1 parent c989ac1 commit 733dae4

File tree

3 files changed

+11
-11
lines changed

3 files changed

+11
-11
lines changed

electron/src/book.backend.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ import {
1919
import { Book, Record } from './models';
2020

2121
export class BookBackend {
22-
crud = new CRUD();
23-
2422
constructor (
23+
private crud: CRUD,
2524
private booksDir: string,
2625
private book: Book,
2726
private insideWindow: BrowserWindow,

electron/src/crud.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ConnectionManager, Connection } from 'typeorm';
22

33
import { Book, Category, Writer, Website, Record } from './models';
4+
import { join } from 'path';
45

56
import {
67
TTableName,
@@ -13,14 +14,14 @@ import {
1314
export class CRUD {
1415
conn: Connection;
1516

16-
constructor () {
17+
constructor (appDir: string) {
1718
const connectionManager = new ConnectionManager();
1819
this.conn = connectionManager.create({
1920
type: 'sqlite',
2021
synchronize: true,
2122
logging: false,
2223
logger: 'simple-console',
23-
database: 'db.sqlite',
24+
database: join(appDir, 'db.sqlite'),
2425
entities: [ Category, Website, Writer, Book, Record ],
2526
});
2627

electron/src/index.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ export default class Main {
2727
static myCapacitorApp = createCapacitorElectronApp();
2828
static winChildren: Array<Electron.BrowserWindow> = [];
2929
static processChildren: Array<ChildProcess> = [];
30-
static crud = new CRUD();
30+
static booksDir = join(app.getPath('appData'), 'gbr_books');
31+
static crud = new CRUD(Main.booksDir);
3132

3233
// This method will be called when Electron has finished
3334
// initialization and is ready to create browser windows.
@@ -63,8 +64,7 @@ export default class Main {
6364

6465
Main.myCapacitorApp.init();
6566

66-
const booksDir = join(app.getPath('appData'), 'gbr_books');
67-
const serverProcess: ChildProcess = fork(join(__dirname, 'server.js'), ['-d', booksDir]);
67+
const serverProcess: ChildProcess = fork(join(__dirname, 'server.js'), ['-d', Main.booksDir]);
6868
Main.processChildren.push(serverProcess);
6969

7070
const mainWindow = Main.myCapacitorApp.getMainWindow();
@@ -88,17 +88,17 @@ export default class Main {
8888
});
8989

9090
ipcMain.on('open-book', (event, book: Book) => {
91-
const _book = new BookBackend(booksDir, book, mainWindow, loadingWin)
91+
const _book = new BookBackend(Main.crud, Main.booksDir, book, mainWindow, loadingWin)
9292
_book.open(w => Main.winChildren.push(w));
9393
});
9494

9595
ipcMain.on('open-book-with-path', (event, msg: IBookWithPath) => {
96-
const book = new BookBackend(booksDir, msg.book, mainWindow, loadingWin, msg.path)
96+
const book = new BookBackend(Main.crud, Main.booksDir, msg.book, mainWindow, loadingWin, msg.path)
9797
book.open(w => Main.winChildren.push(w));
9898
});
9999

100100
ipcMain.on('download-book', (event, book: Book) => {
101-
const _book = new BookBackend(booksDir, book, mainWindow, loadingWin);
101+
const _book = new BookBackend(Main.crud, Main.booksDir, book, mainWindow, loadingWin);
102102
_book.download(cp => Main.processChildren.push(cp));
103103
});
104104

@@ -121,7 +121,7 @@ export default class Main {
121121

122122
if(query.table === 'Book'){
123123
const book = query.item as Book;
124-
const bookDir = join(booksDir, book.website.uri, book.writer.name, book.name);
124+
const bookDir = join(Main.booksDir, book.website.uri, book.writer.name, book.name);
125125
await remove(bookDir)
126126
.then(() => res.message.push(',已成功从文件系统移除'))
127127
.catch((e: Error) => res.message.push(e.message));

0 commit comments

Comments
 (0)