-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. This will fix the issue eggjs/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
Showing
15 changed files
with
226 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
title: controller | ||
title: Controller | ||
--- | ||
|
||
## What is Controller | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)两篇文章。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters