Skip to content

Commit

Permalink
feat(general): only one instance of Lumi possible in all OSes (#1652)
Browse files Browse the repository at this point in the history
  • Loading branch information
sr258 authored Jul 9, 2021
1 parent 65bdd69 commit 31b8dba
Showing 1 changed file with 164 additions and 129 deletions.
293 changes: 164 additions & 129 deletions server/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,6 @@ let currentPath: string = '/';
const isDevelopment = process.env.NODE_ENV === 'development';
const BrowserWindow = electron.BrowserWindow;

const serverConfig = serverConfigFactory(
process.env.USERDATA || app.getPath('userData')
);
Sentry.init({
dsn: 'http://1f4ae874b81a48ed8e22fe6e9d52ed1b@sentry.lumi.education/3',
release: app.getVersion(),
environment: process.env.NODE_ENV,
beforeSend: async (event: Sentry.Event) => {
if (settingsCache.getSettings().bugTracking) {
return event;
}
return null;
}
});

process.on('uncaughtException', (error) => {
Sentry.captureException(error);
log.error(error);
});

export function createMainWindow(websocketArg: SocketIO.Server): void {
if (!mainWindow) {
const window = new BrowserWindow({
Expand Down Expand Up @@ -119,123 +99,178 @@ export function createMainWindow(websocketArg: SocketIO.Server): void {
}
}

// quit application when all windows are closed
app.on('window-all-closed', () => {
// on macOS it is common for applications to stay open until the user explicitly quits
if (process.platform !== 'darwin') {
app.quit();
}
});
const gotSingleInstanceLock = app.requestSingleInstanceLock();

// Handle open file events for MacOS
app.on('open-file', (event: electron.Event, openedFilePath: string) => {
log.debug('Electron open-file event caught');
if (!gotSingleInstanceLock) {
app.quit();
} else {
const serverConfig = serverConfigFactory(
process.env.USERDATA || app.getPath('userData')
);
Sentry.init({
dsn: 'http://1f4ae874b81a48ed8e22fe6e9d52ed1b@sentry.lumi.education/3',
release: app.getVersion(),
environment: process.env.NODE_ENV,
beforeSend: async (event: Sentry.Event) => {
if (settingsCache.getSettings().bugTracking) {
return event;
}
return null;
}
});

/**
* If we are in macOS and the process is still active but there is no
* window, we need to create one.
*/
if (mainWindow === null) {
createMainWindow(websocket);
}
process.on('uncaughtException', (error) => {
Sentry.captureException(error);
log.error(error);
});

delayedWebsocketEmitter.emit('action', {
payload: {
paths: [openedFilePath]
},
type: 'OPEN_H5P'
app.on('second-instance', (event, argv) => {
if (mainWindow) {
if (mainWindow.isMinimized()) {
mainWindow.restore();
}
mainWindow.focus();

if (argv.length >= 2) {
// Check if there are H5Ps specified in the command line args and
// load them (Windows only).
argv.splice(0, 1);
const openFilePaths = argv.filter((arg) =>
arg.endsWith('.h5p')
);
if (openFilePaths.length > 0) {
log.debug(`Opening file(s): ${openFilePaths.join(' ')}`);
delayedWebsocketEmitter.emit('action', {
payload: {
paths: openFilePaths
},
type: 'OPEN_H5P'
});
}
}
}
});

event.preventDefault();
});
// quit application when all windows are closed
app.on('window-all-closed', () => {
// on macOS it is common for applications to stay open until the user explicitly quits
if (process.platform !== 'darwin') {
app.quit();
}
});

app.on('activate', () => {
// on macOS it is common to re-create a window even after all windows have been closed
if (mainWindow === null) {
createMainWindow(websocket);
}
});

app.on('before-quit', async () => {
try {
if (settingsCache.getSettings().usageStatistics) {
const data = {
url: '/Lumi',
_id: await machineId(),
uid: await machineId(),
e_c: 'App',
e_a: 'quit',
lang: electron.app.getLocale(),
ua: os.type()
};
matomo.track(data);
// Handle open file events for MacOS
app.on('open-file', (event: electron.Event, openedFilePath: string) => {
log.debug('Electron open-file event caught');

/**
* If we are in macOS and the process is still active but there is no
* window, we need to create one.
*/
if (mainWindow === null) {
createMainWindow(websocket);
}
} catch (error) {
Sentry.captureException(error);
}
});

// create main BrowserWindow when electron is ready
app.on('ready', async () => {
log.info('app is ready');
const server = await httpServerFactory(
serverConfigFactory(process.env.USERDATA || app.getPath('userData')),
mainWindow,
{
devMode: app.commandLine.hasSwitch('dev'),
libraryDir:
app.commandLine.getSwitchValue('libs') !== ''
? app.commandLine.getSwitchValue('libs')
: undefined

delayedWebsocketEmitter.emit('action', {
payload: {
paths: [openedFilePath]
},
type: 'OPEN_H5P'
});

event.preventDefault();
});

app.on('activate', () => {
// on macOS it is common to re-create a window even after all windows have been closed
if (mainWindow === null) {
createMainWindow(websocket);
}
);
log.info('server booted');

port = (server.address() as any).port;
log.info(`port is ${port}`);

websocket = websocketFactory(server);
log.info('websocket created');

delayedWebsocketEmitter.setWebsocket(websocket);

updater(app, websocket, serverConfig);
log.info('updater started');

createMainWindow(websocket);
log.info('window created');

const argv = process.argv;
if (process.platform === 'win32' && argv.length >= 2) {
// Check if there are H5Ps specified in the command line args and
// load them (Windows only).
argv.splice(0, 1);
const openFilePaths = argv.filter((arg) => arg.endsWith('.h5p'));
if (openFilePaths.length > 0) {
log.debug(`Opening file(s): ${openFilePaths.join(' ')}`);
delayedWebsocketEmitter.emit('action', {
payload: {
paths: openFilePaths
},
type: 'OPEN_H5P'
});
});

app.on('before-quit', async () => {
try {
if (settingsCache.getSettings().usageStatistics) {
const data = {
url: '/Lumi',
_id: await machineId(),
uid: await machineId(),
e_c: 'App',
e_a: 'quit',
lang: electron.app.getLocale(),
ua: os.type()
};
matomo.track(data);
}
} catch (error) {
Sentry.captureException(error);
}
}
});

// create main BrowserWindow when electron is ready
app.on('ready', async () => {
log.info('app is ready');
const server = await httpServerFactory(
serverConfigFactory(
process.env.USERDATA || app.getPath('userData')
),
mainWindow,
{
devMode: app.commandLine.hasSwitch('dev'),
libraryDir:
app.commandLine.getSwitchValue('libs') !== ''
? app.commandLine.getSwitchValue('libs')
: undefined
}
);
log.info('server booted');

port = (server.address() as any).port;
log.info(`port is ${port}`);

try {
if (settingsCache.getSettings().usageStatistics) {
const data = {
url: '/Lumi',
_id: await machineId(),
uid: await machineId(),
e_c: 'App',
e_a: 'start',
lang: electron.app.getLocale(),
ua: os.type()
};
matomo.track(data);
websocket = websocketFactory(server);
log.info('websocket created');

delayedWebsocketEmitter.setWebsocket(websocket);

updater(app, websocket, serverConfig);
log.info('updater started');

createMainWindow(websocket);
log.info('window created');

const argv = process.argv;
if (process.platform === 'win32' && argv.length >= 2) {
// Check if there are H5Ps specified in the command line args and
// load them (Windows only).
argv.splice(0, 1);
const openFilePaths = argv.filter((arg) => arg.endsWith('.h5p'));
if (openFilePaths.length > 0) {
log.debug(`Opening file(s): ${openFilePaths.join(' ')}`);
delayedWebsocketEmitter.emit('action', {
payload: {
paths: openFilePaths
},
type: 'OPEN_H5P'
});
}
}
} catch (error) {
Sentry.captureException(error);
}
});

try {
if (settingsCache.getSettings().usageStatistics) {
const data = {
url: '/Lumi',
_id: await machineId(),
uid: await machineId(),
e_c: 'App',
e_a: 'start',
lang: electron.app.getLocale(),
ua: os.type()
};
matomo.track(data);
}
} catch (error) {
Sentry.captureException(error);
}
});
}

0 comments on commit 31b8dba

Please sign in to comment.