Skip to content

Commit

Permalink
Merge pull request #937 from nukeop/fix/746
Browse files Browse the repository at this point in the history
Fix #746 and #832
  • Loading branch information
nukeop authored May 11, 2021
2 parents ef11ed9 + 01bd256 commit c761122
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions packages/main/src/utils/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Container extends InversifyContainer {
const logger = this.get<Logger>($mainLogger);

logger.log(`Async service loaded => ${service.name}`);

this.bind(provide).to(service).inSingletonScope();
}

Expand All @@ -39,26 +39,32 @@ class Container extends InversifyContainer {
const logger = this.get<Logger>($ipcLogger);

this.controllers.forEach((Controller) => {
this.bind(Controller).to(Controller).inSingletonScope();
const meta: ControllerMeta[] = Reflect.getMetadata(IPC_EVENT_KEY, Controller.prototype);

const controller = this.get(Controller);
meta.forEach(({ eventName, name, once }) => {
const on = once ? ipc.once : ipc.on;
try {
this.bind(Controller).to(Controller).inSingletonScope();
const meta: ControllerMeta[] = Reflect.getMetadata(IPC_EVENT_KEY, Controller.prototype);

const controller = this.get(Controller);
meta.forEach(({ eventName, name, once }) => {
const on = once ? ipc.once : ipc.on;

on.bind(ipc)(eventName, (event: Event, data: any) => {
logger.logEvent({ once, direction: 'in', event: eventName, data });
on.bind(ipc)(eventName, (event: Event, data: any) => {
logger.logEvent({ once, direction: 'in', event: eventName, data });

const result = controller[name](event, data);
const result = controller[name](event, data);

if (result instanceof Promise) {
result.catch((err: any) => {
logger.error(`error in event ${eventName} => ${err.message}`);
logger.error(err.stack);
});
}
if (result instanceof Promise) {
result.catch((err: any) => {
logger.error(`error in event ${eventName} => ${err.message}`);
logger.error(err.stack);
});
}
});
});
});
} catch (e) {
logger.error('Service failed to initialize');
logger.error(e);
}
});
}
}
Expand Down

0 comments on commit c761122

Please sign in to comment.