import { app, BrowserWindow, ipcMain, IpcMainEvent } from 'electron';
import path from 'path';
import * as sqlite3 from 'sqlite3';
const createWindow = () => {
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
allowRunningInsecureContent: false,
contextIsolation: true,
enableRemoteModule: false,
nodeIntegration: false,
nodeIntegrationInWorker: false,
preload: path.join(app.getAppPath(), 'preload.js'),
},
});
win.loadFile(path.join(app.getAppPath(), 'index.html'));
win.on('closed', () => {
win = null;
});
};
// DB操作を追記
ipcMain.handle('connectTest', async () =>{
console.log('-- connection starts --');
const db = new sqlite3.Database('database.sqlite3');
db.serialize(function() {
db.run("CREATE TABLE lorem (info TEXT)");
let stmt = db.prepare("INSERT INTO lorem VALUES (?)");
for (let i = 0; i < 10; i++) {
stmt.run("Ipsum " + i);
}
stmt.finalize();
db.each("SELECT rowid AS id, info FROM lorem", function(err, row) {
console.log(row.id + ": " + row.info);
});
});
db.close();
console.log('-- It worked --');
});
これを IPC 通信を用いて renderer プロセスから呼び出してやれば良いわけです。
ここに来てついに React の出番となります。
preload にて定義した API のelectronIpcInvoke を呼び出します。
import React from 'react';
import ReactDOM from 'react-dom';
import './config/api.interface';
function btnClick(e):void {
window.api.electronIpcInvoke('connectTest','');
}
const container = document.getElementById('contents');
ReactDOM.render(
, container);
これで Connect ボタンをクリックすると IPC 通信で main プロセスの connectTest を呼び出して、データベースに接続・操作を行います。
この状態でビルド、実行してボタンをクリックした際に以下のように表示されれば成功です。
$ npm start
> electron ./dist/main.js
-- connection starts --
-- It worked --
1: Ipsum 0
2: Ipsum 1
3: Ipsum 2
4: Ipsum 3
5: Ipsum 4
6: Ipsum 5
7: Ipsum 6
8: Ipsum 7
9: Ipsum 8
10: Ipsum 9
TS1219: Experimental support for decorators is a feature that is subject to change in a future release. Set the 'experimentalDecorators' option in your 'tsconfig' or 'jsconfig' to remove this warning.
お好きなエディタで設定ファイルを開いて該当箇所のコメントアウトを削除してください。
$ vim tsconfig.json
/* Experimental Options */
"experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
$ vim /etc/systemd/timesyncd.conf
# This file is part of systemd.
#
# systemd is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Entries in this file show the compile time defaults.
# You can change settings by editing this file.
# Defaults can be restored by simply deleting this file.
#
# See timesyncd.conf(5) for details.
[Time]
NTP=ntp.nict.jp
#FallbackNTP=0.debian.pool.ntp.org 1.debian.pool.ntp.org 2.debian.pool.ntp.org 3.debian.pool.ntp.org
#RootDistanceMaxSec=5
#PollIntervalMinSec=32
#PollIntervalMaxSec=2048
FallbackNTP は NTP が定義されている場合には無視されますので気にしなくて結構です。
設定ファイルを書き換えましたらシステムサービスの NTP 機能を有効化して、状態を表示します。
$ sudo timedatectl set-ntp true
$ sudo timedatectl status
Local time: Sat 2020-09-05 23:24:53 CDT
Universal time: Sun 2020-09-06 04:24:53 UTC
RTC time: n/a
Time zone: America/Chicago (CDT, -0500)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no
ステータス表示 NTP service: active になっていれば、NTP 機能が稼働しています。
$ sudo systemctl restart systemd-timesyncd.service
$ sudo systemctl status systemd-timesyncd.service
\u25cf systemd-timesyncd.service - Network Time Synchronization
Loaded: loaded (/lib/systemd/system/systemd-timesyncd.service; enabled; vend
Drop-In: /lib/systemd/system/systemd-timesyncd.service.d
\u2514\u2500disable-with-time-daemon.conf
Active: active (running) since Sun 2020-09-06 12:45:38 HKT; 11s ago
Docs: man:systemd-timesyncd.service(8)
Main PID: 22048 (systemd-timesyn)
Status: "Synchronized to time server for the first time [2001:df0:232:eea0::fff4]:123 (ntp.nict.jp)."
Memory: 856.0K
CGroup: /system.slice/systemd-timesyncd.service
\u2514\u250022048 /lib/systemd/systemd-timesyncd
以下のようにNICT サーバに接続できていることが表示されれば成功です。
“Synchronized to time server for the first time [2001:df0:232:eea0::fff4]:123 (ntp.nict.jp).”