main.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. const Appliaction = require('ee-core').Appliaction;
  2. const getPort = require('get-port');
  3. const { app } = require('electron');
  4. class Main extends Appliaction {
  5. constructor() {
  6. super();
  7. // this === eeApp;
  8. }
  9. /**
  10. * core app have been loaded
  11. */
  12. async ready () {
  13. // do some things
  14. await this.createJavaPorts();
  15. await this.startJava();
  16. }
  17. async createJavaPorts() {
  18. if (this.config.javaServer.enable) {
  19. const javaPort = await getPort({ port: this.config.javaServer.port });
  20. process.env.EE_JAVA_PORT = javaPort;
  21. this.config.javaServer.port = javaPort;
  22. }
  23. // 更新config配置
  24. this.getCoreDB().setItem("config", this.config);
  25. }
  26. async startJava() {
  27. this.logger.info("[main] startJava start");
  28. const javaServer = require("./public/lib/javaServer");
  29. javaServer.start(this);
  30. this.logger.info("[main] startJava end");
  31. }
  32. /**
  33. * electron app ready
  34. */
  35. async electronAppReady () {
  36. // do some things
  37. }
  38. /**
  39. * main window have been loaded
  40. */
  41. async windowReady () {
  42. // do some things
  43. // 延迟加载,无白屏
  44. const winOpt = this.config.windowsOption;
  45. if (winOpt.show == false) {
  46. const win = this.electron.mainWindow;
  47. win.once('ready-to-show', () => {
  48. win.show();
  49. })
  50. }
  51. const self = this;
  52. this.electron.mainWindow.webContents.on("did-finish-load", () => {
  53. const updateFrontend = require('./public/lib/updateFrontend');
  54. updateFrontend.install(self);
  55. });
  56. app.on("before-quit", async () => {
  57. await this.killJava();
  58. });
  59. }
  60. /**
  61. * before app close
  62. */
  63. async beforeClose () {
  64. // do some things
  65. }
  66. async killJava() {
  67. if (this.config.javaServer.enable) {
  68. const javaServer = require("./public/lib/javaServer");
  69. await javaServer.kill(this);
  70. }
  71. }
  72. }
  73. new Main();