-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.ts
389 lines (331 loc) · 14 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/**
* This module executes inside of electron's main process. You can start electron renderer process
* from here and communicate with the other processes through IPC.
*
* When running `npm run build` or `npm run build:main`, this file is compiled to `./src/main.js`
* using webpack. This gives us some performance wins.
*/
import path from 'path';
import { app, BrowserWindow, shell, ipcMain, IpcMainInvokeEvent } from 'electron';
// Removed until we have a release. See https://github.com/paranext/paranext-core/issues/83
/* import { autoUpdater } from 'electron-updater'; */
import windowStateKeeper from 'electron-window-state';
import '@main/global-this.model';
import dotnetDataProvider from '@main/services/dotnet-data-provider.service';
import logger from '@shared/services/logger.service';
import * as networkService from '@shared/services/network.service';
import * as commandService from '@shared/services/command.service';
import { resolveHtmlPath } from '@node/utils/util';
import extensionHostService from '@main/services/extension-host.service';
import networkObjectService from '@shared/services/network-object.service';
import extensionAssetProtocolService from '@main/services/extension-asset-protocol.service';
import { wait, serialize } from 'platform-bible-utils';
import { CommandNames } from 'papi-shared-types';
import { SerializedRequestType } from '@shared/utils/util';
import networkObjectStatusService from '@shared/services/network-object-status.service';
import { get } from '@shared/services/project-data-provider.service';
import { VerseRef } from '@sillsdev/scripture';
import { startNetworkObjectStatusService } from '@main/services/network-object-status.service-host';
import { DEV_MODE_RENDERER_INDICATOR } from '@shared/data/platform.data';
import { startProjectLookupService } from '@main/services/project-lookup.service-host';
import { PROJECT_INTERFACE_PLATFORM_BASE } from '@shared/models/project-data-provider.model';
const PROCESS_CLOSE_TIME_OUT = 2000;
async function main() {
// The network service relies on nothing else, and other things rely on it, so start it first
await networkService.initialize();
// The network object status service relies on seeing everything else start up later
await startNetworkObjectStatusService();
// The project lookup service relies on the network object status service
await startProjectLookupService();
// The .NET data provider relies on the network service and nothing else
dotnetDataProvider.start();
// TODO (maybe): Wait for signal from the .NET data provider process that it is ready
// The extension host service relies on the network service.
// Extensions inside the extension host might rely on the .NET data provider and each other
// Some extensions inside the extension host rely on the renderer to accept 'getWebView' commands.
// The renderer relies on the extension host, so something has to break the dependency loop.
// For now, the dependency loop is broken by retrying 'getWebView' in a loop for a while.
await extensionHostService.start();
// TODO (maybe): Wait for signal from the extension host process that it is ready (except 'getWebView')
// We could then wait for the renderer to be ready and signal the extension host
// #region Start the renderer
// Removed until we have a release. See https://github.com/paranext/paranext-core/issues/83
/* class AppUpdater {
constructor() {
autoUpdater.logger = logger;
autoUpdater.checkForUpdatesAndNotify();
}
} */
// Keep a global reference of the window object. If you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow: BrowserWindow | undefined;
if (process.env.NODE_ENV === 'production') {
const sourceMapSupport = await import('source-map-support');
sourceMapSupport.install();
}
const isDebug = process.env.NODE_ENV === 'development' || process.env.DEBUG_PROD === 'true';
if (isDebug) {
const electronDebug = await import('electron-debug');
electronDebug.default();
}
/** Install extensions into the Chromium renderer process */
async function installExtensions() {
const installer = await import('electron-devtools-installer');
const forceDownload = !!process.env.UPGRADE_EXTENSIONS;
const extensions = [installer.REACT_DEVELOPER_TOOLS];
return installer.default(extensions, forceDownload).catch(logger.info);
}
function getAssetPath(...paths: string[]): string {
return path.join(globalThis.resourcesPath, 'assets', ...paths);
}
/** Sets up the electron BrowserWindow renderer process */
const createWindow = async () => {
if (isDebug) {
await installExtensions();
}
// Load the previous state with fallback to defaults
const mainWindowState = windowStateKeeper({
defaultWidth: 1024,
defaultHeight: 728,
});
mainWindow = new BrowserWindow({
show: false,
x: mainWindowState.x,
y: mainWindowState.y,
width: mainWindowState.width,
height: mainWindowState.height,
icon: getAssetPath('icon.png'),
webPreferences: {
preload: app.isPackaged
? path.join(__dirname, 'preload.js')
: path.join(__dirname, '../../.erb/dll/preload.js'),
},
});
// Set our custom protocol handler to load assets from extensions
extensionAssetProtocolService.initialize();
// Register listeners on the window, so the state is updated automatically
// (the listeners will be removed when the window is closed)
// and restore the maximized or full screen state
mainWindowState.manage(mainWindow);
mainWindow.loadURL(
`${resolveHtmlPath('index.html')}${globalThis.isNoisyDevModeEnabled ? DEV_MODE_RENDERER_INDICATOR : ''}`,
);
mainWindow.on('ready-to-show', () => {
if (!mainWindow) {
throw new Error('"mainWindow" is not defined');
}
if (process.env.START_MINIMIZED) {
mainWindow.minimize();
} else {
mainWindow.show();
}
});
mainWindow.on('closed', () => {
mainWindow = undefined;
});
// 'null' to interact with external API
// eslint-disable-next-line no-null/no-null
mainWindow.setMenu(null);
// Open urls in the user's browser
// Note that webviews can get to this handler with window.open and anchor tags with
// target="_blank". Please revise web-view.service-host.ts as necessary if you make changes here
mainWindow.webContents.setWindowOpenHandler((handlerDetails) => {
// Only allow https urls
if (handlerDetails.url?.startsWith('https://')) shell.openExternal(handlerDetails.url);
return { action: 'deny' };
});
// Remove this if your app does not use auto updates
// eslint-disable-next-line
// Removed until we have a release. See https://github.com/paranext/paranext-core/issues/83
// new AppUpdater();
};
app.on('window-all-closed', () => {
// Respect the OSX convention of having the application in memory even
// after all windows have been closed
if (process.platform !== 'darwin') {
app.quit();
}
});
let isClosing = false;
app.on('will-quit', async (e) => {
if (!isClosing) {
// Prevent closing before graceful shutdown is complete.
// Also, in the future, this should allow a "are you sure?" dialog to display.
e.preventDefault();
isClosing = true;
networkService.shutdown();
await Promise.all([
dotnetDataProvider.waitForClose(PROCESS_CLOSE_TIME_OUT),
extensionHostService.waitForClose(PROCESS_CLOSE_TIME_OUT),
]);
// In development, the dotnet watcher was killed so we have to wait here.
if (process.env.NODE_ENV !== 'production') await wait(500);
app.quit();
} else {
dotnetDataProvider.kill();
extensionHostService.kill();
}
});
/** Map from ipc channel to handler function. Use with ipcRenderer.invoke */
const ipcHandlers: {
[ipcChannel: SerializedRequestType]: (
event: IpcMainInvokeEvent,
// We don't know the exact parameter types since ipc handlers can be anything
// eslint-disable-next-line @typescript-eslint/no-explicit-any
...args: any[]
) => Promise<unknown> | unknown;
} = {
'electronAPI:env.test': (_event, message: string) => `From main.ts: test ${message}`,
};
app
.whenReady()
// eslint-disable-next-line promise/always-return
.then(() => {
// Set up ipc handlers
Object.entries(ipcHandlers).forEach(([ipcChannel, ipcHandler]) =>
ipcMain.handle(ipcChannel, ipcHandler),
);
createWindow();
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 (!mainWindow) createWindow();
});
return undefined;
})
.catch(logger.info);
Object.entries(ipcHandlers).forEach(([ipcHandle, handler]) => {
networkService.registerRequestHandler(
// Re-assert type after passing through `forEach`.
// eslint-disable-next-line no-type-assertion/no-type-assertion
ipcHandle as SerializedRequestType,
// Handle with an empty event.
// eslint-disable-next-line no-type-assertion/no-type-assertion
async (...args: unknown[]) => handler({} as IpcMainInvokeEvent, ...args),
);
});
// #endregion
// #region Register commands
// `main.ts`'s command handler declarations are in `papi-shared-types.ts` so they can be picked up
// by papi-dts
// This map should allow any functions because commands can be any function type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const commandHandlers: { [commandName: string]: (...args: any[]) => any } = {
'platform.restartExtensionHost': async () => {
restartExtensionHost();
},
'platform.quit': async () => {
app.quit();
},
'platform.restart': async () => {
app.relaunch();
app.quit();
},
};
Object.entries(commandHandlers).forEach(([commandName, handler]) => {
// Re-assert type after passing through `forEach`.
// eslint-disable-next-line no-type-assertion/no-type-assertion
commandService.registerCommand(commandName as CommandNames, handler);
});
// #endregion
// #region Noisy dev tests
if (globalThis.isNoisyDevModeEnabled) {
// Register commands only for testing purposes
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const testCommandHandlers: { [commandName: string]: (...args: any[]) => any } = {
'test.echo': async (message: string) => {
return message;
},
'test.echoExtensionHost': async (message: string) => {
await commandService.sendCommand('test.addMany', 3, 5, 7, 1, 4);
return message;
},
'test.throwError': async (message: string) => {
throw new Error(`Test Error thrown in throwError command: ${message}`);
},
};
Object.entries(testCommandHandlers).forEach(([commandName, handler]) => {
// Re-assert type after passing through `forEach`.
// eslint-disable-next-line no-type-assertion/no-type-assertion
commandService.registerCommand(commandName as CommandNames, handler);
});
// Call a test command registered elsewhere
setTimeout(async () => {
logger.debug(
`Add Many (from EH): ${await commandService.sendCommand('test.addMany', 2, 5, 9, 7)}`,
);
}, 20000);
// Register a test network object
const testMain = {
doStuff: (stuff: string) => {
const result = `testMain did stuff: ${stuff}!`;
logger.debug(result);
return result;
},
dispose: () => {
logger.debug('testMain.dispose() ran in testMain');
return Promise.resolve(true);
},
};
const testMainDisposer = await networkObjectService.set('testMain', testMain);
testMain.doStuff('main things');
testMainDisposer.onDidDispose(() => {
logger.debug('testMain disposed in main message #1');
});
testMainDisposer.onDidDispose(() => {
logger.debug('testMain disposed in main message #2');
});
setTimeout(testMainDisposer.dispose, 20000);
// Create a test network object registered elsewhere
setTimeout(async () => {
let testExtensionHost = await networkObjectService.get<{
getVerse: () => Promise<string>;
}>('testExtensionHost');
if (testExtensionHost) {
logger.debug(`get verse: ${await testExtensionHost.getVerse()}`);
testExtensionHost.onDidDispose(() => {
logger.debug('testExtensionHost disposed in main');
testExtensionHost = undefined;
});
} else logger.error('Could not get testExtensionHost from main');
}, 5000);
// Dump all the network objects after things have settled a bit
setTimeout(async () => {
logger.info(
`Available network objects after 30 seconds: ${serialize(
await networkObjectStatusService.getAllNetworkObjectDetails(),
)}`,
);
}, 30000);
// Get a data provider and do something with it
setTimeout(async () => {
const usxPdp = await get(
'platformScripture.USX_Chapter',
'32664dc3288a28df2e2bb75ded887fc8f17a15fb',
);
const verse = await usxPdp.getChapterUSX(new VerseRef('JHN', '1', '1'));
logger.info(`Got PDP data: ${verse}`);
if (verse !== undefined) await usxPdp.setChapterUSX(new VerseRef('JHN', '1', '1'), verse);
const basePdp = await get(
PROJECT_INTERFACE_PLATFORM_BASE,
'32664dc3288a28df2e2bb75ded887fc8f17a15fb',
);
basePdp.setExtensionData(
{ extensionName: 'foo', dataQualifier: 'fooData' },
'This is the data from extension foo',
);
}, 20000);
}
// #endregion
}
async function restartExtensionHost() {
logger.info('Restarting extension host');
await extensionHostService.waitForClose(PROCESS_CLOSE_TIME_OUT);
logger.debug('Extension host closed, restarting now');
await extensionHostService.start();
}
(async () => {
logger.info('Starting main');
await main();
logger.info('Main is complete');
})().catch(logger.error);