Skip to content

Commit

Permalink
fix: ElectronExternalApi fallback to super when electron app is undef…
Browse files Browse the repository at this point in the history
…ined, fixes #399 (#400)

* fix: ElectronExternalApi fallback to super when electron app is undefined; accept electron arg

* test: ElectronExternalApi fallbacks to super when electron app is undefined

* chore: Comsetic changes
  • Loading branch information
douglascayers authored Feb 1, 2024
1 parent ba23a9a commit a86e48f
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 24 deletions.
63 changes: 40 additions & 23 deletions src/main/ElectronExternalApi.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,45 @@
'use strict';

const electron = require('electron');
const path = require('path');
const NodeExternalApi = require('../node/NodeExternalApi');

class ElectronExternalApi extends NodeExternalApi {
/**
* @type {typeof Electron}
*/
electron = undefined;

/**
* @param {object} options
* @param {typeof Electron} [options.electron]
*/
constructor({ electron } = {}) {
super();
this.electron = electron;
}

getAppName() {
let appName;
try {
return electron.app?.name || electron.app?.getName();
appName = this.electron.app?.name || this.electron.app?.getName();
} catch {
return super.getAppName();
// fallback to default value below
}
return appName || super.getAppName();
}

getAppUserDataPath(appName) {
return this.getPath('userData') || super.getAppUserDataPath(appName);
}

getAppVersion() {
let appVersion;
try {
return electron.app?.getVersion();
appVersion = this.electron.app?.getVersion();
} catch {
return super.getAppVersion();
// fallback to default value below
}
return appVersion || super.getAppVersion();
}

getElectronLogPath() {
Expand All @@ -36,7 +53,7 @@ class ElectronExternalApi extends NodeExternalApi {
*/
getPath(name) {
try {
return electron.app?.getPath(name);
return this.electron.app?.getPath(name);
} catch {
return undefined;
}
Expand All @@ -55,8 +72,8 @@ class ElectronExternalApi extends NodeExternalApi {
}

isDev() {
if (electron.app?.isPackaged !== undefined) {
return !electron.app.isPackaged;
if (this.electron.app?.isPackaged !== undefined) {
return !this.electron.app.isPackaged;
}

if (typeof process.execPath === 'string') {
Expand All @@ -68,36 +85,36 @@ class ElectronExternalApi extends NodeExternalApi {
}

onAppEvent(eventName, handler) {
electron.app?.on(eventName, handler);
this.electron.app?.on(eventName, handler);

return () => {
electron.app?.off(eventName, handler);
this.electron.app?.off(eventName, handler);
};
}

onAppReady(handler) {
if (electron.app?.isReady()) {
if (this.electron.app?.isReady()) {
handler();
} else if (electron.app?.once) {
electron.app?.once('ready', handler);
} else if (this.electron.app?.once) {
this.electron.app?.once('ready', handler);
} else {
handler();
}
}

onEveryWebContentsEvent(eventName, handler) {
electron.webContents?.getAllWebContents().forEach((webContents) => {
this.electron.webContents?.getAllWebContents()?.forEach((webContents) => {
webContents.on(eventName, handler);
});

electron.app?.on('web-contents-created', onWebContentsCreated);
this.electron.app?.on('web-contents-created', onWebContentsCreated);

return () => {
electron.webContents?.getAllWebContents().forEach((webContents) => {
this.electron.webContents?.getAllWebContents().forEach((webContents) => {
webContents.off(eventName, handler);
});

electron.app?.off('web-contents-created', onWebContentsCreated);
this.electron.app?.off('web-contents-created', onWebContentsCreated);
};

function onWebContentsCreated(_, webContents) {
Expand All @@ -111,25 +128,25 @@ class ElectronExternalApi extends NodeExternalApi {
* @param {function} listener
*/
onIpc(channel, listener) {
electron.ipcMain?.on(channel, listener);
this.electron.ipcMain?.on(channel, listener);
}

onIpcInvoke(channel, listener) {
electron.ipcMain?.handle?.(channel, listener);
this.electron.ipcMain?.handle?.(channel, listener);
}

/**
* @param {string} url
* @param {Function} [logFunction]
*/
openUrl(url, logFunction = console.error) { // eslint-disable-line no-console
electron.shell?.openExternal(url).catch(logFunction);
this.electron.shell?.openExternal(url).catch(logFunction);
}

setPreloadFileForSessions({
filePath,
includeFutureSession = true,
getSessions = () => [electron.session?.defaultSession],
getSessions = () => [this.electron.session?.defaultSession],
}) {
for (const session of getSessions().filter(Boolean)) {
setPreload(session);
Expand All @@ -155,15 +172,15 @@ class ElectronExternalApi extends NodeExternalApi {
* @param {any} message
*/
sendIpc(channel, message) {
electron.BrowserWindow?.getAllWindows().forEach((wnd) => {
this.electron.BrowserWindow?.getAllWindows()?.forEach((wnd) => {
if (wnd.webContents?.isDestroyed() === false) {
wnd.webContents.send(channel, message);
}
});
}

showErrorBox(title, message) {
electron.dialog?.showErrorBox(title, message);
this.electron.dialog?.showErrorBox(title, message);
}
}

Expand Down
59 changes: 59 additions & 0 deletions src/main/__specs__/ElectronExternalApi.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
'use strict';

const humilePkg = require('humile/package.json');
const ElectronExternalApi = require('../ElectronExternalApi');

describe('ElectronExternalApi', () => {
describe('getAppName', () => {
it('gets the electron app name property', () => {
const electron = {
app: {
name: 'test-prop',
getName: () => 'test-func',
},
};

expect(api({ electron }).getAppName()).toBe('test-prop');
});

it('calls the electron app name function when no name property', () => {
const electron = {
app: {
name: undefined,
getName: () => 'test-func',
},
};

expect(api({ electron }).getAppName()).toBe('test-func');
});

it('fallsback to super function when no electron names', () => {
const electron = {
app: {
name: undefined,
getName: undefined,
},
};

expect(api({ electron }).getAppName()).toBe(humilePkg.name);
});

it('fallsback to super function when no electron', () => {
const electron = undefined;

expect(api({ electron }).getAppName()).toBe(humilePkg.name);
});
});

/**
* @param {object} options
* @param {NodeJS.Platform} options.platform
* @param {typeof Electron} options.electron
* @returns {ElectronExternalApi}
*/
function api({ electron, platform = process.platform } = {}) {
const externalApi = new ElectronExternalApi({ electron });
externalApi.setPlatform(platform);
return externalApi;
}
});
3 changes: 2 additions & 1 deletion src/main/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
'use strict';

const electron = require('electron');
const ElectronExternalApi = require('./ElectronExternalApi');
const { initialize } = require('./initialize');
const createDefaultLogger = require('../node/createDefaultLogger');

const externalApi = new ElectronExternalApi();
const externalApi = new ElectronExternalApi({ electron });
const defaultLogger = createDefaultLogger({
dependencies: { externalApi },
initializeFn: initialize,
Expand Down

0 comments on commit a86e48f

Please sign in to comment.