-
Notifications
You must be signed in to change notification settings - Fork 3
/
ipc-main-api.ts
29 lines (25 loc) · 975 Bytes
/
ipc-main-api.ts
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
import tcpPing from "tcp-ping";
import {app} from "electron";
import {interval} from "rxjs";
import {map, mergeMap, take} from "rxjs/operators";
import {promisify} from "util";
import {IPC_MAIN_API_SERVICE, ScannedIpcMainApiService} from "src/shared/ipc-main-api-definition";
export function register(): ScannedIpcMainApiService["ApiClient"] {
const api: ScannedIpcMainApiService["ApiImpl"] = {
ping: ({domain, times}) => interval(/*one second*/ 1000).pipe(
take(times),
mergeMap(() => promisify(tcpPing.ping)({address: domain, attempts: times})),
map(({avg: value}) => {
if (typeof value === "undefined" || isNaN(value)) {
throw new Error(`Host "${domain}" is unreachable`);
}
return {domain, value};
}),
),
async quitApp() {
app.quit();
},
};
IPC_MAIN_API_SERVICE.register(api);
return api;
}