Skip to content

Commit

Permalink
doc:Add new loaderUpdate.md (#3395)
Browse files Browse the repository at this point in the history
1. This will fix the issue eggjs/egg-core#195 and gives a good answer to #3362. We should notify users how to upgrade their lifecyle events quickly.

2. Fix some '##title', we should directly use 'title:...' instead.

3. Due to the conflict of typescript of nodejs in the global/local, we've forcely defined the type path.
  • Loading branch information
Maledong authored Jan 22, 2019
1 parent 7176800 commit b3256b5
Show file tree
Hide file tree
Showing 15 changed files with 226 additions and 19 deletions.
98 changes: 98 additions & 0 deletions docs/source/en/advanced/loaderUpdate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
title: Upgrade your event functions in your lifecycle
---

We've simplified the functions of our lifecycle for your convenience to control when to load application or plugins. Generally speaking, the lifecycle events can be divided into two forms:

1. Function (already deprecated, just for compatibility).
2. Class Method (recommanded).

## Replacer for `beforeStart`

We usually handle `beforeStart` through `module.export` with the input parameter `app` in the app.js.
Take this as an example below:

```js
module.exports = app => {
app.beforeStart(async () => {
// Here's where your codes were before
});
};
```
Now we've got some changes after upgration: We can use methods in a class in `app.js`. For application, we should write in the `WillReady`, for plugins, `didLoad` is our choice. They look like below:

```js
// app.js or agent.js:
class AppBootHook {

constructor(app) {
this.app = app;
}

async didLoad() {
// Please put your codes of `app.beforeStart` here for your plugin
}

async willReady() {
// Please put your codes of `app.beforeStart` here for your application
}
}

module.exports = AppBootHook;
```
## Replacer for `ready`

We used to process our logic in `app.ready`:

```js
module.exports = app => {
app.ready(async () => {
// Here's where your codes were before
});
};
```
Now `didReady` takes the place of it:

```js
// app.js or agent.js:
class AppBootHook {

constructor(app) {
this.app = app;
}

async didReady() {
// Please put your codes of `app.ready` here
}
}

module.exports = AppBootHook;
```
## Replacer for `beforeClose`

We used to handle `app.beforeClose` like this following:

```js
module.exports = app => {
app.beforeClose(async () => {
// Here's where your codes were before
});
};
```
Now we can use `beforeClose` instead of it:

```js
// app.js or agent.js:
class AppBootHook {

constructor(app) {
this.app = app;
}

async beforeClose() {
// Please put your codes of `app.beforeClose` here
}
}
```
## Others

In order to let you pick up quickly to replace your old functions, this torturial only tells you how to replace item by item. So if you want to know more about the whole principle of `Loader`, as well as all the functions of Egg's lifecycle, Please refer to [Loader](./loader.md) and [Application Startup Configuration](../basics/app-start.md).
3 changes: 2 additions & 1 deletion docs/source/en/advanced/view-plugin.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
## Title: View Plugin Development
title: View Plugin Development
---

In most cases, we need to read the data, render the template and then present it to the user. The framework does not force to use one template engine, but allows developers to select the [template](../core/view.md) by themselves. For details, see [Template Rendering](../core/view.md).

Expand Down
5 changes: 4 additions & 1 deletion docs/source/en/basics/app-start.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Application Startup Configuration
title: Application Startup Configuration
---

When the application starts up, we often need to set up some initialization logic. The application bootstraps with those specific configurations. It is in a healthy state and be able to take external service requests after those configurations successfully applied. Otherwise, it failed.

Expand Down Expand Up @@ -78,3 +79,5 @@ class AppBootHook {
```

**Note: It is not recommended to do long-time operations in the custom lifecycle function, because the framework has a startup timeout detection.**

If your Egg's life-cycle functions are old, we suggest you upgrading to the "class-method" mode. For more you can refer to [Upgrade your event functions in your lifecycle](../advanced/loaderUpdate.md).
2 changes: 1 addition & 1 deletion docs/source/en/basics/controller.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
title: controller
title: Controller
---

## What is Controller
Expand Down
3 changes: 2 additions & 1 deletion docs/source/en/basics/plugin.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
## title: Plugin
title: Plugin
---

Plugin mechanism is a major feature of our framework. Not only can it ensure that the core of the framework is sufficiently streamlined, stable and efficient, but also can promote the reuse of business logic and the formation of an ecosystem. In the following sections, we will try to answer questions such as

Expand Down
3 changes: 2 additions & 1 deletion docs/source/en/resource.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
## Title: Resources
title: Resources
---

* [awesome-egg](https://github.com/eggjs/awesome-egg)

Expand Down
3 changes: 2 additions & 1 deletion docs/source/en/style-guide.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
## Title: Code Style Guide
title: Code Style Guide
---

Developers are advised to use `egg-init --type=simple showcase` to generate and observe the recommended project structure and configuration.

Expand Down
3 changes: 2 additions & 1 deletion docs/source/en/tutorials/passport.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
## Title: Passport
title: Passport
---

**`Login authentication`** is a common business scenario, including "account password login" and "third-party unified login".

Expand Down
3 changes: 2 additions & 1 deletion docs/source/en/tutorials/socketio.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
## title: Socket.IO
title: Socket.IO
---

**Socket.IO** is a real-time application framework based on Node.js, which has a wide range of applications including instant messaging, notification and message push, real-time analysis and other scenarios.

Expand Down
13 changes: 6 additions & 7 deletions docs/source/zh-cn/advanced/loader.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,12 +261,11 @@ module.exports = AppBootHook;

## ready

`ready` 方法注册的任务在 load 结束并且所有的 `beforeStart` 方法执行结束后顺序执行, HTTP server 监听也是在这个时候开始, 此时代表所有的插件已经加载完毕并且准备工作已经完成, 一般用来执行一些启动的后置任务。
开发者应使用 `didReady` 替换。
`ready` 方法注册的任务在 load 结束并且所有的 `beforeStart` 方法执行结束后顺序执行, HTTP server 监听也是在这个时候开始, 此时代表所有的插件已经加载完毕并且准备工作已经完成, 一般用来执行一些启动的后置任务。开发者应使用 `didReady` 替换。

## beforeClose

`beforeClose` 注册方法在 app/agent 实例的 `close` 方法被调用后, 按注册的逆序执行。一般用于资源的释放操作, 例如 [`egg`](https://github.com/eggjs/egg/blob/master/lib/egg.js) 用来关闭 logger , 删除监听方法等。开发者不应该直接使用 `app.beforeClose`, 而是定义类的形式, 实现 `beforeClose` 方法。
`beforeClose` 注册方法在 app/agent 实例的 `close` 方法被调用后, 按注册的逆序执行。一般用于资源的释放操作, 例如 [`egg`](https://github.com/eggjs/egg/blob/master/lib/egg.js) 用来关闭 logger删除监听方法等。开发者不应该直接使用 `app.beforeClose`, 而是定义类的形式, 实现 `beforeClose` 方法。

__这个方法不建议在生产环境使用, 可能遇到未执行完就结束进程的问题。__

Expand All @@ -285,7 +284,7 @@ Loader 还提供了 [caseStyle](#caseStyle-string) 强制指定首字母大小

## 扩展 Loader

[Loader] 是一个基类,并根据文件加载的规则提供了一些内置的方法,但基本本身并不会去调用,而是由继承类调用。
[Loader] 是一个基类,并根据文件加载的规则提供了一些内置的方法,它本身并不会去调用这些方法,而是由继承类调用。

- loadPlugin()
- loadConfig()
Expand Down Expand Up @@ -346,13 +345,13 @@ module.exports = Object.assign(egg, {
});
```

通过 Loader 提供的这些 API,可以很方便的定制团队的自定义加载,如 `this.model.xx` `app/extend/filter.js` 等等。
通过 Loader 提供的这些 API,可以很方便的定制团队的自定义加载,如 `this.model.xx``app/extend/filter.js` 等等。

以上只是说明 Loader 的写法,具体可以查看[框架开发](./framework.md)

## Loader API
## 加载器函数(Loader API

Loader 还提供一些底层的 API,在扩展时可以简化代码,全部 API 请[查看](https://github.com/eggjs/egg-core#eggloader)
Loader 还提供一些底层的 API,在扩展时可以简化代码,[点击此处](https://github.com/eggjs/egg-core#eggloader)查看所有相关 API。

### loadFile

Expand Down
97 changes: 97 additions & 0 deletions docs/source/zh-cn/advanced/loaderUpdate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
title: 升级你的生命周期事件函数
---

为了使得大家更方便地控制加载应用和插件的时机,我们对 Loader 的生命周期函数进行了精简处理。概括地说,生命周期事件目前总共可以分成两种形式:

1. 函数形式(已经作废,仅为兼容保留)。
2. 类形式(推荐使用)。

## beforeStart 函数替代

我们通常在 app.js 中通过 `module.export` 中传入的 `app` 参数进行此函数的操作,一个典型的例子:

```js
module.exports = app => {
app.beforeStart(async () => {
// 此处是你原来的逻辑代码
});
};
```
现在升级之后的写法略有改变 —— 我们可以直接在 `app.js` 中用类方法的形式体现出来:对于应用开发而言,我们应该写在 `willReady` 方法中;对于插件则写在 `didLoad` 中。形式如下:

```js
// app.js 或 agent.js 文件:
class AppBootHook {

constructor(app) {
this.app = app;
}

async didLoad() {
// 请将你的插件项目中 app.beforeStart 中的代码置于此处。
}

async willReady() {
// 请将你的应用项目中 app.beforeStart 中的代码置于此处。
}
}

module.exports = AppBootHook;
```
## ready 函数替代

同样地,我们之前在 `app.ready` 中处理我们的逻辑:

```js
module.exports = app => {
app.ready(async () => {
// 此处是你原来的逻辑代码
});
};
```
现在直接用 `didReady` 进行替换:

```js
// app.js 或 agent.js 文件:
class AppBootHook {

constructor(app) {
this.app = app;
}

async didReady() {
// 请将您的 app.beforeStart 中的代码置于此处。
}
}

module.exports = AppBootHook;
```
## beforeClose 函数替代

原先的 `app.beforeClose` 如以下形式:

```js
module.exports = app => {
app.beforeClose(async () => {
// 此处是你原来的逻辑代码
});
};
```
现在我们只需使用类方法 `beforeClose` 替代即可:

```js
// app.js 或 agent.js 文件:
class AppBootHook {

constructor(app) {
this.app = app;
}

async beforeClose() {
// 请将您的 app.beforeClose 中的代码置于此处。
}
}
```
## 其它说明

本教程只是一对一地讲了替换方法,便于开发者们快速上手进行替换;若想要具体了解整个 Loader 原理以及生命周期的完整函数版本,请参考[加载器](./loader.md)[启动自定义](../basics/app-start.md)两篇文章。
2 changes: 2 additions & 0 deletions docs/source/zh-cn/basics/app-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,5 @@ class AppBootHook {
```

**注意:在自定义生命周期函数中不建议做太耗时的操作,框架会有启动的超时检测。**

如果你的 Egg 框架的生命周期函数是旧版本的,建议你升级到类方法模式;详情请查看[升级你的生命周期事件函数](../advanced/loaderUpdate.md)
2 changes: 1 addition & 1 deletion docs/source/zh-cn/basics/middleware.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
title: Middleware 中间件
title: 中间件(Middleware)
---

[前面的章节](../intro/egg-and-koa.md)中,我们介绍了 Egg 是基于 Koa 实现的,所以 Egg 的中间件形式和 Koa 的中间件形式是一样的,都是基于[洋葱圈模型](../intro/egg-and-koa.md#midlleware)。每次我们编写一个中间件,就相当于在洋葱外面包了一层。
Expand Down
2 changes: 1 addition & 1 deletion docs/source/zh-cn/core/i18n.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
title: I18n 国际化
title: 国际化(I18n)
---

为了方便开发多语言应用,框架内置了国际化(I18n)支持,由 [egg-i18n](https://github.com/eggjs/egg-i18n) 插件提供。
Expand Down
6 changes: 4 additions & 2 deletions test/fixtures/apps/app-ts/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
"target": "es2017",
"baseUrl": ".",
"paths": {
"egg": ["../../../../index"]
"egg": [
"../../../../index"
]
},
"module": "commonjs",
"strict": true,
"noImplicitAny": false
}
}
}

0 comments on commit b3256b5

Please sign in to comment.