feat: use uuid to get the correct ipc response only in the promise call that the request came from
This commit is contained in:
parent
763d954775
commit
3079c33d37
|
@ -27,7 +27,8 @@
|
|||
"jsdom": "^15.1.1",
|
||||
"node-fetch": "^2.6.0",
|
||||
"sqlite3": "^4.0.9",
|
||||
"typeorm": "^0.2.18"
|
||||
"typeorm": "^0.2.18",
|
||||
"uuid": "^3.3.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@electron-forge/cli": "^6.0.0-beta.43",
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
declare module 'uuid/v1';
|
||||
declare module 'uuid/v3';
|
||||
declare module 'uuid/v4';
|
||||
declare module 'uuid/v5';
|
|
@ -7,6 +7,7 @@ const ipcServer: IIpcServer = {
|
|||
handler(payload.data)
|
||||
.then((result: any) => {
|
||||
const response: IIpcResponse = {
|
||||
id: payload.id,
|
||||
success: true,
|
||||
data: result,
|
||||
};
|
||||
|
@ -14,6 +15,7 @@ const ipcServer: IIpcServer = {
|
|||
})
|
||||
.catch((reason: any) => {
|
||||
const response: IIpcResponse = {
|
||||
id: payload.id,
|
||||
success: false,
|
||||
error: reason.toString(),
|
||||
};
|
||||
|
|
|
@ -1,20 +1,25 @@
|
|||
import { ipcRenderer } from 'electron';
|
||||
import IpcMessageEvent = Electron.IpcMessageEvent;
|
||||
import { uuid } from '../../services/uuid';
|
||||
|
||||
const ipcClient: IIpcClient = {
|
||||
ask: (channel: IpcChannels, data?: any): Promise<any> => {
|
||||
const id = uuid();
|
||||
const payload: IIpcPayload = {
|
||||
id,
|
||||
data,
|
||||
};
|
||||
|
||||
return new Promise((resolve: (value?: any) => void, reject: (reason?: any) => void): void => {
|
||||
const listener = (event: IpcMessageEvent, response: IIpcResponse): void => {
|
||||
if (response.id === id) {
|
||||
if (response.success) {
|
||||
resolve(response.data);
|
||||
} else {
|
||||
reject(response.error);
|
||||
}
|
||||
ipcRenderer.removeListener(channel, listener);
|
||||
}
|
||||
};
|
||||
ipcRenderer.send(channel, payload);
|
||||
ipcRenderer.on(channel, listener);
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
import uuidv1 from 'uuid/v1';
|
||||
|
||||
const R = 0x52;
|
||||
const e = 0x65;
|
||||
const n = 0x6e;
|
||||
const a = 0x61;
|
||||
const i = 0x69;
|
||||
const nice = 0x45;
|
||||
|
||||
export function uuid(): string {
|
||||
return uuidv1({
|
||||
node: [R, e, n, a, i, nice],
|
||||
});
|
||||
}
|
|
@ -4,10 +4,12 @@ const enum IpcChannels {
|
|||
}
|
||||
|
||||
interface IIpcPayload {
|
||||
id: string;
|
||||
data: any;
|
||||
}
|
||||
|
||||
interface IIpcResponse {
|
||||
id: string;
|
||||
success: boolean;
|
||||
data?: any;
|
||||
error?: any;
|
||||
|
|
Loading…
Reference in New Issue