chore: upgrade prettier and fix linting errors
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
export interface CallbackStore {
|
||||
wrappedCallback: any;
|
||||
callback: any;
|
||||
wrappedCallback: any
|
||||
callback: any
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { Event } from '../Events';
|
||||
import { Event } from '../Events'
|
||||
|
||||
export interface EventBusInterface {
|
||||
subscribe<MessageType>(event: Event<MessageType>, callback: (msg: MessageType) => void): void;
|
||||
unsubscribeAll<MessageType>(event: Event<MessageType>): void;
|
||||
emit<MessageType>(event: Event<MessageType>, msg: MessageType): void;
|
||||
unsubscribe<MessageType>(event: Event<MessageType>, callback: any): void;
|
||||
subscribe<MessageType>(event: Event<MessageType>, callback: (msg: MessageType) => void): void
|
||||
unsubscribeAll<MessageType>(event: Event<MessageType>): void
|
||||
emit<MessageType>(event: Event<MessageType>, msg: MessageType): void
|
||||
unsubscribe<MessageType>(event: Event<MessageType>, callback: any): void
|
||||
}
|
||||
|
||||
@@ -1,34 +1,34 @@
|
||||
import { IpcMain } from 'electron';
|
||||
import { Event } from '../Events';
|
||||
import { EventBusInterface } from "./EventBusInterface";
|
||||
import { IpcMain } from 'electron'
|
||||
import { Event } from '../Events'
|
||||
import { EventBusInterface } from './EventBusInterface'
|
||||
|
||||
export class IpcMainEventBus implements EventBusInterface {
|
||||
private ipc: IpcMain;
|
||||
private client: any;
|
||||
constructor(ipc: IpcMain) {
|
||||
this.ipc = ipc;
|
||||
}
|
||||
private ipc: IpcMain
|
||||
private client: any
|
||||
constructor(ipc: IpcMain) {
|
||||
this.ipc = ipc
|
||||
}
|
||||
|
||||
public subscribe<MessageType>(subscribeEvent: Event<MessageType>, callback: (msg: MessageType) => void) {
|
||||
console.log('subscribing', subscribeEvent.topic);
|
||||
this.ipc.on(subscribeEvent.topic, (event: any, arg: any) => {
|
||||
this.client = event.sender;
|
||||
callback(arg);
|
||||
});
|
||||
}
|
||||
public subscribe<MessageType>(subscribeEvent: Event<MessageType>, callback: (msg: MessageType) => void) {
|
||||
console.log('subscribing', subscribeEvent.topic)
|
||||
this.ipc.on(subscribeEvent.topic, (event: any, arg: any) => {
|
||||
this.client = event.sender
|
||||
callback(arg)
|
||||
})
|
||||
}
|
||||
|
||||
public unsubscribeAll<MessageType>(event: Event<MessageType>) {
|
||||
console.log('unsubscribeAll', event.topic);
|
||||
this.ipc.removeAllListeners(event.topic);
|
||||
}
|
||||
public unsubscribeAll<MessageType>(event: Event<MessageType>) {
|
||||
console.log('unsubscribeAll', event.topic)
|
||||
this.ipc.removeAllListeners(event.topic)
|
||||
}
|
||||
|
||||
public unsubscribe<MessageType>(event: Event<MessageType>, callback: any) {
|
||||
throw new Error('Not implemented'); // Todo: implement
|
||||
}
|
||||
public unsubscribe<MessageType>(event: Event<MessageType>, callback: any) {
|
||||
throw new Error('Not implemented') // Todo: implement
|
||||
}
|
||||
|
||||
public emit<MessageType>(event: Event<MessageType>, msg: MessageType) {
|
||||
if (!this.client.isDestroyed()) {
|
||||
this.client.send(event.topic, msg);
|
||||
}
|
||||
public emit<MessageType>(event: Event<MessageType>, msg: MessageType) {
|
||||
if (!this.client.isDestroyed()) {
|
||||
this.client.send(event.topic, msg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { CallbackStore } from "./CallbackStore"
|
||||
import { EventBusInterface } from "./EventBusInterface"
|
||||
import { CallbackStore } from './CallbackStore'
|
||||
import { EventBusInterface } from './EventBusInterface'
|
||||
import { Event } from '../Events'
|
||||
import { IpcRenderer } from 'electron'
|
||||
|
||||
@@ -15,7 +15,7 @@ export class IpcRendererEventBus implements EventBusInterface {
|
||||
const wrappedCallback = (_: any, arg: any) => {
|
||||
callback(arg)
|
||||
}
|
||||
console.log("subscribing", event.topic)
|
||||
console.log('subscribing', event.topic)
|
||||
this.ipc.on(event.topic, wrappedCallback)
|
||||
this.callbacks.push({
|
||||
callback,
|
||||
|
||||
@@ -1,53 +1,60 @@
|
||||
import { Event } from '../Events';
|
||||
import { Event } from '../Events'
|
||||
import { EventBusInterface } from './EventBusInterface'
|
||||
import { v4 } from 'uuid';
|
||||
import { v4 } from 'uuid'
|
||||
|
||||
export type RpcEvent<RequstType, ResponseType> = {
|
||||
topic: string;
|
||||
};
|
||||
topic: string
|
||||
}
|
||||
|
||||
export class Rpc {
|
||||
constructor(private participant: EventBusInterface) { }
|
||||
constructor(private participant: EventBusInterface) {}
|
||||
|
||||
async call<RpcRequest, RpcResponse>(event: RpcEvent<RpcRequest, RpcResponse>, request: RpcRequest, timeout: number = 0): Promise<RpcResponse> {
|
||||
return new Promise((resolve, reject) => {
|
||||
let id = v4();
|
||||
async call<RpcRequest, RpcResponse>(
|
||||
event: RpcEvent<RpcRequest, RpcResponse>,
|
||||
request: RpcRequest,
|
||||
timeout: number = 0
|
||||
): Promise<RpcResponse> {
|
||||
return new Promise((resolve, reject) => {
|
||||
let id = v4()
|
||||
|
||||
let responseEvent: Event<any> = { topic: `${event.topic}/response/${id}` };
|
||||
let requestEvent: Event<any> = { topic: `${event.topic}/request` };
|
||||
let callback = (result: { id: string; payload: RpcResponse; error: unknown }) => {
|
||||
this.participant.unsubscribe(responseEvent as any, callback);
|
||||
if (result.error) {
|
||||
reject(result.error)
|
||||
} else {
|
||||
resolve(result.payload);
|
||||
}
|
||||
console.log("received", result)
|
||||
};
|
||||
this.participant.subscribe(responseEvent, callback);
|
||||
this.participant.emit(requestEvent, { id, payload: request });
|
||||
let responseEvent: Event<any> = { topic: `${event.topic}/response/${id}` }
|
||||
let requestEvent: Event<any> = { topic: `${event.topic}/request` }
|
||||
let callback = (result: { id: string; payload: RpcResponse; error: unknown }) => {
|
||||
this.participant.unsubscribe(responseEvent as any, callback)
|
||||
if (result.error) {
|
||||
reject(result.error)
|
||||
} else {
|
||||
resolve(result.payload)
|
||||
}
|
||||
console.log('received', result)
|
||||
}
|
||||
this.participant.subscribe(responseEvent, callback)
|
||||
this.participant.emit(requestEvent, { id, payload: request })
|
||||
|
||||
if (timeout > 0) {
|
||||
setTimeout(() => {
|
||||
reject(new Error(`Did not respond to ${event.topic} within ${timeout}ms`))
|
||||
this.participant.unsubscribe(responseEvent as any, callback);
|
||||
}, 10000)
|
||||
}
|
||||
});
|
||||
}
|
||||
if (timeout > 0) {
|
||||
setTimeout(() => {
|
||||
reject(new Error(`Did not respond to ${event.topic} within ${timeout}ms`))
|
||||
this.participant.unsubscribe(responseEvent as any, callback)
|
||||
}, 10000)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
async on<RpcRequest, RpcResponse>(event: RpcEvent<RpcRequest, RpcResponse>, handler: (request: RpcRequest) => Promise<RpcResponse>) {
|
||||
this.participant.subscribe<RpcRequest>({ topic: `${event.topic}/request` } as RpcEvent<any, any>, async (request) => {
|
||||
let payload;
|
||||
let error;
|
||||
try {
|
||||
payload = await handler((request as any).payload);
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
const id = (request as any).id
|
||||
console.log(`${event.topic}/response/${id}`, payload, error)
|
||||
this.participant.emit({ topic: `${event.topic}/response/${id}` }, { id, payload, error });
|
||||
});
|
||||
}
|
||||
async on<RpcRequest, RpcResponse>(
|
||||
event: RpcEvent<RpcRequest, RpcResponse>,
|
||||
handler: (request: RpcRequest) => Promise<RpcResponse>
|
||||
) {
|
||||
this.participant.subscribe<RpcRequest>({ topic: `${event.topic}/request` } as RpcEvent<any, any>, async request => {
|
||||
let payload
|
||||
let error
|
||||
try {
|
||||
payload = await handler((request as any).payload)
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
const id = (request as any).id
|
||||
console.log(`${event.topic}/response/${id}`, payload, error)
|
||||
this.participant.emit({ topic: `${event.topic}/response/${id}` }, { id, payload, error })
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Base64Message } from '../backend/src/Model/Base64Message'
|
||||
import { DataSourceState, MqttOptions } from '../backend/src/DataSource'
|
||||
import { UpdateInfo } from 'builder-util-runtime'
|
||||
import { RpcEvent } from './EventSystem/Rpc';
|
||||
import { RpcEvent } from './EventSystem/Rpc'
|
||||
|
||||
export type Event<MessageType> = {
|
||||
topic: string
|
||||
@@ -52,5 +52,5 @@ export function makeConnectionMessageEvent(connectionId: string): Event<MqttMess
|
||||
}
|
||||
|
||||
export const getAppVersion: RpcEvent<void, string> = {
|
||||
topic: `getAppVersion`
|
||||
topic: `getAppVersion`,
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { OpenDialogOptions, OpenDialogReturnValue } from 'electron';
|
||||
import { RpcEvent } from './EventSystem/Rpc';
|
||||
import { OpenDialogOptions, OpenDialogReturnValue } from 'electron'
|
||||
import { RpcEvent } from './EventSystem/Rpc'
|
||||
|
||||
export function makeOpenDialogRpc(): RpcEvent<OpenDialogOptions, OpenDialogReturnValue> {
|
||||
return {
|
||||
topic: `openDialog`
|
||||
};
|
||||
return {
|
||||
topic: `openDialog`,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
export * from './Events'
|
||||
export * from './EventSystem/EventDispatcher'
|
||||
export * from './EventSystem/EventBus'
|
||||
export * from './EventSystem/EventBusInterface'
|
||||
export * from './EventSystem/EventBusInterface'
|
||||
|
||||
Reference in New Issue
Block a user