feat: use uuid to get the correct ipc response only in the promise call that the request came from

This commit is contained in:
Xymorot 2019-07-30 23:58:58 +02:00
parent 763d954775
commit 3079c33d37
6 changed files with 34 additions and 6 deletions

View File

@ -27,7 +27,8 @@
"jsdom": "^15.1.1", "jsdom": "^15.1.1",
"node-fetch": "^2.6.0", "node-fetch": "^2.6.0",
"sqlite3": "^4.0.9", "sqlite3": "^4.0.9",
"typeorm": "^0.2.18" "typeorm": "^0.2.18",
"uuid": "^3.3.2"
}, },
"devDependencies": { "devDependencies": {
"@electron-forge/cli": "^6.0.0-beta.43", "@electron-forge/cli": "^6.0.0-beta.43",

4
src/declarations/uuid.d.ts vendored Normal file
View File

@ -0,0 +1,4 @@
declare module 'uuid/v1';
declare module 'uuid/v3';
declare module 'uuid/v4';
declare module 'uuid/v5';

View File

@ -7,6 +7,7 @@ const ipcServer: IIpcServer = {
handler(payload.data) handler(payload.data)
.then((result: any) => { .then((result: any) => {
const response: IIpcResponse = { const response: IIpcResponse = {
id: payload.id,
success: true, success: true,
data: result, data: result,
}; };
@ -14,6 +15,7 @@ const ipcServer: IIpcServer = {
}) })
.catch((reason: any) => { .catch((reason: any) => {
const response: IIpcResponse = { const response: IIpcResponse = {
id: payload.id,
success: false, success: false,
error: reason.toString(), error: reason.toString(),
}; };

View File

@ -1,20 +1,25 @@
import { ipcRenderer } from 'electron'; import { ipcRenderer } from 'electron';
import IpcMessageEvent = Electron.IpcMessageEvent; import IpcMessageEvent = Electron.IpcMessageEvent;
import { uuid } from '../../services/uuid';
const ipcClient: IIpcClient = { const ipcClient: IIpcClient = {
ask: (channel: IpcChannels, data?: any): Promise<any> => { ask: (channel: IpcChannels, data?: any): Promise<any> => {
const id = uuid();
const payload: IIpcPayload = { const payload: IIpcPayload = {
id,
data, data,
}; };
return new Promise((resolve: (value?: any) => void, reject: (reason?: any) => void): void => { return new Promise((resolve: (value?: any) => void, reject: (reason?: any) => void): void => {
const listener = (event: IpcMessageEvent, response: IIpcResponse): void => { const listener = (event: IpcMessageEvent, response: IIpcResponse): void => {
if (response.success) { if (response.id === id) {
resolve(response.data); if (response.success) {
} else { resolve(response.data);
reject(response.error); } else {
reject(response.error);
}
ipcRenderer.removeListener(channel, listener);
} }
ipcRenderer.removeListener(channel, listener);
}; };
ipcRenderer.send(channel, payload); ipcRenderer.send(channel, payload);
ipcRenderer.on(channel, listener); ipcRenderer.on(channel, listener);

14
src/services/uuid.ts Normal file
View File

@ -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],
});
}

View File

@ -4,10 +4,12 @@ const enum IpcChannels {
} }
interface IIpcPayload { interface IIpcPayload {
id: string;
data: any; data: any;
} }
interface IIpcResponse { interface IIpcResponse {
id: string;
success: boolean; success: boolean;
data?: any; data?: any;
error?: any; error?: any;