生命周期
# 介绍
项目入口文件及生命周期
提示
v3.3.0 将 main.js中业务代码,放到了 ./electron/index.js 里面。
这样所有的业务代码都在 ./electron 文件夹下,方便业务统一与全局代码加密。
# 入口 main.js
该文件只做实例化操作,启动 ElectronEgg
const { ElectronEgg } = require('ee-core');
new ElectronEgg();
# 生命周期
new ElectronEgg()
实例化会触发 ./electron/index.js
模块,core功能加载顺序如下:
// 引入基础 Application 类
const { Application } = require('ee-core');
class Index extends Application {
constructor() {
super();
}
/**
* core app have been loaded
* 加载
* config -> service -> controller -> socket -> ready()
*/
async ready () {
// do some things
}
/**
* electron app ready
* 加载以下事件
* app.on('second-instance')
* app.whenReady().then() 该事件会创建 mainWindow
* app.on('window-all-closed')
* app.on('before-quit')
* 然后触发
* -> electronAppReady()
*/
async electronAppReady () {
// do some things
}
/**
* main window have been loaded
* mainWindow 被创建后,加载
* windowReady() -> addon -> preload
*/
async windowReady () {
// do some things
// 延迟加载,无白屏
const winOpt = this.config.windowsOption;
if (winOpt.show == false) {
const win = this.electron.mainWindow;
win.once('ready-to-show', () => {
win.show();
})
}
}
/**
* before app close
*/
async beforeClose () {
// do some things
}
}
Index.toString = () => '[class Index]';
module.exports = Index;
# 旧版本
# main.js
# 引入框架 Application
const { Application } = require('ee-core');
# 引入 EE 全局对象
const EE = require('ee-core/ee');
# main类
class Main extends Application {
constructor() {
super();
// this对象等于ee框架的全局app对象
}
/**
* core app have been loaded
* 框架核心功能加载完成
*/
async ready () {
// do some things
}
/**
* electron app ready
* Electron完成初始化,主窗口创建之前
*/
async electronAppReady () {
// do some things
}
/**
* main window have been loaded
* 主窗口创建完成
*/
async windowReady () {
// do some things
}
/**
* before app close
* app退出之前
*/
async beforeClose () {
// do some things
}
}
// Instantiate an app object
EE.app = new Main();
上次更新: 2024/11/28, 09:50:56