1- import { app , Menu , BrowserWindow , ipcMain } from "electron" ;
1+ import { app , Menu , BrowserWindow , ipcMain , session } from "electron" ;
22import { createCapacitorElectronApp } from "@capacitor-community/electron" ;
3+ import { fork , ChildProcess } from 'child_process' ;
34
45import { join } from 'path' ;
56
7+ import { TableName ,
8+ IpcChannel ,
9+ ItemType ,
10+ IWhereItem ,
11+ IItem ,
12+ IFindStatement ,
13+ IFindCondition , } from './vendor' ;
14+
15+ import { conn , addItem , updateItem , getItems } from './crud' ;
16+
617import { loadingWindow , bookWindow } from './window.children' ;
718import { bookClone } from './bookOps' ;
819//import { createMenuTemplate } from './menu_template';
9- import { conn } from './crud' ;
10- import { onGetItems , onAddItem , } from './ipc' ;
20+
1121// The MainWindow object can be accessed via myCapacitorApp.getMainWindow()
1222
1323const myCapacitorApp = createCapacitorElectronApp ( ) ;
24+
1425const winChildren : Array < Electron . BrowserWindow > = [ ] ;
26+ const processChildren : Array < ChildProcess > = [ ] ;
27+
1528// This method will be called when Electron has finished
1629// initialization and is ready to create browser windows.
1730// Some Electron APIs can only be used after this event occurs.
1831app . on ( "ready" , ( ) => {
1932
33+ const filter = {
34+ urls : [
35+ 'http://localhost:10080/*' ,
36+ ]
37+ } ;
38+
39+ session . defaultSession . webRequest . onBeforeSendHeaders (
40+ filter ,
41+ ( details , callback ) => {
42+ console . log ( details ) ;
43+ details . requestHeaders [ 'Origin' ] = 'http://localhost:10080' ;
44+ callback ( { requestHeaders : details . requestHeaders } ) ;
45+ }
46+ ) ;
47+
48+ session . defaultSession . webRequest . onHeadersReceived (
49+ filter ,
50+ ( details , callback ) => {
51+ console . log ( details ) ;
52+ details . responseHeaders [ 'Access-Control-Allow-Origin' ] = [
53+ 'capacitor-electron://-'
54+ ] ;
55+ callback ( { responseHeaders : details . responseHeaders } ) ;
56+ }
57+ ) ;
58+
2059 myCapacitorApp . init ( ) ;
2160
2261 const booksDir = join ( app . getPath ( 'appData' ) , 'gbr_books' ) ;
62+ const serverProcess : ChildProcess = fork ( join ( __dirname , 'server.js' ) , [ '-d' , booksDir ] ) ;
63+ processChildren . push ( serverProcess ) ;
2364
2465 const mainWindow = myCapacitorApp . getMainWindow ( ) ;
2566 const webContents = mainWindow . webContents ;
@@ -40,7 +81,7 @@ app.on("ready", () => {
4081 } ) ;
4182
4283 ipcMain . on ( 'open-book' , ( event , book ) => {
43- bookWindow ( mainWindow , loadingWin , book , ( win ) => {
84+ bookWindow ( mainWindow , loadingWin , book , booksDir , ( win ) => {
4485 winChildren . push ( win ) ;
4586 } )
4687 } ) ;
@@ -65,6 +106,11 @@ app.on("window-all-closed", async () => {
65106 if ( winChildren . length > 0 ) {
66107 winChildren . map ( w => w = null ) ;
67108 }
109+ if ( processChildren . length > 0 ) {
110+ processChildren . map ( p => {
111+ p . kill ( 'SIGINT' ) ;
112+ } ) ;
113+ }
68114 app . quit ( ) ;
69115 }
70116 return 0
@@ -78,5 +124,27 @@ app.on("activate", function () {
78124} ) ;
79125
80126// Define any IPC or other custom functionality below here
81- onGetItems ;
82- onAddItem ;
127+ ipcMain . on ( 'add-item' , async ( event , item ) => {
128+ const table = item . table ;
129+ let _item : ItemType ;
130+ await addItem ( item ) . then ( _ => _item = _ ) ;
131+
132+ event . returnValue = _item ;
133+ } ) ;
134+
135+ ipcMain . on ( 'update-item' , async ( event , _item ) => {
136+ const table = _item . table ;
137+
138+ let item : ItemType ;
139+ await updateItem ( _item ) . then ( _ => item = _ ) ;
140+
141+ event . returnValue = item ;
142+ } ) ;
143+
144+ ipcMain . on ( 'get-items' , async ( event , getParam ) => {
145+ const table = getParam . table ;
146+
147+ let items : Array < ItemType > ;
148+ await getItems ( getParam ) . then ( _ => items = _ ) ;
149+ event . returnValue = items ;
150+ } ) ;
0 commit comments