typedelectron/ts/classes.typedelectronbackend.ts

35 lines
1.1 KiB
TypeScript
Raw Normal View History

2023-12-06 01:16:36 +00:00
import * as plugins from './plugins.js';
export class TypedElectronBackend {
// STATIC
public static async createTypedElectronBackend() {
return new TypedElectronBackend();
}
// INSTANCE
public typedrouter = new plugins.typedrequest.TypedRouter();
constructor() {
2023-12-06 18:04:14 +00:00
plugins.electron.ipcMain.on('typedrequest', async (eventArg, payloadArg) => {
const updatedPayload = await this.typedrouter.routeAndAddResponse({ ...payloadArg }); // TODO: check how to pass on eventArg
eventArg.sender.send('typedrequest', updatedPayload);
2023-12-06 01:16:36 +00:00
});
}
createTypedRequest<T extends plugins.typedrequestInterfaces.ITypedRequest>(
methodName: T['method'],
2023-12-06 10:41:10 +00:00
windowArg: plugins.electron.BrowserWindow
2023-12-06 01:16:36 +00:00
) {
2023-12-06 10:41:10 +00:00
const typedrequest = new plugins.typedrequest.TypedRequest<T>(
new plugins.typedrequest.TypedTarget({
typedRouterRef: this.typedrouter,
postMethodWithTypedRouter: async (payloadArg: T) => {
windowArg.webContents.send('typedrequest', payloadArg);
},
}),
methodName
);
2023-12-06 01:16:36 +00:00
return typedrequest;
}
}