-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpreload.js
30 lines (25 loc) · 1.1 KB
/
preload.js
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
const { ipcRenderer, contextBridge } = require("electron");
let _callbacks = [];
ipcRenderer.on("fetched", (_event, queryId, payload) => {
const result = JSON.parse(payload);
_callbacks
.filter((callback) => callback.queryId === queryId)
.forEach((callback) => callback.next(result));
});
ipcRenderer.on("completed", (_event, queryId) => {
_callbacks
.filter((callback) => callback.queryId === queryId)
.forEach((callback) => callback.complete());
_callbacks = _callbacks.filter((callback) => callback.queryId !== queryId);
});
contextBridge.exposeInMainWorld("gqlmapi", {
startService: () => ipcRenderer.invoke("startService"),
stopService: () => ipcRenderer.invoke("stopService"),
parseQuery: (query) => ipcRenderer.invoke("parseQuery", query),
discardQuery: (queryId) => ipcRenderer.invoke("discardQuery", queryId),
fetchQuery: (queryId, operationName, variables, next, complete) => {
_callbacks.push({ queryId, next, complete });
ipcRenderer.send("fetchQuery", queryId, operationName, variables);
},
unsubscribe: (queryId) => ipcRenderer.invoke("unsubscribe", queryId),
});