electron initialization without remote

This commit is contained in:
Jan Prochazka
2021-12-16 11:26:29 +01:00
parent 1fdf942715
commit 0a7c56dace
6 changed files with 107 additions and 40 deletions

View File

@@ -1,15 +1,23 @@
import axios from 'axios';
import resolveApi, { resolveApiHeaders } from './resolveApi';
const axiosInstance = axios.create({
baseURL: resolveApi(),
});
let axiosInstance;
axiosInstance.defaults.headers = {
'Cache-Control': 'no-cache',
Pragma: 'no-cache',
Expires: '0',
...resolveApiHeaders(),
};
function recreateAxiosInstance() {
axiosInstance = axios.create({
baseURL: resolveApi(),
});
axiosInstance.defaults.headers = {
'Cache-Control': 'no-cache',
Pragma: 'no-cache',
Expires: '0',
...resolveApiHeaders(),
};
}
window['dbgate_recreateAxiosInstance'] = recreateAxiosInstance;
recreateAxiosInstance();
export default axiosInstance;

View File

@@ -1,7 +1,57 @@
export default function getElectron() {
class ElectronApi {
public port?: number;
public authorization?: string;
private ipcRenderer = getIpcRenderer();
constructor(args) {
this.port = args.port;
this.authorization = args.authorization;
}
send(msg, args) {
this.ipcRenderer.send(msg, args);
}
}
let apiInstance = null;
function initializeElectron(args) {
// console.log('Initialize electron with args:', args);
apiInstance = new ElectronApi(args);
if (window['dbgate_recreateAxiosInstance']) {
// console.log('Recreating axios instance');
window['dbgate_recreateAxiosInstance']();
}
}
window['dbgate_initializeElectron'] = initializeElectron;
function getIpcRenderer() {
if (window['require']) {
const electron = window['require']('electron');
return electron;
return electron?.ipcRenderer;
}
return null;
}
export function shouldWaitForElectronInitialize() {
return !!getIpcRenderer() && !apiInstance;
}
export default function getElectron(): ElectronApi {
return apiInstance;
// try {
// // @ts-ignore
// return ipcRenderer;
// } catch (e) {
// return null;
// }
// if (window['require']) {
// const electron = window['require']('electron');
// console.log('electron?.ipcRenderer', electron?.ipcRenderer);
// return electron?.ipcRenderer;
// }
// return null;
}

View File

@@ -1,18 +1,14 @@
import getElectron from './getElectron';
let apiUrl = null;
try {
apiUrl = process.env.API_URL;
} catch {}
export default function resolveApi() {
if (window['require']) {
const electron = window['require']('electron');
if (electron) {
const port = electron.remote.getGlobal('port');
if (port) {
return `http://localhost:${port}`;
}
}
const electron = getElectron();
if (electron?.port) {
return `http://localhost:${electron.port}`;
}
if (apiUrl) {
@@ -22,14 +18,13 @@ export default function resolveApi() {
}
export function resolveApiHeaders() {
if (window['require']) {
const electron = window['require']('electron');
const electron = getElectron();
if (electron) {
return {
Authorization: electron.remote.getGlobal('authorization'),
};
}
if (electron?.authorization) {
return {
Authorization: electron.authorization,
};
}
return {};
}