api running in electron main process

This commit is contained in:
Jan Prochazka
2021-12-25 09:23:03 +01:00
parent 2ff9e8c452
commit 24071ebde7
11 changed files with 173 additions and 104 deletions

View File

@@ -1,6 +1,7 @@
import resolveApi, { resolveApiHeaders } from './resolveApi';
import { writable } from 'svelte/store';
import { cacheClean } from './cache';
import getElectron from './getElectron';
// import socket from './socket';
let eventSource;
@@ -13,37 +14,53 @@ function wantEventSource() {
}
export async function apiCall(route: string, args: {} = undefined) {
const resp = await fetch(`${resolveApi()}/${route}`, {
method: 'POST',
cache: 'no-cache',
headers: {
'Content-Type': 'application/json',
...resolveApiHeaders(),
},
body: JSON.stringify(args),
});
return resp.json();
const electron = getElectron();
if (electron) {
const resp = await electron.invoke(route.replace('/', '-'), args);
return resp;
} else {
const resp = await fetch(`${resolveApi()}/${route}`, {
method: 'POST',
cache: 'no-cache',
headers: {
'Content-Type': 'application/json',
...resolveApiHeaders(),
},
body: JSON.stringify(args),
});
return resp.json();
}
}
const apiHandlers = new WeakMap();
export function apiOn(event: string, handler: Function) {
wantEventSource();
if (!apiHandlers.has(handler)) {
const handlerProxy = e => {
// console.log('RECEIVED', e.type, JSON.parse(e.data));
handler(JSON.parse(e.data));
};
apiHandlers.set(handler, handlerProxy);
}
const electron = getElectron();
if (electron) {
electron.addEventListener(event, handler);
} else {
wantEventSource();
if (!apiHandlers.has(handler)) {
const handlerProxy = e => {
// console.log('RECEIVED', e.type, JSON.parse(e.data));
handler(JSON.parse(e.data));
};
apiHandlers.set(handler, handlerProxy);
}
eventSource.addEventListener(event, apiHandlers.get(handler));
eventSource.addEventListener(event, apiHandlers.get(handler));
}
}
export function apiOff(event: string, handler: Function) {
wantEventSource();
if (apiHandlers.has(handler)) {
eventSource.removeEventListener(event, apiHandlers.get(handler));
const electron = getElectron();
if (electron) {
electron.removeEventListener(event, handler);
} else {
wantEventSource();
if (apiHandlers.has(handler)) {
eventSource.removeEventListener(event, apiHandlers.get(handler));
}
}
}